appkit  1.5.1
RoadNarrows Robotics Application Kit
rnr_eg_cli

Classes

struct  AnimalInfo
 
struct  AppCmdExec
 Command description and exectuion structure. More...
 
struct  AppCmdExec3
 Self-Reference command description and exectuion structure. More...
 

Macros

#define APP_EC_OK   0
 success exit code
 
#define APP_EC_ARGS   2
 command-line options/arguments error exit code
 
#define APP_EC_EXEC   4
 execution exit code
 

Enumerations

enum  Activity {
  ActIdle,
  ActSleeping,
  ActEating,
  ActWalking
}
 Animal activities. More...
 

Functions

static ostream & operator<< (ostream &os, const char *art[])
 ASCII art output stream operator. More...
 
static void showAsciiArt (ostream &os, const char *art0[], const char *art1[], const char *art2[]=NULL, const char *art3[]=NULL)
 Write a set of ASCII art to cout. More...
 
static AnimalInfofindAnimal (const string &strCommonName)
 Find the animal with the common name. More...
 
const string CliName ("Adopt-An-Animal")
 CLI name.
 
const string CliPrompt ("aaa> ")
 CLI prompt.
 

Variables

static char * Argv0
 the command
 
static OptsPgmInfo_T PgmInfo
 Program information. More...
 
static OptsInfo_T OptsInfo []
 Command line options information. More...
 
static const char * AsciiAardvark []
 ASCII ardvaark. More...
 
static const char * AsciiMandrill []
 ASCII Mandrill. More...
 
static const char * AsciiNumbat []
 ASCII numbat. More...
 
static const char * AsciiZebraSmall []
 ASCII small zebra. More...
 
static const char * AsciiZebra []
 ASCII zebra. More...
 
static const char * AsciiAnt []
 ASCII ant. More...
 
static const char * AsciiGrub []
 ASCII grub. More...
 
static const char * AsciiCarrot []
 ASCII carrot. More...
 
static const char * AsciiVitamins []
 ASCII bottle of vitamins. More...
 
static const char * AsciiWalking [][2]
 ASCII walk sequence. More...
 
const char * AnimalInfo::m_sCommonName
 command name
 
const char * AnimalInfo::m_sScientificName
 scientific name
 
const char ** AnimalInfo::m_sSelfie
 ASCII selfie.
 
string AnimalInfo::m_strGivenName
 adopted given name by owner
 
bool AnimalInfo::m_bIsAdopted
 is [not] adopted
 
Activity AnimalInfo::m_eActivity
 current activity
 
double AnimalInfo::m_fActStart
 activity start time
 
double AnimalInfo::m_fActDuration
 activitiy uninterrupted duration
 
const char * NoName = "No given name"
 'no name' name
 
AnimalInfo Animals []
 The animals. More...
 
size_t NumOfAnimals = arraysize(Animals)
 number of animals
 
Time ActivityTime
 activity time
 
CommandLine Cli (CliName, CliPrompt)
 the CLI
 
CmdDesc AppCmdExec::m_desc
 command description and syntax specification
 
CmdExec2Func AppCmdExec::m_fnExec
 command execution function
 
CmdDesc AppCmdExec3::m_desc
 command description and syntax specification
 
CmdExec3Func AppCmdExec3::m_fnExec
 command execution function
 
AppCmdExec Commands []
 The command descriptions.
 
const size_t NumOfCmds = arraysize(Commands)
 Number of commands.
 
static const char * CliTestCmdName = "clitest"
 
AppCmdExec3 Command3s []
 The command descriptions. More...
 
const size_t NumOfCmd3s = arraysize(Command3s)
 Number of commands.
 
int checkCmd (const CmdExtArg &argv0, int argc, const string strTgtName="")
 Check if command input is matched correctly with the command execution. More...
 
static int execAdopt (const CmdExtArgVec &argv)
 Execute 'adopt' command. More...
 
static int execSleep (const CmdExtArgVec &argv)
 Execute 'sleep' command. More...
 
static int execNameAnimal (const CmdExtArgVec &argv)
 Execute 'name' command. More...
 
static int execNamaste (const CmdExtArgVec &argv)
 Execute 'namaste' command. More...
 
static int execFeedAnimal (const CmdExtArgVec &argv)
 Execute 'feed' command. More...
 
static int execWalkAnimal (const CmdExtArgVec &argv)
 Execute 'walk' command. More...
 
static int execListAnimals (const CmdExtArgVec &argv)
 Execute 'list' command. More...
 
static int execGetPetsState (const CmdExtArgVec &argv)
 Execute 'get' command. More...
 
static int execReward (const CmdExtArgVec &argv)
 Execute 'reward' command. More...
 
static int execSave (const CmdExtArgVec &argv)
 Execute 'save' command. More...
 
static int findCommand (const std::string &strName)
 Find command by name. More...
 
static int execTAdd (CommandLine &cli, const CmdExtArgVec &argv)
 Execute clitest tadd subcommand. More...
 
static int execTBt (CommandLine &cli, const CmdExtArgVec &argv)
 Execute clitest tbt subcommand. More...
 
static int execTCompile (CommandLine &cli, const CmdExtArgVec &argv)
 Execute clitest tcompile subcommand. More...
 
static int execTDump (CommandLine &cli, const CmdExtArgVec &argv)
 Execute clitest tdump subcommand. More...
 
static int execTKbhit (CommandLine &cli, const CmdExtArgVec &argv)
 Execute clitest tkbhit subcommand. More...
 
static int execTPrint (CommandLine &cli, const CmdExtArgVec &argv)
 Execute clitest tprint subcommand. More...
 
static int execTPush (CommandLine &cli, const CmdExtArgVec &argv)
 Execute clitest tpush subcommand. More...
 
static int execTRemove (CommandLine &cli, const CmdExtArgVec &argv)
 Execute clitest tremove subcommand. More...
 
static int execCliTest (CommandLine &cli, const CmdExtArgVec &argv)
 Execute command-line interface methods command. More...
 
static void mainInit (int argc, char *argv[])
 Main initialization. More...
 
static void updateAnimals ()
 Update all animals' state data.
 
static int loadCommands (CommandLine &cli)
 Load commands into command line. More...
 
static int run (CommandLine &cli)
 Command line interface main loop. More...
 
int main (int argc, char *argv[])
 Main. More...
 
#define PERROR(_err)   cout << CliName << ": " << "Error: " << _err << endl
 Error and warning printing macros.
 
#define PWARN(_warn)   cout << CliName << ": " << _warn << endl
 
#define PCMDERROR(_cmd, _err)   cout << CliName << ": " << _cmd << ": " << "Error: " << _err << endl
 
#define PCMDWARN(_cmd, _warn)   cout << CliName << ": " << _cmd << ": " << _warn << endl
 

Detailed Description

Enumeration Type Documentation

enum Activity

Animal activities.

Enumerator
ActIdle 

idle, doing nothing

ActSleeping 

sleeping

ActEating 

eating

ActWalking 

walking

Definition at line 475 of file rnr_eg_cli.cxx.

476 {
477  ActIdle, ///< idle, doing nothing
478  ActSleeping, ///< sleeping
479  ActEating, ///< eating
480  ActWalking ///< walking
481 };
walking
Definition: rnr_eg_cli.cxx:480
idle, doing nothing
Definition: rnr_eg_cli.cxx:477
eating
Definition: rnr_eg_cli.cxx:479
sleeping
Definition: rnr_eg_cli.cxx:478

Function Documentation

int checkCmd ( const CmdExtArg argv0,
int  argc,
const string  strTgtName = "" 
)

Check if command input is matched correctly with the command execution.

Note
This should never happen if the command definitions are well defined and unambiguous.
Parameters
argv0Input argument 0. (Any extended argument actually works).
argcNumber of input arguments.
strTgtNameExpected target name of execution.
Returns
OK(0) on success, negative value on failure.

Definition at line 592 of file rnr_eg_cli.cxx.

References rnr::cmd::CommandLine::at(), Cli, rnr::cmd::CommandLine::numOfRequiredArgs(), PERROR, and rnr::cmd::CmdExtArg::uid().

Referenced by execAdopt(), execCliTest(), execFeedAnimal(), execGetPetsState(), execNameAnimal(), execReward(), execSave(), execSleep(), and execWalkAnimal().

593 {
594  const string &strDefName = Cli.at(argv0.uid()).getName();
595 
596  if( !strTgtName.empty() && (strTgtName != strDefName) )
597  {
598  PERROR("Command execution is for '" << strTgtName << "' "
599  << "not input command '" << strDefName << "'");
600  return RC_ERROR;
601  }
602  else if( argc < Cli.numOfRequiredArgs(argv0) )
603  {
604  PERROR("Command '" << strDefName << "' has missing arguments.");
605  return RC_ERROR;
606  }
607  else
608  {
609  return OK;
610  }
611 }
int uid() const
Get the argument&#39;s associated parsed command unique id.
Definition: CmdExtArg.h:158
const CmdDef & at(const int uid) const
Get the command definition with the unique id.
int numOfRequiredArgs(int uid, int iform) const
Get the total number of required arguments.
CommandLine Cli(CliName, CliPrompt)
the CLI
#define PERROR(_err)
Error and warning printing macros.
Definition: rnr_eg_cli.cxx:564
static int execAdopt ( const CmdExtArgVec argv)
static

