Hex  1.0
Hydrogen-electron collision solver
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
Functions
cmdline.h File Reference
#include <algorithm>
#include <string>
#include "misc.h"
Include dependency graph for cmdline.h:
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Functions

template<class DefaultCallback >
bool HandleSwitch (int &i, int argc, char *argv[], DefaultCallback callback)
 Command line option default hanndler. More...
 
template<class Callback , class... Params>
bool HandleSwitch (int &i, int argc, char *argv[], std::string longoptname, std::string shortoptname, unsigned noptarg, Callback callback, Params...params)
 Command line option handler. More...
 
template<class... Params>
void ParseCommandLine (int argc, char *argv[], Params...params)
 Parse command line. More...
 

Function Documentation

template<class DefaultCallback >
bool HandleSwitch ( int &  i,
int  argc,
char *  argv[],
DefaultCallback  callback 
)

This function is used by the parser ParseCommandLine and it is not expected to be used on its own by the user. It is called by ParseCommandLine or by the other HandleSwitch function template if the remaining number of handlers is one – just the default callback.

Parameters
iIndex of current argv[i] being parsed. On return, the value is incremented because the argv[i] will have been digested by this function.
argcArgc as passed to the main function.
argvArgv as passed to the main function.
callbackDefault callback, which is a functor accepting two std::string parameters: the unmatched option and its optarg.
template<class Callback , class... Params>
bool HandleSwitch ( int &  i,
int  argc,
char *  argv[],
std::string  longoptname,
std::string  shortoptname,
unsigned  noptarg,
Callback  callback,
Params...  params 
)

This function is used by the parser ParseCommandLine and it is not expected to be used on its own by the user.

Parameters
iIndex of the option (argv[i]) to handle. On return, the value can be once or more times incremented, if more argv[i] values have been digested as optarg-s.
argcArgc as passed to the main function.
argvArgv as passed to the main function.
longoptnameLong option name of the handler to use.
shortoptnameShort option name of the handler to use.
noptargNumber of optarg-s needed for this option.
callbackCallback function accepting std::string optarg and returning bool.
...paramsOther params that will be ignored in this pass, but may be used in the next one if argv[i] does match neither longoptname nor shortoptname.
Returns
True if the parsing is to be continued, false to stop the parsing (e.g. on reaching i == argc).
template<class... Params>
void ParseCommandLine ( int  argc,
char *  argv[],
Params...  params 
)

This variadic function template can be used to parse the command line arguments. For every argv[i], i > 0, it will scan through the supplied handlers and whenever it finds a correct handler, it will call the associated callback function. A "handler" is a quartet of parameters:

  • [std::string] long option name (e.g. "help")
  • [std::string] short option name (e.g. "h")
  • [unsigned] expected number of optargs (zero or one in the present implementation)
  • [functor] callback function that accepts exactly one string argument (the optarg)

A typical usage of the parser is:

* argc, argv,
* "help", "h", 0, [](std::string optarg) -> bool { std::cout << "Help.\n"; return true; },
* "sleep", "s", 1, [](std::string optarg) -> bool { sleep(atoi(optarg.c_str())); return true; },
* ...
* [](std::string opt, std::string optarg) -> bool { std::cout << "Unknown option \"" << opt << "\" with argument \"" << optarg << "\"\n"; return false; }
* );
*

The last argument is the default handler that accepts also the option name. It is used if no handler matched the option name. If any handler returns "false", it will stop the parsing of further options. The long and short options are equivalent – both are expected to be introduced by one or more dashes on the command line. Short options can't be chained (e.g. "-hs") in the present implementation. The option argument can be given with or without the equation sign.

* # all variants are allowed:
* ./program --option=optarg
* ./program --option optarg
* ./program -o=optarg
* ./program -------option optarg
* ...
*