appkit  1.5.1
RoadNarrows Robotics Application Kit
rnr::cmd::addons Namespace Reference

Add-Ons. More...

Enumerations

enum  HelpSect {
  HelpSectAll,
  HelpSectName,
  HelpSectBrief,
  HelpSectUsage
}
 Command help section identifiers. More...
 

Functions

int printCmdHelp (std::ostream &os, const CmdDef &cmddef, const HelpSect section=HelpSectAll)
 Print help for a command convenience function. More...
 
int printCmdHelp (std::ostream &os, const CmdDesc &desc, const HelpSect section=HelpSectAll)
 Print help for a command convenience function. More...
 
int printCmdHelp (std::ostream &os, const std::string &strName, const std::string &strSyntax, const std::string &strSynopsis, const std::string &strLongDesc, const HelpSect section=HelpSectAll)
 Print help for a command convenience function. More...
 
int addHelpCommand (CommandLine &cli, const std::string &strName="help")
 Add the core 'help' command to the command-line interface. More...
 
int addQuitCommand (CommandLine &cli, const std::string &strName="quit")
 Add the core 'quit' command to the command-line interface. More...
 
int addBtEnableCommand (CommandLine &cli, const std::string &strName="bt")
 Add the core 'bt' command to the command-line interface. More...
 
static string atname ("@N@")
 string to replace by command name
 
static int execHelp (CommandLine &cli, const CmdExtArgVec &argv)
 Execute 'help' command. More...
 
int printCmdHelp (ostream &os, const CmdDef &cmddef, const HelpSect section)
 
int printCmdHelp (ostream &os, const CmdDesc &desc, const HelpSect section)
 
int printCmdHelp (ostream &os, const string &strName, const string &strSyntax, const string &strSynopsis, const string &strLongDesc, const HelpSect section)
 
int addHelpCommand (CommandLine &cli, const string &strName)
 
static int execQuit (CommandLine &cli, const CmdExtArgVec &argv)
 Execute 'quit' command. More...
 
int addQuitCommand (CommandLine &cli, const string &strName)
 
static int execBtEnable (CommandLine &cli, const CmdExtArgVec &argv)
 Execute 'backtrace' command. More...
 
int addBtEnableCommand (CommandLine &cli, const string &strName)
 

Detailed Description

Add-Ons.

Enumeration Type Documentation

Command help section identifiers.

Enumerator
HelpSectAll 

all commond help sections

HelpSectName 

command name help section

HelpSectBrief 

command brief description help section

HelpSectUsage 

command usage (synopsis) help section

Definition at line 95 of file CmdAddOns.h.

96  {
97  HelpSectAll, ///< all commond help sections
98  HelpSectName, ///< command name help section
99  HelpSectBrief, ///< command brief description help section
100  HelpSectUsage ///< command usage (synopsis) help section
101  };
command name help section
Definition: CmdAddOns.h:98
command brief description help section
Definition: CmdAddOns.h:99
command usage (synopsis) help section
Definition: CmdAddOns.h:100
all commond help sections
Definition: CmdAddOns.h:97

Function Documentation

int rnr::cmd::addons::addBtEnableCommand ( CommandLine cli,
const std::string &  strName = "bt" 
)

Add the core 'bt' command to the command-line interface.

Syntax: bt <onoff>

The bt command enables/disables backtracing of input line parsing. The trace is written to cerr.

Any relevant command data is applied to the command line interface "core" data section.

The name of the command may be overridded, but the syntax and semantics cannot.

Parameters
cliCommand-line interface.
strNameName of the command.
Returns
On success, returns command's assigned unique id. Otherwise NoUid is returned.

Referenced by execBtEnable(), and loadCommands().

int rnr::cmd::addons::addHelpCommand ( CommandLine cli,
const std::string &  strName = "help" 
)

Add the core 'help' command to the command-line interface.

Syntax: help [{–brief | -b | –list | -l | –usage | -u}] [<cmd>]

The help command will print help for the given help sections for the given command names to cout.

Any relevant command data is applied to the command line interface "core" data section.

The name of the command may be overridded, but the syntax and semantics cannot.

