Home
About NuDesign
Sales
Support
Eval Download
Partners
Testimonials
SNMP Links
Tutorials
News

Tutorial for Visual xAgentBuilder for C++

Lesson 2: CLI

In this lesson we’ll show how to handle command line interface using built in support.

Here is the source code:

 

int main(int argc, char* argv[])

{

   BTime tm;

 

   BMibHandlerEx mh;

   mh.Init(&tm, NULL);

 

   BCLI cli;

 

   MyCmdHandler mycmdhndlr(mh);

   cli.Register( &mycmdhndlr );

 

   cli.Stream().Write(HEADING);

   cli.Start("ndmp# ");

 

   return 0;

}

 

Command line interface is handled in BCLI class. BCLI class interacts with console through the ICmdStream interface. If not provided in the constructor, default one is used (class BCmdStream), which outputs using printf and reads using getchar function.

BCLI class maintains collection of “command handlers”. Command handler is object that implements ICmdHandler interface. To simplify development of command handlers, class BCmdHandler is created. It implements lookup table for storing command names and corresponding ids, and six out of seven methods defined in ICmdHandler interface. In other words, user’s command handler implementation has to deal with only one method.

BCmdHandler class is used as the base class for MyCmdHandler class in this lesson.

 

class MyCmdHandler : public BCmdHandler

{

private:

   enum ECmd

   {

       eCmdDescr= 0,

       eCmdOID

   };

 

. . .

 

   void CmdExecute( ICmdStream& stream,

                    const BString& cmd,

                    const BStringArray& args,

                    const BSecurityParams& secParams,

                    const BString& ctxName)

   {

          // is it known command?

          tInt32 cmdId = CmdId(cmd, stream);

          if (cmdId < 0)

              return;

 

          // ok try to execute command

          switch (cmdId)

          {

          case eCmdDescr:

               . . .

              break;

 

          case eCmdOID:

              . . .

              break;

          }

       }

 

   DECLARE_CMD_MAP()

 

. . .

};

 

 

BEGIN_CMD_MAP(MyCmdHandler, "MY Command Handler")

   CMD_ENTRY( eCmdDescr, "sysdescr", "Display sysDescr",    "" )

   CMD_ENTRY( eCmdOID,   "sysoid",   "Display sysObjectID", ""  )

END_CMD_MAP()

 

 

Lookup table is defined as a list of CMD_ENTRY-s between BEGIN_CMD_MAP and END_CMD_MAP. Don’t forget DECLARE_CMD_MAP in class declaration.

The first argument for of CMD_ENTRY is command id (for convenience declared as enumeration at the top of the class), the second is command name, third one is short description (that will be displayed when user enters command “help <cmdname>”, and the last one is description of arguments.

The only method from ICmdHandler interface that we must implement here is CmdExecute.

Now back to main:

 

   MyCmdHandler mycmdhndlr(mh);

   cli.Register( &mycmdhndlr );

 

In the first line our command handler is created and in the second registered with cli. Following is the line that prints the name of the lesson on the screen and then BCLI’s Start method is called.

 

   cli.Start("ndmp# ");

 

Start method is implemented as loop. Inside the loop prompt is output to the screen (or to wherever command stream’s Write method sends it). BCLI then waits for the user input. In other words, Start is blocking call.  How does the application ends then?

The second parameter for the start method is “quit” command, which is by default single character ‘q’. In other words, to exit the application user has to enter ‘q’ on the prompt.

We defined two commands in our handler sysdescr and sysoid. You should try both and check the result. Please note that it is enough to enter juts enough characters to make command unique. For example sysd, sysde or sysdescr will all execute the same command.

MIB handler is also command handler. As the final exercise in this lesson, add following line to the main:

 

   cli.Register( &mh );

 

Start the application and type ‘?” on the command prompt. You will be presented with the list of all available commands.

 

Please try different commands and when you are done proceed to Lesson3: SNMP topic.

 

Previous    Next