Execute 'adopt' command.

Parameters
argvCommand line arguments.
Returns
OK(0) on success, negative value on failure.

Definition at line 625 of file rnr_eg_cli.cxx.

References checkCmd(), findAnimal(), and AnimalInfo::m_bIsAdopted.

626 {
627  static const char *cmdname = "adopt";
628 
629  size_t argc = argv.size();
630  size_t n;
631 
632  if( checkCmd(argv[0], argc, cmdname) != OK )
633  {
634  return RC_ERROR;
635  }
636 
637  n = 1; // skip argv0
638 
639  //
640  // Required arguments.
641  //
642  string animal = argv[n].s();
643 
644  AnimalInfo *p = findAnimal(animal);
645 
646  if( p == NULL )
647  {
648  PCMDERROR(cmdname, "Unknown animal '" << animal << "'.");
649  return RC_ERROR;
650  }
651 
652  else if( p->m_bIsAdopted )
653  {
654  PCMDWARN(cmdname, "The " << animal << " is already adopted.");
655  return RC_ERROR;
656  }
657 
658  else
659  {
660  p->m_bIsAdopted = true;
661  return OK;
662  }
663 }
bool m_bIsAdopted
is [not] adopted
Definition: rnr_eg_cli.cxx:495
int checkCmd(const CmdExtArg &argv0, int argc, const string strTgtName="")
Check if command input is matched correctly with the command execution.
Definition: rnr_eg_cli.cxx:592
static AnimalInfo * findAnimal(const string &strCommonName)
Find the animal with the common name.
Definition: rnr_eg_cli.cxx:536
static int execCliTest ( CommandLine cli,
const CmdExtArgVec argv 
)
static

Execute command-line interface methods command.

Parameters
cliCommand line interface.
argvCommand line arguments.
Returns
OK(0) on success, negative value on failure.

Definition at line 1760 of file rnr_eg_cli.cxx.

References checkCmd(), execTAdd(), execTBt(), execTCompile(), execTDump(), execTKbhit(), execTPrint(), execTPush(), execTRemove(), and rnr::cmd::CommandLine::popPrompt().

1761 {
1762  size_t nCmd = 0;
1763  int rc;
1764 
1765  if( checkCmd(argv[nCmd], argv.size()) != OK )
1766  {
1767  rc = RC_ERROR;
1768  }
1769 
1770  //
1771  // Add command to interface.
1772  //
1773  else if( argv[nCmd] == "tadd" )
1774  {
1775  rc = execTAdd(cli, argv);
1776  }
1777 
1778  //
1779  // Backtrace last execution sequence.
1780  //
1781  else if( argv[nCmd] == "tbt" )
1782  {
1783  rc = execTBt(cli, argv);
1784  }
1785 
1786  //
1787  // (Re)compile interface.
1788  //
1789  else if( argv[nCmd] == "tcompile" )
1790  {
1791  rc = execTCompile(cli, argv);
1792  }
1793 
1794  //
1795  // Dump interface component to output stream.
1796  //
1797  else if( argv[nCmd] == "tdump" )
1798  {
1799  rc = execTDump(cli, argv);
1800  }
1801 
1802  //
1803  // Check for keyboard hits.
1804  //
1805  else if( argv[nCmd] == "tkbhit" )
1806  {
1807  rc = execTKbhit(cli, argv);
1808  }
1809 
1810  //
1811  // Print interface attribute.
1812  //
1813  else if( argv[nCmd] == "tprint" )
1814  {
1815  rc = execTPrint(cli, argv);
1816  }
1817 
1818  //
1819  // Push new prompt.
1820  //
1821  else if( argv[nCmd] == "tpush" )
1822  {
1823  rc = execTPush(cli, argv);
1824  }
1825 
1826  //
1827  // Pop current prompt, restore previous.
1828  //
1829  else if( argv[nCmd] == "tpop" )
1830  {
1831  cli.popPrompt();
1832  rc = OK;
1833  }
1834 
1835  //
1836  // Remove command from interface.
1837  //
1838  else if( argv[nCmd] == "tremove" )
1839  {
1840  rc = execTRemove(cli, argv);
1841  }
1842 
1843  else
1844  {
1845  PCMDERROR(CliTestCmdName,
1846  "Do not know how to execute '" << argv[nCmd] << "'.");
1847  rc = RC_ERROR;
1848  }
1849 
1850  return rc;
1851 }
void popPrompt()
Pop prompt string from stack of prompts.
int checkCmd(const CmdExtArg &argv0, int argc, const string strTgtName="")
Check if command input is matched correctly with the command execution.
Definition: rnr_eg_cli.cxx:592
static int execTRemove(CommandLine &cli, const CmdExtArgVec &argv)
Execute clitest tremove subcommand.
static int execTDump(CommandLine &cli, const CmdExtArgVec &argv)
Execute clitest tdump subcommand.
static int execTKbhit(CommandLine &cli, const CmdExtArgVec &argv)
Execute clitest tkbhit subcommand.
static int execTBt(CommandLine &cli, const CmdExtArgVec &argv)
Execute clitest tbt subcommand.
static int execTPrint(CommandLine &cli, const CmdExtArgVec &argv)
Execute clitest tprint subcommand.
static int execTAdd(CommandLine &cli, const CmdExtArgVec &argv)
Execute clitest tadd subcommand.
static int execTCompile(CommandLine &cli, const CmdExtArgVec &argv)
Execute clitest tcompile subcommand.
static int execTPush(CommandLine &cli, const CmdExtArgVec &argv)
Execute clitest tpush subcommand.
static int execFeedAnimal ( const CmdExtArgVec argv)
static

Execute 'feed' command.

Parameters
argvCommand line arguments.
Returns
OK(0) on success, negative value on failure.

Definition at line 801 of file rnr_eg_cli.cxx.

References ActEating, AsciiAnt, AsciiCarrot, AsciiGrub, AsciiVitamins, checkCmd(), findAnimal(), AnimalInfo::m_bIsAdopted, AnimalInfo::m_eActivity, AnimalInfo::m_fActDuration, AnimalInfo::m_fActStart, AnimalInfo::m_sCommonName, AnimalInfo::m_sSelfie, rnr::chronos::Time::now(), and showAsciiArt().

802 {
803  static const char *cmdname = "feed";
804 
805  size_t argc = argv.size();
806  size_t n;
807 
808  if( checkCmd(argv[0], argc, cmdname) != OK )
809  {
810  return RC_ERROR;
811  }
812 
813  n = 1; // skip argv0
814 
815  //
816  // Required arguments.
817  //
818 
819  string animal = argv[n].s();
820 
821  AnimalInfo *p = findAnimal(animal);
822 
823  if( p == NULL )
824  {
825  PCMDERROR(cmdname, "Unknown animal '" << animal << "'.");
826  return RC_ERROR;
827  }
828 
829  else if( !p->m_bIsAdopted )
830  {
831  PCMDWARN(cmdname, "The " << animal << " is not yours to feed.");
832  return OK;
833  }
834 
835  ++n; // next argument
836 
837  const char **food = NULL;
838  double fSecs;
839 
840  if( argv[n] == "ants" )
841  {
842  food = AsciiAnt;
843  fSecs = 30.0;
844  }
845  else if( argv[n] == "carrots" )
846  {
847  food = AsciiCarrot;
848  fSecs = 10.0;
849  }
850  else if( argv[n] == "grubs" )
851  {
852  food = AsciiGrub;
853  fSecs = 20.0;
854  }
855  else if( argv[n] == "vitamins" )
856  {
857  food = AsciiVitamins;
858  fSecs = 5.0;
859  }
860  else
861  {
862  PCMDERROR(cmdname, "Unknown food '" << argv[n] << "'.");
863  return RC_ERROR;
864  }
865 
866  if( (p->m_sCommonName == "numbat") || (p->m_sCommonName == "zebra") )
867  {
868  showAsciiArt(cout, food, p->m_sSelfie);
869  }
870  else
871  {
872  showAsciiArt(cout, p->m_sSelfie, food);
873  }
874  cout << "Delicious! Give me " << fSecs << " seconds to enjoy." << endl;
875 
876  p->m_eActivity = ActEating;
878  p->m_fActDuration = fSecs;
879 
880  return OK;
881 }
const char ** m_sSelfie
ASCII selfie.
Definition: rnr_eg_cli.cxx:491
bool m_bIsAdopted
is [not] adopted
Definition: rnr_eg_cli.cxx:495
static void showAsciiArt(ostream &os, const char *art0[], const char *art1[], const char *art2[]=NULL, const char *art3[]=NULL)
Write a set of ASCII art to cout.
Definition: rnr_eg_cli.cxx:387
int checkCmd(const CmdExtArg &argv0, int argc, const string strTgtName="")
Check if command input is matched correctly with the command execution.
Definition: rnr_eg_cli.cxx:592
Time ActivityTime
activity time
Definition: rnr_eg_cli.cxx:548
const char * m_sCommonName
command name
Definition: rnr_eg_cli.cxx:489
static const char * AsciiAnt[]
ASCII ant.
Definition: rnr_eg_cli.cxx:260
double m_fActDuration
activitiy uninterrupted duration
Definition: rnr_eg_cli.cxx:498
double now()
Get the current time, indentified by CLOCK_REALTIME, since the last Epoch.
Definition: Time.cxx:208
Activity m_eActivity
current activity
Definition: rnr_eg_cli.cxx:496
static const char * AsciiGrub[]
ASCII grub.
Definition: rnr_eg_cli.cxx:273
static const char * AsciiCarrot[]
ASCII carrot.
Definition: rnr_eg_cli.cxx:298
static const char * AsciiVitamins[]
ASCII bottle of vitamins.
Definition: rnr_eg_cli.cxx:325
static AnimalInfo * findAnimal(const string &strCommonName)
Find the animal with the common name.
Definition: rnr_eg_cli.cxx:536
double m_fActStart
activity start time
Definition: rnr_eg_cli.cxx:497
eating
Definition: rnr_eg_cli.cxx:479
static int execGetPetsState ( const CmdExtArgVec argv)
static