Parameters
cliCommand-line interface.
strNameName of the command.
Returns
On success, returns command's assigned unique id. Otherwise NoUid is returned.

Referenced by loadCommands(), and printCmdHelp().

int rnr::cmd::addons::addQuitCommand ( CommandLine cli,
const std::string &  strName = "quit" 
)

Add the core 'quit' command to the command-line interface.

Syntax: quit

The quit command will disable anymore execution of the command line. It is up to the application to terminate by querying the command line ok() method.

Any relevant command data is applied to the command line interface "core" data section.

The name of the command may be overridded, but the syntax and semantics cannot.

Parameters
cliCommand-line interface.
strNameName of the command.
Returns
On success, returns command's assigned unique id. Otherwise NoUid is returned.

Referenced by execQuit(), and loadCommands().

static int rnr::cmd::addons::execBtEnable ( CommandLine cli,
const CmdExtArgVec argv 
)
static

Execute 'backtrace' command.

Syntax:
bt <onoff>
Parameters
cliCommand line interface.
argvExtended command line arguments.
Returns
OK(0) on success, negative value on failure.

Definition at line 474 of file CmdAddOns.cxx.

References addBtEnableCommand(), rnr::cmd::CommandLine::addCommand(), atname(), rnr::cmd::CmdDesc::name, rnr::str::replace(), and rnr::cmd::CommandLine::setBtEnable().

475  {
476  cli.setBtEnable(argv[1].b());
477  return OK;
478  }
void setBtEnable(bool bEnable)
Enable/disable backtracing.
Definition: CommandLine.h:510
static int rnr::cmd::addons::execHelp ( CommandLine cli,
const CmdExtArgVec argv 
)
static

Execute 'help' command.

Syntax:
help [{–list | -l | –synopsis | -s | –usage | -u}] [<cmd>]
Todo:
Support ellipses ... syntax.
Parameters
cliCommand line interface.
argvExtended command line arguments.
Returns
OK(0) on success, negative value on failure.

Definition at line 114 of file CmdAddOns.cxx.

References rnr::cmd::CommandLine::at(), rnr::cmd::CommandLine::begin(), rnr::cmd::CommandLine::end(), rnr::cmd::CmdDef::getLongDesc(), rnr::cmd::CmdDef::getName(), rnr::cmd::CmdDef::getSynopsis(), rnr::cmd::CmdDef::getSyntax(), rnr::cmd::CmdDef::getUid(), HelpSectAll, HelpSectBrief, HelpSectName, HelpSectUsage, rnr::cmd::CmdDesc::longdesc, rnr::cmd::CmdDesc::name, rnr::cmd::NoUid, printCmdHelp(), rnr::cmd::CmdDesc::synopsis, and rnr::cmd::CmdDesc::syntax.

Referenced by printCmdHelp().

