CTBCAFCli

From TBwiki
Jump to: navigation, search

The class CTBCAFCli provides a command-line interface that's more convenient that a single shell prompt, by allowing the application to display multiple pages of information, view the application log, and enter commands.

It also allows the application to be "remotely controlled" using the tbx_cli_tools_remote application.

This module should be initialized through the GlobalSetParams function. Parameters for this module are contained in class CTBCAFCliParams

Please refer to documentation in the C++ header files for class CTBCAFCliParams to know more about available parameters.

An application that initializes this class has add one or more "Pages" (classes based on ITBCAFCliPage). The base class 'ITBCAFCliPage' has a couple of functions that need to be implemented:

  • Display: Called every time the page needs to be refreshed on the screen. Application can present any relevant information on that page. Remaining space will automatically be used to show the log file.
  • HandleChoice: Called when user pressed a key on the keyboard. Application can attach any behavior to any key. Some behaviors may prompt user for parameters (see example code below).
  • ValidateUserInput: Called when it's required to prompt user for values after a command key has been pressed. This callback asks the application to validate the user input.
  • ApplyUserInput: Called when the user finished entering the prompted values, and command is ready to be executed. Application should execute the command according to passed user input values.
  • GetTitle: The application should return the title that it wants the page to appear with
  • IsEnabled: The application should return whether current page should be visible or not
  • SetEnabled: Called to enable the current page
  • GetHelp: The application should return the help string to use with current page.


Example usage

class CTBMyCliPage : public ITBCAFCliPage
{
	// See sample code packages for example code using ITBCAFCliPage 
};

class MyApplication
{
	// your class definition

	// Smart pointer to a CLI Page
	CTBCAFPtr< CTBMyCliPage >		mptrMyCliPage;
};

TBX_RESULT MyApplication::Init()
{
	TBX_RESULT			Result = TBX_RESULT_OK;
	CTBCAFGlobalsParams	CafGlobalParams;
	CTBCAFCliParams		CafCliParams;

	// Set the parameters you want
	CafCliParams.mfRemoteControllable	= TBX_TRUE;	// Make current application controllable through tbx_cli_tools_remote
	CafCliParams.mfRunInServiceMode		= TBX_TRUE;	// Prevents CAFCli to use stdin or stdout
	// (other parameters are generally left to default values)

	CafGlobalParams.mpCliParams			= &CafCliParams;
	// CafGlobalParams.mpCommParams		= [your paramters...]
	// CafGlobalParams.mpLogParams		= [your paramters...]
	// etc...

	Result = TBCAF::GlobalSetParam( &CafGlobalParams );

	if( TBX_RESULT_SUCCESS( Result ) )
	{
		// Allocate a new CTBCAFCli page
		mptrMyCliPage = tbnew CTBMyCliPage();
	}

	if( mptrMyCliPage != NULL )
	{
		// Register cli page
		Result = CTBCAFCli::AddPage( mptrMyCliPage );
	}

	return Result;
}

TBX_RESULT MyApplication::UnInit()
{
	TBX_RESULT				Result = TBX_RESULT_OK;
	CTBCAFGlobalsParams		EmptyGlobalParams;

	// Detach and free the page
	if( mptrMyCliPage != NULL )
	{
		// Unregister cli page
		CTBCAFCli::RemovePage( mptrMyCliPage );
	}
	mptrMyCliPage = NULL;

	// Terminate all CAF modules
	Result = TBCAF::GlobalSetParam( &EmptyGlobalParams );


	return Result;
}
Personal tools