Execute 'get' command.

Parameters
argvCommand line arguments.
Returns
OK(0) on success, negative value on failure.

Definition at line 989 of file rnr_eg_cli.cxx.

References ActEating, ActIdle, ActSleeping, ActWalking, checkCmd(), findAnimal(), AnimalInfo::m_bIsAdopted, AnimalInfo::m_eActivity, AnimalInfo::m_sCommonName, AnimalInfo::m_sScientificName, AnimalInfo::m_sSelfie, and AnimalInfo::m_strGivenName.

990 {
991  static const char *cmdname = "get";
992 
993  size_t argc = argv.size();
994  size_t n;
995 
996  // optional arguments defaults
997  long eState = 0;
998 
999  if( checkCmd(argv[0], argc, cmdname) != OK )
1000  {
1001  return RC_ERROR;
1002  }
1003 
1004  n = 1; // skip argv0
1005 
1006  //
1007  // Required arguments.
1008  //
1009 
1010  string animal = argv[n].s();
1011 
1012  AnimalInfo *p = findAnimal(animal);
1013 
1014  if( p == NULL )
1015  {
1016  PCMDERROR(cmdname, "Unknown animal '" << animal << "'.");
1017  return RC_ERROR;
1018  }
1019 
1020  string strAdoptState;
1021  string strActivity;
1022 
1023  strAdoptState = p->m_bIsAdopted? "Adopted": "Not adopted";
1024 
1025  switch( p->m_eActivity )
1026  {
1027  case ActIdle:
1028  strActivity = "Just chill'n dude.";
1029  break;
1030  case ActSleeping:
1031  strActivity = "Sleeping, don't wake me.";
1032  break;
1033  case ActEating:
1034  strActivity = "Num, num, num.";
1035  break;
1036  case ActWalking:
1037  strActivity = "And I'm walking, and walking, walking...";
1038  break;
1039  default:
1040  strActivity = "I dunno what I'm doing.";
1041  break;
1042  }
1043 
1044  ++n; // next argument
1045 
1046  //
1047  // Optional arguments.
1048  //
1049 
1050  //
1051  // Argument 2
1052  //
1053  // State: all | adoption | given | activity | species | selfie
1054  //
1055  if( n < argc )
1056  {
1057  // convert to enum index
1058  eState = argv[n].e();
1059  }
1060 
1061  switch( eState )
1062  {
1063  case 1: // adoption
1064  cout << strAdoptState << endl;
1065  break;
1066  case 2: // given
1067  cout << p->m_strGivenName << endl;
1068  break;
1069  case 3: // activity
1070  cout << strActivity << endl;
1071  break;
1072  case 4: // species
1073  cout << p->m_sCommonName << " (" << p->m_sScientificName << ")" << endl;
1074  break;
1075  case 5: // selfie
1076  cout << p->m_sSelfie;
1077  break;
1078  case 0: // all
1079  cout << p->m_sSelfie;
1080  cout << p->m_sCommonName << " (" << p->m_sScientificName << ")" << endl;
1081  cout << strAdoptState << endl;
1082  cout << p->m_strGivenName << endl;
1083  cout << strActivity << endl;
1084  break;
1085  default:
1086  PCMDERROR(cmdname, "Unknown state '" << argv[n].arg() << "'.");
1087  return RC_ERROR;
1088  break;
1089  }
1090 
1091  return OK;
1092 }
const char ** m_sSelfie
ASCII selfie.
Definition: rnr_eg_cli.cxx:491
bool m_bIsAdopted
is [not] adopted
Definition: rnr_eg_cli.cxx:495
int checkCmd(const CmdExtArg &argv0, int argc, const string strTgtName="")
Check if command input is matched correctly with the command execution.
Definition: rnr_eg_cli.cxx:592
const char * m_sCommonName
command name
Definition: rnr_eg_cli.cxx:489
const char * m_sScientificName
scientific name
Definition: rnr_eg_cli.cxx:490
walking
Definition: rnr_eg_cli.cxx:480
Activity m_eActivity
current activity
Definition: rnr_eg_cli.cxx:496
static AnimalInfo * findAnimal(const string &strCommonName)
Find the animal with the common name.
Definition: rnr_eg_cli.cxx:536
idle, doing nothing
Definition: rnr_eg_cli.cxx:477
eating
Definition: rnr_eg_cli.cxx:479
sleeping
Definition: rnr_eg_cli.cxx:478
string m_strGivenName
adopted given name by owner
Definition: rnr_eg_cli.cxx:494
static int execListAnimals ( const CmdExtArgVec argv)
static

Execute 'list' command.

Parameters
argvCommand line arguments.
Returns
OK(0) on success, negative value on failure.

Definition at line 968 of file rnr_eg_cli.cxx.

References AnimalInfo::m_sCommonName, and NumOfAnimals.

969 {
970  static const char *cmdname = "list";
971 
972  for(size_t i = 0; i < NumOfAnimals; ++i)
973  {
974  cout << Animals[i].m_sCommonName << " ";
975  }
976 
977  cout << endl;
978 
979  return OK;
980 }
AnimalInfo Animals[]
The animals.
Definition: rnr_eg_cli.cxx:506
const char * m_sCommonName
command name
Definition: rnr_eg_cli.cxx:489
size_t NumOfAnimals
number of animals
Definition: rnr_eg_cli.cxx:525
static int execNamaste ( const CmdExtArgVec argv)
static

Execute 'namaste' command.

Parameters
argvCommand line arguments.
Returns
OK(0) on success, negative value on failure.

Definition at line 785 of file rnr_eg_cli.cxx.

786 {
787  static const char *cmdname = "namaste";
788 
789  cout << "Aye. Baaaa to the divine Ewe." << endl;
790 
791  return OK;
792 }
static int execNameAnimal ( const CmdExtArgVec argv)
static

Execute 'name' command.

Parameters
argvCommand line arguments.
Returns
OK(0) on success, negative value on failure.

Definition at line 737 of file rnr_eg_cli.cxx.

References checkCmd(), findAnimal(), AnimalInfo::m_bIsAdopted, and AnimalInfo::m_strGivenName.

738 {
739  static const char *cmdname = "name";
740 
741  size_t argc = argv.size();
742  size_t n;
743 
744  if( checkCmd(argv[0], argc, cmdname) != OK )
745  {
746  return RC_ERROR;
747  }
748 
749  n = 1; // skip argv0
750 
751  //
752  // Required arguments.
753  //
754 
755  string animal = argv[n].s();
756 
757  AnimalInfo *p = findAnimal(animal);
758 
759  if( p == NULL )
760  {
761  PCMDERROR(cmdname, "Unknown animal '" << animal << "'.");
762  return RC_ERROR;
763  }
764 
765  else if( !p->m_bIsAdopted )
766  {
767  PCMDWARN(cmdname, "You have not adopted the " << animal << ".");
768  return OK;
769  }
770 
771  ++n; // next argument
772 
773  p->m_strGivenName = argv[n].s();
774 
775  return OK;
776 }
bool m_bIsAdopted
is [not] adopted
Definition: rnr_eg_cli.cxx:495
int checkCmd(const CmdExtArg &argv0, int argc, const string strTgtName="")
Check if command input is matched correctly with the command execution.
Definition: rnr_eg_cli.cxx:592
static AnimalInfo * findAnimal(const string &strCommonName)
Find the animal with the common name.
Definition: rnr_eg_cli.cxx:536
string m_strGivenName
adopted given name by owner
Definition: rnr_eg_cli.cxx:494
static int execReward ( const CmdExtArgVec argv)
static