115  {
116  HelpSect section = HelpSectAll; // default help option
117 
118  std::set<string> setOfCmds; // set of commands for help
119  std::set<string>::iterator iterHelp; // help commands iterator
120  size_t i;
121 
122  //
123  // Process Input arguments.
124  //
125  for(i = 1; i < argv.size(); ++i)
126  {
127  if( (argv[i] == "--brief") || (argv[i] == "-b") )
128  {
129  section = HelpSectBrief;
130  }
131  else if( (argv[i] == "--list") || (argv[i] == "-l") )
132  {
133  section = HelpSectName;
134  }
135  else if( (argv[i] == "--usage") || (argv[i] == "-u") )
136  {
137  section = HelpSectUsage;
138  }
139  else
140  {
141  setOfCmds.insert(argv[i].s());
142  }
143  }
144 
145  //
146  // All commands.
147  //
148  if( setOfCmds.empty() )
149  {
150  CmdDefCIter iterDefs;
151 
152  // alphbetize
153  for(iterDefs = cli.begin(); iterDefs != cli.end(); ++iterDefs)
154  {
155  setOfCmds.insert(iterDefs->second.getName());
156  }
157  }
158 
159  //
160  // Print help for command(s).
161  //
162  for(iterHelp = setOfCmds.begin();
163  iterHelp != setOfCmds.end();
164  ++iterHelp)
165  {
166  const string &cmdName = *iterHelp;
167  const CmdDef &cmdDef = cli.at(cmdName);
168 
169  if( cmdDef.getUid() == NoUid )
170  {
171  cout << "Error: help: No help for command '" << cmdName << "'."
172  << endl;
173  return RC_ERROR;
174  }
175 
176  printCmdHelp(cout, cmdDef, section);
177 
178  switch( section )
179  {
180  case HelpSectName:
181  cout << " ";
182  break;
183  case HelpSectBrief:
184  case HelpSectUsage:
185  cout << endl;
186  break;
187  case HelpSectAll:
188  default:
189  cout << " ---" << endl << endl;
190  break;
191  }
192  }
193 
194  switch( section )
195  {
196  case HelpSectName:
197  cout << endl;
198  break;
199  case HelpSectBrief:
200  case HelpSectUsage:
201  case HelpSectAll:
202  default:
203  cout << " " << setOfCmds.size() << " commands" << endl;
204  break;
205  }
206 
207  return OK;
208  }
int getUid() const
Get command&#39;s unique id.
Definition: CmdDef.h:134
CmdDefMap::const_iterator CmdDefCIter
cmd const iter type
Definition: CommandLine.h:418
CmdDefCIter begin() const
Return a constant iterator pointing to the first element of the list of command definitions.
Definition: CommandLine.h:1119
CmdDefCIter end() const
Return a constant iterator referring to the past-the-end element of the list of command definitions...
Definition: CommandLine.h:1130
const int NoUid
Special values.
Definition: CmdCore.h:116
const CmdDef & at(const int uid) const
Get the command definition with the unique id.
Compiled command definition class.
Definition: CmdDef.h:88
HelpSect
Command help section identifiers.
Definition: CmdAddOns.h:95
command name help section
Definition: CmdAddOns.h:98
int printCmdHelp(ostream &os, const string &strName, const string &strSyntax, const string &strSynopsis, const string &strLongDesc, const HelpSect section)
Definition: CmdAddOns.cxx:226
command brief description help section
Definition: CmdAddOns.h:99
command usage (synopsis) help section
Definition: CmdAddOns.h:100
all commond help sections
Definition: CmdAddOns.h:97
static int rnr::cmd::addons::execQuit ( CommandLine cli,
const CmdExtArgVec argv 
)
static

Execute 'quit' command.

Syntax:
quit
Parameters
cliCommand line interface.
argvExtended command line arguments.
Returns
OK(0) on success, negative value on failure.

Definition at line 428 of file CmdAddOns.cxx.

References rnr::cmd::CommandLine::addCommand(), addQuitCommand(), atname(), rnr::cmd::CmdDesc::name, rnr::cmd::CommandLine::quit(), and rnr::str::replace().

429  {
430  cli.quit();
431  return OK;
432  }
void quit()
Mark command-line interface to quit.
Definition: CommandLine.h:489
int rnr::cmd::addons::printCmdHelp ( std::ostream &  os,
const CmdDef cmddef,
const HelpSect  section = HelpSectAll 
)

Print help for a command convenience function.

Parameters
osOutput stream.
cmddefCommand definition.
sectionHelp section.
Returns
OK(0) on success, negative value on failure.
int rnr::cmd::addons::printCmdHelp ( std::ostream &  os,
const CmdDesc desc,
const HelpSect  section = HelpSectAll 
)

Print help for a command convenience function.

Parameters
osOutput stream.
descCommand description.
sectionHelp section.
Returns
OK(0) on success, negative value on failure.
int rnr::cmd::addons::printCmdHelp ( std::ostream &  os,
const std::string &  strName,
const std::string &  strSyntax,
const std::string &  strSynopsis,
const std::string &  strLongDesc,
const HelpSect  section = HelpSectAll 
)

Print help for a command convenience function.

Parameters
osOutput stream.
strNameCommand name.
strSyntaxCommand usage syntax.
strSynopsisCommand short synopsis.
strLongDescCommand long description.
sectionHelp section.
Returns
OK(0) on success, negative value on failure.
int rnr::cmd::addons::printCmdHelp ( ostream &  os,
const string &  strName,
const string &  strSyntax,
const string &  strSynopsis,
const string &  strLongDesc,
const HelpSect  section 
)