Execute 'reward' command.

Parameters
argvCommand line arguments.
Returns
OK(0) on success, negative value on failure.

Definition at line 1101 of file rnr_eg_cli.cxx.

References checkCmd(), findAnimal(), AnimalInfo::m_bIsAdopted, and AnimalInfo::m_sCommonName.

1102 {
1103  static const char *cmdname = "reward";
1104 
1105  size_t argc = argv.size();
1106  size_t n;
1107 
1108  if( checkCmd(argv[0], argc, cmdname) != OK )
1109  {
1110  return RC_ERROR;
1111  }
1112 
1113  n = 1; // skip argv0
1114 
1115  //
1116  // Required Arguments
1117  //
1118 
1119  string animal = argv[n].s();
1120 
1121  AnimalInfo *p = findAnimal(animal);
1122 
1123  if( p == NULL )
1124  {
1125  PCMDERROR(cmdname, "Unknown animal '" << animal << "'.");
1126  return RC_ERROR;
1127  }
1128 
1129  else if( !p->m_bIsAdopted )
1130  {
1131  PCMDWARN(cmdname, "The " << animal << " is not yours to reward.");
1132  return OK;
1133  }
1134 
1135  ++n; // next argument
1136 
1137  bool isGood = argv[n].b();
1138 
1139  cout << "Your " << p->m_sCommonName << " ";
1140 
1141  if( p->m_sCommonName == "aardvark" )
1142  {
1143  if( isGood )
1144  {
1145  cout << "is a good girl." << endl;
1146  cout << "She ate all of those pesky ants pilfering food in the kitchen."
1147  << endl;
1148  }
1149  else
1150  {
1151  cout << "was a bad girl." << endl;
1152  cout << "She dug under the foundation again." << endl;
1153  }
1154  }
1155 
1156  else if( p->m_sCommonName == "mandrill" )
1157  {
1158  if( isGood )
1159  {
1160  cout << "is a good boy." << endl;
1161  cout << "You are now well groomed and free of fleas." << endl;
1162  }
1163  else
1164  {
1165  cout << "is a mischieveus boy." << endl;
1166  cout << "Why does he love reality tv so much?" << endl;
1167  }
1168  }
1169 
1170  else if( p->m_sCommonName == "numbat" )
1171  {
1172  if( isGood )
1173  {
1174  cout << "is a good lad." << endl;
1175  cout << "Those termites won't be invading any time soon." << endl;
1176  }
1177  else
1178  {
1179  cout << "is a snarky lad." << endl;
1180  cout << "He needs to quit putting his long, narrow tongue where it "
1181  << "doesn't belong!" << endl;
1182  }
1183  }
1184 
1185  else if( p->m_sCommonName == "zebra" )
1186  {
1187  if( isGood )
1188  {
1189  cout << "is a good lass." << endl;
1190  cout << "You rode her all the way to town this time without being "
1191  << "thrown." << endl;
1192  }
1193  else
1194  {
1195  cout << "is a very naughty lass." << endl;
1196  cout << "She ate all of the apples ripening on the tree." << endl;
1197  }
1198  }
1199 
1200  else
1201  {
1202  cout << " is " << (isGood? "good": "not good") << "." << endl;
1203  }
1204 
1205  return OK;
1206 }
bool m_bIsAdopted
is [not] adopted
Definition: rnr_eg_cli.cxx:495
int checkCmd(const CmdExtArg &argv0, int argc, const string strTgtName="")
Check if command input is matched correctly with the command execution.
Definition: rnr_eg_cli.cxx:592
const char * m_sCommonName
command name
Definition: rnr_eg_cli.cxx:489
static AnimalInfo * findAnimal(const string &strCommonName)
Find the animal with the common name.
Definition: rnr_eg_cli.cxx:536
static int execSave ( const CmdExtArgVec argv)
static

Execute 'save' command.

Parameters
argvCommand line arguments.
Returns
OK(0) on success, negative value on failure.

Definition at line 1215 of file rnr_eg_cli.cxx.

References checkCmd(), findAnimal(), AnimalInfo::m_sCommonName, and AnimalInfo::m_sSelfie.

1216 {
1217  static const char *cmdname = "save";
1218 
1219  size_t argc = argv.size();
1220  size_t n;
1221 
1222  if( checkCmd(argv[0], argc, cmdname) != OK )
1223  {
1224  return RC_ERROR;
1225  }
1226 
1227  n = 1; // skip argv0
1228 
1229  //
1230  // Required arguments.
1231  //
1232 
1233  string animal = argv[n].s();
1234 
1235  AnimalInfo *p = findAnimal(animal);
1236 
1237  if( p == NULL )
1238  {
1239  PCMDERROR(cmdname, "Unknown animal '" << animal << "'.");
1240  return RC_ERROR;
1241  }
1242 
1243  ++n; // next argument
1244 
1245  string filename = argv[n].s();
1246  bool bSave = false;
1247 
1248  if( access(filename.c_str(), F_OK) == 0 )
1249  {
1250  string line;
1251  string ans;
1252 
1253  cout << "Overwrite file '" << argv[n] << "'? [ny] ";
1254 
1255  if( std::getline(cin, line) )
1256  {
1257  istringstream iss(line);
1258  if( (iss >> ans) && (ans == "y") )
1259  {
1260  bSave = true;
1261  }
1262  }
1263  }
1264  else
1265  {
1266  bSave = true;
1267  }
1268 
1269  if( bSave )
1270  {
1271  ofstream selfie;
1272 
1273  selfie.open(filename.c_str());
1274 
1275  if( selfie.is_open() )
1276  {
1277  selfie << p->m_sSelfie;
1278  selfie.close();
1279  cout << "A " << p->m_sCommonName << " selfie writen to file '"
1280  << argv[n] << "'." << endl;
1281  }
1282  else
1283  {
1284  PCMDERROR(cmdname, "Failed to open file '" << argv[n] << "'.");
1285  }
1286  }
1287 
1288  return OK;
1289 }
const char ** m_sSelfie
ASCII selfie.
Definition: rnr_eg_cli.cxx:491
int checkCmd(const CmdExtArg &argv0, int argc, const string strTgtName="")
Check if command input is matched correctly with the command execution.
Definition: rnr_eg_cli.cxx:592
const char * m_sCommonName
command name
Definition: rnr_eg_cli.cxx:489
static AnimalInfo * findAnimal(const string &strCommonName)
Find the animal with the common name.
Definition: rnr_eg_cli.cxx:536
static int execSleep ( const CmdExtArgVec argv)
static

Execute 'sleep' command.

Parameters
argvCommand line arguments.
Returns
OK(0) on success, negative value on failure.

Definition at line 672 of file rnr_eg_cli.cxx.

References ActSleeping, checkCmd(), findAnimal(), AnimalInfo::m_bIsAdopted, AnimalInfo::m_eActivity, AnimalInfo::m_fActDuration, AnimalInfo::m_fActStart, AnimalInfo::m_sCommonName, and rnr::chronos::Time::now().

673 {
674  static const char *cmdname = "sleep";
675 
676  size_t argc = argv.size();
677  size_t n;
678 
679  // optional defaults
680  long secs = 10;
681 
682  if( checkCmd(argv[0], argc, cmdname) != OK )
683  {
684  return RC_ERROR;
685  }
686 
687  n = 1; // skip argv0
688 
689  //
690  // Required arguments.
691  //
692 
693  string animal = argv[n].s();
694 
695  AnimalInfo *p = findAnimal(animal);
696 
697  if( p == NULL )
698  {
699  PCMDERROR(cmdname, "Unknown animal '" << animal << "'.");
700  return RC_ERROR;
701  }
702 
703  else if( !p->m_bIsAdopted )
704  {
705  PCMDWARN(cmdname, "The " << animal << " is not yours to rock to sleep.");
706  return OK;
707  }
708 
709  ++n;
710 
711  //
712  // Optional arguments
713  //
714 
715  if( n < argc )
716  {
717  secs = argv[n].i();
718  }
719 
722  p->m_fActDuration = (double)secs;
723 
724  cout << "The " << p->m_sCommonName << " is sleeping for " << secs
725  << " seconds." << endl;
726 
727  return OK;
728 }
bool m_bIsAdopted
is [not] adopted
Definition: rnr_eg_cli.cxx:495
int checkCmd(const CmdExtArg &argv0, int argc, const string strTgtName="")
Check if command input is matched correctly with the command execution.
Definition: rnr_eg_cli.cxx:592
Time ActivityTime
activity time
Definition: rnr_eg_cli.cxx:548
const char * m_sCommonName
command name
Definition: rnr_eg_cli.cxx:489
double m_fActDuration
activitiy uninterrupted duration
Definition: rnr_eg_cli.cxx:498
double now()
Get the current time, indentified by CLOCK_REALTIME, since the last Epoch.
Definition: Time.cxx:208
Activity m_eActivity
current activity
Definition: rnr_eg_cli.cxx:496
static AnimalInfo * findAnimal(const string &strCommonName)
Find the animal with the common name.
Definition: rnr_eg_cli.cxx:536
double m_fActStart
activity start time
Definition: rnr_eg_cli.cxx:497
sleeping
Definition: rnr_eg_cli.cxx:478
static int execTAdd ( CommandLine cli,
const CmdExtArgVec argv 
)
static

Execute clitest tadd subcommand.

Add a new command to interface.

Parameters
cliCommand line interface.
argvCommand line arguments.
Returns
OK(0) on success, negative value on failure.

Definition at line 1449 of file rnr_eg_cli.cxx.

References rnr::cmd::CommandLine::addCommand(), rnr::cmd::CommandLine::compile(), findCommand(), rnr::cmd::CommandLine::getErrorStr(), rnr::cmd::CommandLine::hasCmd(), AppCmdExec::m_desc, AppCmdExec::m_fnExec, and rnr::cmd::NoUid.

Referenced by execCliTest().

1450 {
1451  size_t nCmd = 0; // index to command name
1452  size_t nArg = 1; // index to current working argument
1453  int i;
1454  int nUid;
1455  int rc = RC_ERROR;
1456 
1457  if( nArg >= argv.size() )
1458  {
1459  PCMDERROR(CliTestCmdName, argv[nCmd] << ": No <cmd> specified.");
1460  }
1461  else if( (i = findCommand(argv[nArg].s())) < 0 )
1462  {
1463  PCMDERROR(CliTestCmdName, argv[nCmd]
1464  << ": Unknown <cmd> '" << argv[nArg] << "'.");
1465  }
1466  else if( cli.hasCmd(argv[nArg].s()) )
1467  {
1468  PCMDERROR(CliTestCmdName, argv[nCmd]
1469  << ": The <cmd> '" << argv[nArg]
1470  << "' is already present in the interface.");
1471  }
1472  else if( (nUid = cli.addCommand(Commands[i].m_desc, Commands[i].m_fnExec)) ==
1473  NoUid )
1474  {
1475  PCMDERROR(CliTestCmdName, argv[nCmd]
1476  << ": Failed to add <cmd> '" << argv[nArg] << "': "
1477  << cli.getErrorStr() << ".");
1478  }
1479  else if( (rc = cli.compile()) != OK )
1480  {
1481  PCMDERROR(CliTestCmdName, argv[nCmd]
1482  << ": Failed to (re)compile interface: "
1483  << cli.getErrorStr() << ".");
1484  }
1485  else
1486  {
1487  cout << "Command '" << argv[nArg] << ", uid(" << nUid << ") added."
1488  << endl;
1489  rc = OK;
1490  }
1491 
1492  return rc;
1493 }
AppCmdExec Commands[]
The command descriptions.
CmdExec2Func m_fnExec
command execution function
virtual int compile()
Compile all added commands.
const int NoUid
Special values.
Definition: CmdCore.h:116
virtual int addCommand(const CmdDesc &desc)
Add a command to the command line interface.
bool hasCmd(const int uid) const
Test if command exists.
static int findCommand(const std::string &strName)
Find command by name.
const std::string & getErrorStr() const
Get the most recent error.
CmdDesc m_desc
command description and syntax specification
static int execTBt ( CommandLine cli,
const CmdExtArgVec argv 
)
static

Execute clitest tbt subcommand.

Print interface attribute.

Parameters
cliCommand line interface.
argvCommand line arguments.
Returns
OK(0) on success, negative value on failure.

Definition at line 1505 of file rnr_eg_cli.cxx.

References rnr::cmd::CommandLine::backtrace().

Referenced by execCliTest().

1506 {
1507  cli.backtrace(cout, true);
1508 
1509  return OK;
1510 }
std::ostream & backtrace(std::ostream &os, const bool bAll=false) const
Insert trace and error log backtrace into output stream.
static int execTCompile ( CommandLine cli,
const CmdExtArgVec argv 
)
static

Execute clitest tcompile subcommand.

(Re)compile the interface.

Parameters
cliCommand line interface.
argvCommand line arguments.
Returns
OK(0) on success, negative value on failure.

Definition at line 1522 of file rnr_eg_cli.cxx.

References rnr::cmd::CommandLine::compile(), rnr::cmd::CommandLine::getErrorStr(), and rnr::cmd::CommandLine::numOfCmds().

Referenced by execCliTest().

1523 {
1524  size_t nCmd = 0; // index to command name
1525  int rc;
1526 
1527  if( (rc = cli.compile()) != OK )
1528  {
1529  PCMDERROR(CliTestCmdName, argv[nCmd]
1530  << ": Failed to (re)compile interface: "
1531  << cli.getErrorStr() << ".");
1532  rc = RC_ERROR;
1533  }
1534  else
1535  {
1536  cout << "Compiled " << cli.numOfCmds() << " commands." << endl;
1537  rc = OK;
1538  }
1539 
1540  return rc;
1541 }
virtual int compile()
Compile all added commands.
int numOfCmds() const
Get the total number of added commands.
Definition: CommandLine.h:1108
const std::string & getErrorStr() const
Get the most recent error.
static int execTDump ( CommandLine cli,
const CmdExtArgVec argv 
)
static

Execute clitest tdump subcommand.

Dump specified command or full command-line interface.

Parameters
cliCommand line interface.
argvCommand line arguments.
Returns
OK(0) on success, negative value on failure.

Definition at line 1553 of file rnr_eg_cli.cxx.

References rnr::cmd::CommandLine::at(), and rnr::cmd::NoUid.

Referenced by execCliTest().

1554 {
1555  size_t nCmd = 0; // index to command name
1556  size_t nArg = 1; // index to current working argument
1557  int rc;
1558 
1559  // dump command definition
1560  if( nArg < argv.size() )
1561  {
1562  if( cli.at(argv[nArg].s()).getUid() == NoUid )
1563  {
1564  PCMDERROR(CliTestCmdName, argv[nCmd]
1565  << ": The <cmd> '" << argv[nArg]
1566  << "' is not present in the interface.");
1567  rc = RC_ERROR;
1568  }
1569  else
1570  {
1571  cout << cli.at(argv[nArg].s()) << endl; // dump command
1572  rc = OK;
1573  }
1574  }
1575 
1576  // dump full interface
1577  else
1578  {
1579  cout << cli << endl;
1580  rc = OK;
1581  }
1582 
1583  return rc;
1584 }
const int NoUid
Special values.
Definition: CmdCore.h:116
const CmdDef & at(const int uid) const
Get the command definition with the unique id.
static int execTKbhit ( CommandLine cli,
const CmdExtArgVec argv 
)
static

Execute clitest tkbhit subcommand.

Loop to check non-blocking input.

Parameters
cliCommand line interface.
argvCommand line arguments.
Returns
OK(0) on success, negative value on failure.

Definition at line 1596 of file rnr_eg_cli.cxx.

Referenced by execCliTest().

1597 {
1598  int c = 0;
1599 
1600  cout << "Press any key, 's' to stop test." << endl;
1601 
1602  while( c != 's' )
1603  {
1604  if( cli.kbhit() )
1605  {
1606  c = getchar();
1607  cout << " \r" << std::flush;
1608  switch( c )
1609  {
1610  case '\n':
1611  cout << "\\n";
1612  break;
1613  case '\t':
1614  cout << "\\t";
1615  break;
1616  default:
1617  cout << (char)c;
1618  break;
1619  }
1620  cout << "\r" << std::flush;
1621  }
1622  }
1623 
1624  cout << endl;
1625 
1626  return OK;
1627 }
static int execTPrint ( CommandLine cli,
const CmdExtArgVec argv 
)
static

Execute clitest tprint subcommand.

Print interface attribute.

Parameters
cliCommand line interface.
argvCommand line arguments.
Returns
OK(0) on success, negative value on failure.

Definition at line 1639 of file rnr_eg_cli.cxx.

References rnr::cmd::CommandLine::getErrorStr(), rnr::cmd::CommandLine::getName(), rnr::cmd::CommandLine::getPrompt(), and rnr::cmd::CommandLine::numOfCmds().

Referenced by execCliTest().