wrap and indent

Definition at line 226 of file CmdAddOns.cxx.

References rnr::cmd::CommandLine::addCommand(), addHelpCommand(), atname(), execHelp(), HelpSectAll, HelpSectBrief, HelpSectName, HelpSectUsage, rnr::cmd::CmdDesc::name, rnr::str::replace(), and rnr::str::split().

Referenced by execHelp().

232  {
233  size_t i;
234 
235  if( strName.empty() )
236  {
237  os << "Error: Command has no name." << endl;
238  return RC_ERROR;
239  }
240 
241  if( strSyntax.empty() )
242  {
243  os << "Error: Command has no extended usage syntax." << endl;
244  return RC_ERROR;
245  }
246 
247  StringVec usages = split(strSyntax, '\n');
248 
249  switch( section )
250  {
251  case HelpSectName:
252  os << strName;
253  break;
254 
255  case HelpSectBrief:
256  os << strName << " - " << strSynopsis << endl;
257  break;
258 
259  case HelpSectUsage:
260  for(i = 0; i < usages.size(); ++i)
261  {
262  if( !usages[i].empty() )
263  {
264  os << usages[i] << "\n";
265  }
266  }
267  break;
268 
269  case HelpSectAll:
270  default:
271  ssize_t len;
272  size_t w;
273 
274  len = (ssize_t)strName.size() + 2;
275  len = 80 - 2 * len;
276  os << strName << "()";
277  if( len > 1 )
278  {
279  w = os.width();
280  os.width(len);
281  os << "";
282  os.width(w);
283  os << strName << "()";
284  }
285  os << "\n\n";
286 
287  os << "Name\n " << strName;
288  os << " - " << strSynopsis;
289  os << "\n\n";
290 
291  os << "Synopsis\n";
292  for(i = 0; i < usages.size(); ++i)
293  {
294  if( !usages[i].empty() )
295  {
296  os << " " << usages[i] << "\n";
297  }
298  }
299  os << "\n";
300 
301  if( !strLongDesc.empty() )
302  {
303  os << "Description\n";
304 
305  StringVec lines = split(strLongDesc, '\n');
306  size_t wstart, wend, wlen; // word data
307  size_t cursor; // output line cursor
308 
309  for(i = 0; i < lines.size(); ++i)
310  {
311  // indent
312  os << " ";
313  cursor = 2;
314 
315  //
316  // Output line, wrapping if necessary.
317  //
318  for(size_t j = 0; j < lines[i].size(); )
319  {
320  // whitespace
321  while( isspace(lines[i][j]) && (j < lines[i].size()) )
322  {
323  // less than max output line length
324  if( cursor < 80 )
325  {
326  cout << ' ';
327  ++cursor;
328  }
329  // wrap
330  else
331  {
332  // wrap and indent
333  cout << "\n ";
334  cursor = 2;
335  }
336  ++j;
337  }
338 
339  if( j >= lines[i].size() )
340  {
341  break;
342  }
343 
344  // word
345  wstart = j;
346 
347  while( !isspace(lines[i][j]) && (j < lines[i].size()) )
348  {
349  ++j;
350  }
351 
352  wend = j;
353  wlen = wend - wstart;
354 
355  // wrap
356  if( (cursor > 2) && (cursor+wlen >= 80) )
357  {
358  /// wrap and indent
359  cout << "\n ";
360  cursor = 2;
361  }
362 
363  cout << lines[i].substr(wstart, wlen);
364  cursor += wlen;
365  }
366 
367  cout << '\n';
368  }
369  cout << '\n';
370  }
371  break;
372  }
373 
374  return OK;
375  }
std::vector< std::string > StringVec
Useful types.
Definition: StringTheory.h:83
size_t split(const std::string &str, const char delim, StringVec &elems)
Split string.
command name help section
Definition: CmdAddOns.h:98
command brief description help section
Definition: CmdAddOns.h:99
command usage (synopsis) help section
Definition: CmdAddOns.h:100
all commond help sections
Definition: CmdAddOns.h:97