1640 {
1641  size_t nCmd = 0; // index to command name
1642  size_t nArg = 1; // index to current working argument
1643  int rc = OK;
1644 
1645  if( nArg >= argv.size() )
1646  {
1647  PCMDERROR(CliTestCmdName, argv[nCmd] << ": No <attr> specified.");
1648  rc = RC_ERROR;
1649  }
1650  else if( argv[nArg] == "name" ) // interface name
1651  {
1652  cout << cli.getName() << endl;
1653  }
1654  else if( argv[nArg] == "prompt" ) // current prompt string
1655  {
1656  cout << cli.getPrompt() << endl;
1657  }
1658  else if( argv[nArg] == "numcmds" ) // number of added commands
1659  {
1660  cout << cli.numOfCmds() << endl;
1661  }
1662  else if( argv[nArg] == "errstr" ) // last error
1663  {
1664  cout << cli.getErrorStr() << endl;
1665  }
1666  else
1667  {
1668  PCMDERROR(CliTestCmdName, argv[nCmd]
1669  << ": Unknown <attr> '" << argv[nArg] << "'.");
1670  rc = RC_ERROR;
1671  }
1672 
1673  return rc;
1674 }
int numOfCmds() const
Get the total number of added commands.
Definition: CommandLine.h:1108
const std::string & getPrompt() const
Get the current prompt string.
const std::string & getName() const
Get command line interface&#39;s name.
Definition: CommandLine.h:1040
const std::string & getErrorStr() const
Get the most recent error.
static int execTPush ( CommandLine cli,
const CmdExtArgVec argv 
)
static

Execute clitest tpush subcommand.

Push new prompt.

Parameters
cliCommand line interface.
argvCommand line arguments.
Returns
OK(0) on success, negative value on failure.

Definition at line 1686 of file rnr_eg_cli.cxx.

References rnr::cmd::CommandLine::pushPrompt().

Referenced by execCliTest().

1687 {
1688  size_t nCmd = 0; // index to command name
1689  size_t nArg = 1; // index to current working argument
1690  int rc;
1691 
1692  // push prompt string
1693  if( nArg < argv.size() )
1694  {
1695  cli.pushPrompt(argv[nArg].s());
1696  rc = OK;
1697  }
1698  else
1699  {
1700  PCMDERROR(CliTestCmdName, argv[nCmd] << ": No <prompt> specified.");
1701  rc = RC_ERROR;
1702  }
1703 
1704  return rc;
1705 }
void pushPrompt(const std::string &strPrompt)
Push prompt string onto stack of prompts.
static int execTRemove ( CommandLine cli,
const CmdExtArgVec argv 
)
static

Execute clitest tremove subcommand.

Remove command from the interface.

Parameters
cliCommand line interface.
argvCommand line arguments.
Returns
OK(0) on success, negative value on failure.

Definition at line 1717 of file rnr_eg_cli.cxx.

References rnr::cmd::CommandLine::at(), rnr::cmd::CommandLine::getErrorStr(), rnr::cmd::NoUid, and rnr::cmd::CommandLine::removeCommand().

Referenced by execCliTest().

1718 {
1719  size_t nCmd = 0; // index to command name
1720  size_t nArg = 1; // index to current working argument
1721  int nUid;
1722  int rc;
1723 
1724  rc = RC_ERROR;
1725 
1726  if( nArg >= argv.size() )
1727  {
1728  PCMDERROR(CliTestCmdName, argv[nCmd] << ": No <cmd> specified.");
1729  }
1730  else if( (nUid = cli.at(argv[nArg].s()).getUid()) == NoUid )
1731  {
1732  PCMDERROR(CliTestCmdName, argv[nCmd]
1733  << ": The <cmd> '" << argv[nArg]
1734  << "' is not present in the interface.");
1735  }
1736  else if( (rc = cli.removeCommand(argv[nArg].s())) != OK )
1737  {
1738  PCMDERROR(CliTestCmdName, argv[nCmd]
1739  << ": Failed to remove <cmd> '" << argv[nArg] << "': "
1740  << cli.getErrorStr() << ".");
1741  }
1742  else
1743  {
1744  cout << "Command '" << argv[nArg] << "', uid(" << nUid << ") removed."
1745  << endl;
1746  rc = OK;
1747  }
1748 
1749  return rc;
1750 }
virtual int removeCommand(const int uid)
Remove command from command line interface.
const int NoUid
Special values.
Definition: CmdCore.h:116
const CmdDef & at(const int uid) const
Get the command definition with the unique id.
const std::string & getErrorStr() const
Get the most recent error.
static int execWalkAnimal ( const CmdExtArgVec argv)
static

Execute 'walk' command.

Parameters
argvCommand line arguments.
Returns
OK(0) on success, negative value on failure.

Definition at line 890 of file rnr_eg_cli.cxx.

References ActWalking, AsciiWalking, checkCmd(), findAnimal(), AnimalInfo::m_bIsAdopted, AnimalInfo::m_eActivity, AnimalInfo::m_fActDuration, AnimalInfo::m_fActStart, AnimalInfo::m_sCommonName, and rnr::chronos::Time::now().

891 {
892  static const char *cmdname = "walk";
893 
894  size_t argc = argv.size();
895  size_t n;
896 
897  if( checkCmd(argv[0], argc, cmdname) != OK )
898  {
899  return RC_ERROR;
900  }
901 
902  n = 1; // skip argv0
903 
904  //
905  // Required arguments.
906  //
907 
908  string animal = argv[n].s();
909 
910  AnimalInfo *p = findAnimal(animal);
911 
912  if( p == NULL )
913  {
914  PCMDERROR(cmdname, "Unknown animal '" << animal << "'.");
915  return RC_ERROR;
916  }
917 
918  else if( !p->m_bIsAdopted )
919  {
920  PCMDWARN(cmdname, "The " << animal << " is not yours to walk.");
921  return OK;
922  }
923 
924  ++n; // next argument
925 
926  double minutes = argv[n].f();
927 
928  p->m_eActivity = ActWalking;
930  p->m_fActDuration = (double)minutes * 60.0;
931 
932  cout << "You are walking the " << p->m_sCommonName << " for " << minutes
933  << " minutes." << endl;
934 
935  //
936  // Fun stuff
937  //
938  const char *gait;
939 
940  //
941  // I used printf here, instead of cout insertion. Lot easier. But then, I've
942  // always preferred stdio over fstream.
943  //
944  for(int i = 0; i < 40; ++i)
945  {
946  gait = AsciiWalking[i%4][0];
947  printf("%*s%s\r", i, " ", gait);
948  fflush(stdout);
949  usleep(50000);
950  }
951 
952  usleep(250000);
953  printf("%*s\r", 80, " ");
954  fflush(stdout);
955 
956  printf("\n");
957 
958  return OK;
959 }
bool m_bIsAdopted
is [not] adopted
Definition: rnr_eg_cli.cxx:495
int checkCmd(const CmdExtArg &argv0, int argc, const string strTgtName="")
Check if command input is matched correctly with the command execution.
Definition: rnr_eg_cli.cxx:592
Time ActivityTime
activity time
Definition: rnr_eg_cli.cxx:548
const char * m_sCommonName
command name
Definition: rnr_eg_cli.cxx:489
double m_fActDuration
activitiy uninterrupted duration
Definition: rnr_eg_cli.cxx:498
walking
Definition: rnr_eg_cli.cxx:480
double now()
Get the current time, indentified by CLOCK_REALTIME, since the last Epoch.
Definition: Time.cxx:208
Activity m_eActivity
current activity
Definition: rnr_eg_cli.cxx:496
static AnimalInfo * findAnimal(const string &strCommonName)
Find the animal with the common name.
Definition: rnr_eg_cli.cxx:536
double m_fActStart
activity start time
Definition: rnr_eg_cli.cxx:497
static const char * AsciiWalking[][2]
ASCII walk sequence.
Definition: rnr_eg_cli.cxx:351
static AnimalInfo* findAnimal ( const string &  strCommonName)
static

Find the animal with the common name.

Parameters
strCommonNameCommon name.
Returns
On succes, returns pointer to the animal's info. On failure, NULL is returned.

Definition at line 536 of file rnr_eg_cli.cxx.

References NumOfAnimals.

Referenced by execAdopt(), execFeedAnimal(), execGetPetsState(), execNameAnimal(), execReward(), execSave(), execSleep(), and execWalkAnimal().

537 {
538  for(size_t i = 0; i < NumOfAnimals; ++i)
539  {
540  if( strCommonName == Animals[i].m_sCommonName )
541  {
542  return &Animals[i];
543  }
544  }
545  return NULL;
546 }
AnimalInfo Animals[]
The animals.
Definition: rnr_eg_cli.cxx:506
size_t NumOfAnimals
number of animals
Definition: rnr_eg_cli.cxx:525
static int findCommand ( const std::string &  strName)
static

Find command by name.

Parameters
strNameCommand to find.
Returns
On succes, returns index to Commands[], otherwise -1 is returned.

Definition at line 1420 of file rnr_eg_cli.cxx.

References NumOfCmds.

Referenced by execTAdd().

1421 {
1422  for(int i = 0; i < NumOfCmds; ++i)
1423  {
1424  if( strName == Commands[i].m_desc.name )
1425  {
1426  return i;
1427  }
1428  }
1429  return -1;
1430 }
AppCmdExec Commands[]
The command descriptions.
const size_t NumOfCmds
Number of commands.
static int loadCommands ( CommandLine cli)
static

Load commands into command line.

Loading involves adding all commands and then compiling.

Parameters
cliCommand line interface.
Returns
OK(0) on success, negative value on failure.

Definition at line 1952 of file rnr_eg_cli.cxx.

References rnr::cmd::addons::addBtEnableCommand(), rnr::cmd::CommandLine::addCommand(), rnr::cmd::addons::addHelpCommand(), rnr::cmd::addons::addQuitCommand(), rnr::cmd::CommandLine::backtrace(), rnr::cmd::CommandLine::compile(), rnr::cmd::NoUid, NumOfCmd3s, NumOfCmds, and PERROR.

Referenced by main().

1953 {
1954  int nUid;
1955  int rc;
1956 
1957  //
1958  // Add "normal" commands to command-line interface.
1959  //
1960  for(size_t i = 0; i < NumOfCmds; ++i)
1961  {
1962  nUid = cli.addCommand(Commands[i].m_desc, Commands[i].m_fnExec);
1963 
1964  if( nUid == NoUid )
1965  {
1966  PERROR("Failed to add command '" << Commands[i].m_desc.name << "'.");
1967  return RC_ERROR;
1968  }
1969  }
1970 
1971  //
1972  // Add "self-referenced" commands to command-line interface.
1973  //
1974  for(size_t i = 0; i < NumOfCmd3s; ++i)
1975  {
1976  nUid = cli.addCommand(Command3s[i].m_desc, Command3s[i].m_fnExec);
1977 
1978  if( nUid == NoUid )
1979  {
1980  PERROR("Failed to add command '" << Command3s[i].m_desc.name << "'.");
1981  return RC_ERROR;
1982  }
1983  }
1984 
1985  //
1986  // Add built-in commands to interface.
1987  //
1991 
1992  //
1993  // Now compile comamnd-line interace.
1994  //
1995  if( (rc = cli.compile()) != OK )
1996  {
1997  PERROR("Compile failed.");
1998  cerr << "(backtrace)" << endl;
1999  cli.backtrace(cerr, true);
2000  }
2001 
2002  return rc;
2003 }
AppCmdExec Commands[]
The command descriptions.
const size_t NumOfCmd3s
Number of commands.
virtual int compile()
Compile all added commands.
const int NoUid
Special values.
Definition: CmdCore.h:116
AppCmdExec3 Command3s[]
The command descriptions.
std::ostream & backtrace(std::ostream &os, const bool bAll=false) const
Insert trace and error log backtrace into output stream.
virtual int addCommand(const CmdDesc &desc)
Add a command to the command line interface.
int addBtEnableCommand(CommandLine &cli, const std::string &strName="bt")
Add the core &#39;bt&#39; command to the command-line interface.
int addHelpCommand(CommandLine &cli, const std::string &strName="help")
Add the core &#39;help&#39; command to the command-line interface.
#define PERROR(_err)
Error and warning printing macros.
Definition: rnr_eg_cli.cxx:564
const size_t NumOfCmds
Number of commands.
int addQuitCommand(CommandLine &cli, const std::string &strName="quit")
Add the core &#39;quit&#39; command to the command-line interface.
int main ( int  argc,
char *  argv[] 
)

Main.

Parameters
argcCommand-line argument count.
argvCommand-line argument list.
Returns
Returns 0 on succes, non-zero on failure.

Definition at line 2057 of file rnr_eg_cli.cxx.

References APP_EC_EXEC, APP_EC_OK, AsciiAardvark, Cli, CliName(), loadCommands(), mainInit(), PERROR, and run().

2058 {
2059  mainInit(argc, argv);
2060 
2061  if( loadCommands(Cli) != OK )
2062  {
2063  PERROR("Failed to load commands.");
2064  return APP_EC_EXEC;
2065  }
2066 
2067  // debug
2068  //cerr << Cli << endl;
2069 
2070  cout << AsciiAardvark;
2071  cout << CliName << " CommandLine/ReadLine/LogBook/RegEx Example" << endl;
2072  cout << " (enter 'help' for list of commands)" << endl << endl;
2073 
2074  if( run(Cli) != OK )
2075  {
2076  PERROR("Failed to run commands.");
2077  return APP_EC_EXEC;
2078  }
2079 
2080  return APP_EC_OK;
2081 }
#define APP_EC_EXEC
execution exit code
Definition: rnr_eg_cli.cxx:74
static const char * AsciiAardvark[]
ASCII ardvaark.
Definition: rnr_eg_cli.cxx:125
static int run(CommandLine &cli)
Command line interface main loop.
#define APP_EC_OK
success exit code
Definition: rnr_eg_cli.cxx:72
static void mainInit(int argc, char *argv[])
Main initialization.
static int loadCommands(CommandLine &cli)
Load commands into command line.
CommandLine Cli(CliName, CliPrompt)
the CLI
#define PERROR(_err)
Error and warning printing macros.
Definition: rnr_eg_cli.cxx:564
const string CliName("Adopt-An-Animal")
CLI name.
static void mainInit ( int  argc,
char *  argv[] 
)
static

Main initialization.

Parameters
argcCommand-line argument count.
argvCommand-line argument list.
Exits:
Program terminates on conversion error.

Definition at line 1909 of file rnr_eg_cli.cxx.

References Argv0, OptsInfo, PgmInfo, and PkgInfo.

Referenced by main().

1910 {
1911  // name of this process
1912  Argv0 = basename(argv[0]);
1913 
1914  // parse input options
1915  argv = OptsGet(Argv0, &PkgInfo, &PgmInfo, OptsInfo, true, &argc, argv);
1916 }
static OptsInfo_T OptsInfo[]
Command line options information.
Definition: rnr_eg_cli.cxx:100
static const PkgInfo_T PkgInfo
Definition: version.h:45
static char * Argv0
the command
Definition: rnr_eg_cli.cxx:76
static OptsPgmInfo_T PgmInfo
Program information.
Definition: rnr_eg_cli.cxx:81
static ostream& operator<< ( ostream &  os,
const char *  art[] 
)
static

ASCII art output stream operator.

Parameters
osOutput stream.
artArt to insert.
Returns
Reference to output stream.

Definition at line 367 of file rnr_eg_cli.cxx.

368 {
369  for(int i = 0; art[i] != NULL; ++i)
370  {
371  os << art[i] << endl;
372  }
373  return os;
374 }
static int run ( CommandLine cli)
static

Command line interface main loop.

Parameters
cliCommand line interface.
Returns
OK(0) on success, negative value on failure.

Definition at line 2012 of file rnr_eg_cli.cxx.

References rnr::cmd::CommandLine::addToHistory(), rnr::cmd::CommandLine::execute(), rnr::cmd::CommandLine::ok(), PERROR, rnr::cmd::CommandLine::readCommand(), and updateAnimals().

Referenced by main().

2013 {
2014  CmdExtArgVec argv; // vector of string input arguments
2015  int rc; // return code
2016 
2017  while( cli.ok() )
2018  {
2019  rc = cli.readCommand(argv);
2020 
2021  updateAnimals();
2022 
2023  if( rc == OK )
2024  {
2025  // see the results of a good command match
2026  //cli.backtrace(cerr);
2027 
2028  if( argv.size() > 0 )
2029  {
2030  rc = cli.execute(argv);
2031 
2032  if( rc == OK )
2033  {
2034  cli.addToHistory(argv);
2035  }
2036  }
2037  }
2038  else
2039  {
2040  PERROR("Bad command.");
2041  // cerr << "backtrace:" << endl;
2042  // cli.backtrace(cerr);
2043  }
2044  }
2045 
2046  return OK;
2047 }
virtual int execute(const str::StringVec &argv)
Execute a comamnd with the given arguments.
std::vector< CmdExtArg > CmdExtArgVec
vector of ext args type
Definition: CmdExtArg.h:397
virtual int readCommand(int &uid, int &iform, str::StringVec &argv)
Read an input line from stdin and match to the best compiled command.
Definition: CommandLine.h:683
bool ok() const
Test if command-line interface is ok to continue.
Definition: CommandLine.h:481
#define PERROR(_err)
Error and warning printing macros.
Definition: rnr_eg_cli.cxx:564
virtual void addToHistory(const str::StringVec &argv)
Add command to history.
static void updateAnimals()
Update all animals&#39; state data.
static void showAsciiArt ( ostream &  os,
const char *  art0[],
const char *  art1[],
const char *  art2[] = NULL,
const char *  art3[] = NULL 
)
static

Write a set of ASCII art to cout.

The art written left to right, bottom justified.

Parameters
osOutput stream.
art0Required art 0.
art1Required art 1.
art2Optional art 2.
art3Optional art 3.

Definition at line 387 of file rnr_eg_cli.cxx.

Referenced by execFeedAnimal().

392 {
393  int nPieces = 2;
394  int linecnt[4];
395  int maxlen[4];
396  int maxlinecnt = 0;
397  int len;
398  int i, j;
399 
400  const char **pieces[4];
401 
402  pieces[0] = art0;
403  pieces[1] = art1;
404 
405  if( art2 != NULL )
406  {
407  ++nPieces;
408  pieces[2] = art2;
409  }
410 
411  if( art3 != NULL )
412  {
413  ++nPieces;
414  pieces[3] = art3;
415  }
416 
417  // gather stats
418  for(i = 0; i < nPieces; ++i)
419  {
420  linecnt[i] = 0;
421  maxlen[i] = 0;
422 
423  for(j = 0; pieces[i][j] != NULL; ++j)
424  {
425  len = strlen(pieces[i][j]);
426  if( len > maxlen[i] )
427  {
428  maxlen[i] = len;
429  }
430  linecnt[i] += 1;
431  }
432 
433  if( linecnt[i] > maxlinecnt )
434  {
435  maxlinecnt = linecnt[i];
436  }
437  }
438 
439  char ofill = os.fill();
440  size_t owidth = os.width();
441 
442  os.fill(' ');
443 
444  // show
445  for(int line = 0; line < maxlinecnt; ++line)
446  {
447  for(i = 0; i < nPieces; ++i)
448  {
449  if( linecnt[i] >= maxlinecnt-line )
450  {
451  j = linecnt[i] - (maxlinecnt - line);
452  // os << "line " << line << ", art " << i << " j " << j << endl;
453  len = strlen(pieces[i][j]);
454  os << pieces[i][j] << setw(maxlen[i]-len+1) << "";
455  }
456  else
457  {
458  os << setw(maxlen[i]) << "";
459  }
460  }
461  os << endl;
462  }
463 
464  os.fill(ofill);
465  os.width(owidth);
466 }

Variable Documentation

AnimalInfo Animals[]
Initial value:
=
{
{ "aardvark", "orycteropus afer", AsciiAardvark,
NoName, false, ActIdle, 0.0, 0.0
},
{ "mandrill", "mandrillus sphinx", AsciiMandrill,
NoName, false, ActIdle, 0.0, 0.0
},
{ "numbat", "myrmecobius fasciatus", AsciiNumbat,
NoName, false, ActIdle, 0.0, 0.0
},
{ "zebra", "equus quagga", AsciiZebra,
NoName, false, ActIdle, 0.0, 0.0
}
}
static const char * AsciiZebra[]
ASCII zebra.
Definition: rnr_eg_cli.cxx:226
const char * NoName
&#39;no name&#39; name
Definition: rnr_eg_cli.cxx:501
static const char * AsciiAardvark[]
ASCII ardvaark.
Definition: rnr_eg_cli.cxx:125
idle, doing nothing
Definition: rnr_eg_cli.cxx:477
static const char * AsciiNumbat[]
ASCII numbat.
Definition: rnr_eg_cli.cxx:178
static const char * AsciiMandrill[]
ASCII Mandrill.
Definition: rnr_eg_cli.cxx:151

The animals.

Definition at line 506 of file rnr_eg_cli.cxx.

const char* AsciiAardvark[]
static
Initial value:
=
{
" _.---._ /\\\\",
" ./' \"--`\\//",
" ./ o \\",
" /./\\ )______ \\__ \\",
" ./ / /\\ \\ | \\ \\ \\ \\",
" / / \\ \\ | |\\ \\ \\7",
" \" \" \" \"",
}

ASCII ardvaark.

Definition at line 125 of file rnr_eg_cli.cxx.

Referenced by main().

const char* AsciiAnt[]
static
Initial value:
=
{
" '\\__",
" (o ) ___",
" <>(_)(_)(___)",
" < < > >",
" ' ' ` `",
NULL
}

ASCII ant.

Definition at line 260 of file rnr_eg_cli.cxx.

Referenced by execFeedAnimal().

const char* AsciiCarrot[]
static
Initial value:
=
{
" \\/'",
" \\/'",
" _/'",
" (,;)",
" (,.)",
" (,/",
" |/",
NULL
}

ASCII carrot.

Definition at line 298 of file rnr_eg_cli.cxx.

Referenced by execFeedAnimal().

const char* AsciiGrub[]
static
Initial value:
=
{
" \\",
" '-.__.-'",
" /oo |--.--,--,--.",
" \\_.-'._i__i__i_.'",
" \"\"\"\"\"\"\"\"\"",
NULL
}

ASCII grub.

Definition at line 273 of file rnr_eg_cli.cxx.

Referenced by execFeedAnimal().

const char* AsciiMandrill[]
static
Initial value:
=
{
" :: ,-, ::",
" :: :o~o: ::",
" ::.___.-:{|}:--..-::;",
" \"=___ '_' ,..-\"",
" :-, ' :__",
" \\ \\ / :",
" \\ :. .:,__:",
NULL
}

ASCII Mandrill.

Definition at line 151 of file rnr_eg_cli.cxx.

const char* AsciiNumbat[]
static
Initial value:
=
{
" _ ____ ~^",
" \\`.|\\..----''` / /`'--.,.,, ~''",
" / ' ` / /,/ / / ,,. - ~,,",
" )/' _/ \\ / `-_,/ / - \",,,' ",
" | /' `\"\\_ ,_.-;_.-\\_ ', '',,,'",
" |/ _.-'_/ <__.' ; ",
" {_.-``-' <__",
NULL
}

ASCII numbat.

Definition at line 178 of file rnr_eg_cli.cxx.

const char* AsciiVitamins[]
static
Initial value:
=
{
" _____",
" _(_|_|_)_",
" / _ \\",
" ' (C) ( \\ '",
" | ___ \\B\\ |",
" |(_A_) \\ )|",
" '___________'",
NULL
}

ASCII bottle of vitamins.

Definition at line 325 of file rnr_eg_cli.cxx.

Referenced by execFeedAnimal().

const char* AsciiWalking[][2]
static
Initial value:
=
{
{" |''''|\"> ", NULL},
{" |\\'''|\\\">", NULL},
{" /\\'''/\\\">", NULL},
{" /|''/|\"> ", NULL}
}

ASCII walk sequence.

Definition at line 351 of file rnr_eg_cli.cxx.

Referenced by execWalkAnimal().

const char* AsciiZebra[]
static
Initial value:
=
{
" \\\\/),",
" ,'.' /,",
" (_)- / /,",
" /\\_/ |__..--, *",
" (\\ _ /\\ \\ \\ / ).'",
" \\(-'./ / (_ //",
" \\\\ \\,'--'\\_(",
" )(_/ )_/ )_)",
" (_,' (_.'(_.'",
NULL
}

ASCII zebra.

Definition at line 226 of file rnr_eg_cli.cxx.

const char* AsciiZebraSmall[]
static
Initial value:
=
{
" ,,_ ",
" =/.-\" ",
" ~._ =// ",
" _(||||\\|_ ",
" / / ",
NULL
}

ASCII small zebra.

Definition at line 213 of file rnr_eg_cli.cxx.

AppCmdExec3 Command3s[]
Initial value:
=
{
{ { CliTestCmdName,
"<clitest:re(^t[abcdkpr].+)> [<modifier:multiword>]",
"Test CommandLine interface features.",
"The 'clitest' command validates command wild carding and provides test "
"functions to validate the CommandLine and underlining classes.\n\n"
"Supported Test Functions:\n"
" tadd <cmd> - Add command to interface.\n"
" tbt - Backtrace log.\n"
" tcompile - (Re)compile interface.\n"
" tdump [<cmd>] - Dump all or <cmd> definitions.\n"
" tkbhit - Check for keyboard hits.\n"
" tprint <attr> - Print attribute, where <attr> is one of:\n"
" name prompt numcmds errstr.\n"
" tpush <prompt> - Push new <prompt> string.\n"
" tpop - Pop current prompt string.\n"
" tremove <cmd> - Remove command from interface.\n\n"
"Demonstrates command name wild carding.\n"
"Demonstrates regular expresson variable argument type."
},
}
}
static int execCliTest(CommandLine &cli, const CmdExtArgVec &argv)
Execute command-line interface methods command.

The command descriptions.

Definition at line 1865 of file rnr_eg_cli.cxx.

OptsInfo_T OptsInfo[]
static
Initial value:
=
{
{NULL, }
}

Command line options information.

Definition at line 100 of file rnr_eg_cli.cxx.

Referenced by mainInit().

OptsPgmInfo_T PgmInfo
static
Initial value:
=
{
NULL,
"A RoadNarrows Robotics librnr_appkit CLI example program.",
"The %P command demonstrates the use of librnr_appkit's CommandLine, "
"ReadLine, LogBook, RegEx classes to build a command line interface.",
NULL
}

Program information.

Definition at line 81 of file rnr_eg_cli.cxx.

Referenced by mainInit().