ROSE 0.11.145.192
|
Parses program command line switches and arguments.
--color
. The "--" is the switch prefix, "color" is the switch name, and "--color" is the switch string. --color=grey
or --color grey
(as two command-line arguments). GRAY
, whether the string on the command line was "grey" or "gray". Program command-line parsing consists of the following major components:
The library is used in three phases: first, the command line is described in terms of switches and their arguments; then a parser is constructed and applied to the command line to obtain a result; and finally, the result is used by querying or pushing results into variables.
Some of our goals in designing this library were influenced by other libraries. We wanted to take the good ideas of others but avoid the same pitfalls. Our goals are:
int
in the program. Here's an example to give the basic flavor of the library. The description of the command-line and the parsing of the command-line are combined in a single function, parseCommandLine
:
We'll divide the command-line into two groups of switches: switches that are common to all our tools, and switches that are specific to this particular tool. In real life, the common switches would be returned from some other function defined in a library that all the tools share. Each switch group may have its own documentation, which causes its switches to be grouped together in the manual page. Documentation is free-flowing and automatically wrapped to the width of the terminal window in which the command runs, so there is no need to have line feeds within the source code string literals, although they are allowed. A double line feed in the documentation string is a paragraph separator.
Now we can declare a couple switches inside our standard
switch group. We'll declare one that will show the manual page, and another that shows the version number. The man page is shown with –help
, -h
, or -?
, and the version is shown with –version
or -V
. Switch actions occur only when switch parsing is all done (i.e., the man page won't be emitted unless the command-line is parsable).
We place all the tool-specific switches in another switch group we call tool
. The –isa
switch will accept an argument that can be anything and is stored in std::string settings.isaName
. If the argument is the word "list" then the function that eventually parses it will generate a list (this function is part of the tool source code and is not part of Sawyer).
The next two switches are similar except they expect arguments that are non-negative integers. The argument will be parsed by Sawyer, which accepts decimal, octal, and hexadecimal using C syntax, and eventually assigned to the specified settings
data member, both of which are of type unsigned long
. The parser function, nonNegativeIntegerParser
, is overloaded to accept a variety of integer types and a parsing exception will be thrown if the argument string parses to a value which doesn't fit in the specified variable.
Now that the command-line switches are described, we can create a parser. The parser also controls the non-switch aspects of the manual page, such as the heading and footer, purpose, synopsis, and other sections.
Finally we can do the actual parsing. We could split this statement into multiple statements, but one of Sawyer's idioms is to chain things together in a functional programming style. The with
method inserts our switch declarations from above into the parser; the parse
method, of which there are multiple overloaded varieties, does the real work; and the apply
method applies the results of a successful parse by copying values into the variabiables we specified, and running switch actions (e.g., "help" and "version") if necessary.
Here's the whole example in one shot (the docs/examples/commandLineEx1.C file):
And here's the output from the –help
switch:
COMMANDLINEEX1(1) User Commands COMMANDLINEEX1(1) Name commandLineEx1 - disassembles files one address at a time Synopsis commandLineEx1 [switches] specimen_name Description This program is a very simple disassembler that tries to disassemble an instruction at each address of the specimen file. Options The following switches are recognized by all tools in this package. --help; -h; -? Show this documentation. --version; -V Show version number. The following switches are specific to this command. --alignment align Alignment for instructions. The default is 1 (no alignment). Values larger than one will cause each candidate address to be rounded up to the next multiple of align. If this rounding up causes addresses after a valid instruction to be skipped then a warning is printed. No warning is printed if the alignment skips addresses after a disassembly failure. --at virtual-address The first byte of the file is mapped at the specified virtual- address, which defaults to 0. --isa architecture Instruction set architecture. Specify "list" to see a list of possible ISAs. alpha July 2014 COMMANDLINEEX1(1)
Namespaces | |
namespace | Boost |
Drop-in replacement to help boost users. | |
Typedefs | |
typedef std::vector< ParsedValue > | ParsedValues |
A vector of parsed values. | |
typedef Container::Map< const SwitchGroup *, std::set< const Switch * > > | GroupedSwitches |
Subset of switches grouped by their switch groups. | |
typedef Container::Map< std::string, GroupedSwitches > | NamedSwitches |
Subset of switches indexed by their command-line representation. | |
Enumerations | |
enum | SortOrder { INSERTION_ORDER , DOCKEY_ORDER } |
The order in which things are sorted in the documentation. More... | |
enum | Canonical { CANONICAL , NONCANONICAL , ALL_STRINGS } |
Format of a switch string. More... | |
enum | ShowGroupName { SHOW_GROUP_OPTIONAL , SHOW_GROUP_REQUIRED , SHOW_GROUP_NONE , SHOW_GROUP_INHERIT } |
How to show group names in switch synopsis. More... | |
enum | SwitchSkipping { SKIP_NEVER , SKIP_WEAK , SKIP_STRONG } |
Whether to skip a switch. More... | |
enum | WhichValue { SAVE_NONE , SAVE_ONE , SAVE_LAST , SAVE_FIRST , SAVE_ALL , SAVE_AUGMENTED } |
Describes how to handle switches that occur multiple times. More... | |
Functions | |
std::ostream & | operator<< (std::ostream &, const Location &) |
Print a location. | |
std::ostream & | operator<< (std::ostream &, const ParsedValue &) |
Print some information about a parsed value. | |
template<typename T > | |
AnyParser< T >::Ptr | anyParser (T &storage) |
Factory for value parsers. | |
template<typename T > | |
AnyParser< T >::Ptr | anyParser () |
Factory for value parsers. | |
AnyParser< std::string >::Ptr | anyParser () |
Factory for value parsers. | |
template<typename T > | |
IntegerParser< T >::Ptr | integerParser (T &storage) |
Factory for value parsers. | |
template<typename T > | |
IntegerParser< T >::Ptr | integerParser () |
Factory for value parsers. | |
IntegerParser< int >::Ptr | integerParser () |
Factory for value parsers. | |
template<typename T > | |
NonNegativeIntegerParser< T >::Ptr | nonNegativeIntegerParser (T &storage) |
Factory for value parsers. | |
template<typename T > | |
NonNegativeIntegerParser< T >::Ptr | nonNegativeIntegerParser () |
Factory for value parsers. | |
NonNegativeIntegerParser< unsigned >::Ptr | nonNegativeIntegerParser () |
Factory for value parsers. | |
template<typename T > | |
PositiveIntegerParser< T >::Ptr | positiveIntegerParser (T &storage) |
Factory for value parsers. | |
template<typename T > | |
PositiveIntegerParser< T >::Ptr | positiveIntegerParser () |
Factory for value parsers. | |
PositiveIntegerParser< unsigned >::Ptr | positiveIntegerParser () |
Factory for value parsers. | |
template<typename T > | |
RealNumberParser< T >::Ptr | realNumberParser (T &storage) |
Factory for value parsers. | |
template<typename T > | |
RealNumberParser< T >::Ptr | realNumberParser () |
Factory for value parsers. | |
RealNumberParser< double >::Ptr | realNumberParser () |
Factory for value parsers. | |
template<typename T > | |
BooleanParser< T >::Ptr | booleanParser (T &storage) |
Factory for value parsers. | |
template<typename T > | |
BooleanParser< T >::Ptr | booleanParser () |
Factory for value parsers. | |
BooleanParser< bool >::Ptr | booleanParser () |
Factory for value parsers. | |
template<typename T > | |
EnumParser< T >::Ptr | enumParser (T &storage) |
Factory for value parsers. | |
template<typename T > | |
EnumParser< T >::Ptr | enumParser (std::vector< T > &storage) |
Factory for value parsers. | |
template<typename T > | |
EnumParser< T >::Ptr | enumParser (Optional< T > &storage) |
Factory for value parsers. | |
template<typename T > | |
EnumParser< T >::Ptr | enumParser (BitFlags< T > &storage) |
Factory for value parsers. | |
template<typename T > | |
EnumParser< T >::Ptr | enumParser () |
Factory for value parsers. | |
StringSetParser::Ptr | stringSetParser (std::string &storage) |
Factory for value parsers. | |
StringSetParser::Ptr | stringSetParser () |
Factory for value parsers. | |
ListParser::Ptr | listParser (const ValueParser::Ptr &, const std::string &sepRe="[,;:]\\s*") |
Factory for value parsers. | |
ShowVersion::Ptr | showVersion (const std::string &versionString) |
Factory for switch action. | |
ShowVersionAndExit::Ptr | showVersionAndExit (const std::string &versionString, int exitStatus) |
Factory for switch action. | |
ShowHelp::Ptr | showHelp () |
Factory for switch action. | |
ShowHelpAndExit::Ptr | showHelpAndExit (int exitStatus) |
Factory for switch action. | |
ConfigureDiagnostics::Ptr | configureDiagnostics (const std::string &, Message::Facilities &, bool exitOnHelp=true) |
Factory for switch action. | |
ConfigureDiagnosticsQuiet::Ptr | configureDiagnosticsQuiet (Message::Facilities &) |
Factory for switch action. | |
template<class Functor > | |
UserAction< Functor >::Ptr | userAction (const Functor &functor) |
Factory for switch action. | |
template<typename T > | |
Sum< T >::Ptr | sum () |
Factory for value agumenter. | |
Variables | |
const std::string | STR_NONE |
const Location | NOWHERE |
Indicates an invalid location. | |
typedef std::vector<ParsedValue> Sawyer::CommandLine::ParsedValues |
A vector of parsed values.
Definition at line 638 of file Sawyer/CommandLine.h.
typedef Container::Map<const SwitchGroup*, std::set<const Switch*> > Sawyer::CommandLine::GroupedSwitches |
Subset of switches grouped by their switch groups.
Definition at line 2742 of file Sawyer/CommandLine.h.
typedef Container::Map<std::string, GroupedSwitches> Sawyer::CommandLine::NamedSwitches |
Subset of switches indexed by their command-line representation.
Definition at line 2745 of file Sawyer/CommandLine.h.
The order in which things are sorted in the documentation.
Enumerator | |
---|---|
INSERTION_ORDER | Entities appear in the documentation in the same order they are inserted into the container. For instance, manual page sections will appear in the order of the Parser::doc calls, or switches are sorted within a switch group according to the order they were inserted into the group. |
DOCKEY_ORDER | Entities are sorted according to their documentation keys. Documentation keys, which default to lower-case entity names, are used to sort the entities within their container. This is the default. |
Definition at line 173 of file Sawyer/CommandLine.h.
Format of a switch string.
Enumerator | |
---|---|
CANONICAL | Switch strings that are qualified with the switch group name or which belong to a group that has no name. |
NONCANONICAL | |
ALL_STRINGS | The union of CANONICAL and NONCANONICAL. |
Definition at line 186 of file Sawyer/CommandLine.h.
How to show group names in switch synopsis.
Definition at line 194 of file Sawyer/CommandLine.h.
Whether to skip a switch.
Enumerator | |
---|---|
SKIP_NEVER | Treat the switch normally. |
SKIP_WEAK | Process switch normally, but also add to skipped list. |
SKIP_STRONG | Skip switch and its argument(s) without saving any value. |
Definition at line 202 of file Sawyer/CommandLine.h.
Describes how to handle switches that occur multiple times.
Definition at line 2010 of file Sawyer/CommandLine.h.
std::ostream & Sawyer::CommandLine::operator<< | ( | std::ostream & | , |
const Location & | |||
) |
Print a location.
Prints a location as the dotted pair idx.offset.
|
extern |
Indicates an invalid location.
The library uses this to indicate that a string came from somewhere other than the command-line. The constant NOWHERE
compares equal to itself but unequal to (less than) all valid locations.
Referenced by Sawyer::CommandLine::Switch::Switch(), Sawyer::CommandLine::SwitchArgument::SwitchArgument(), Sawyer::CommandLine::Switch::intrinsicValue(), Sawyer::CommandLine::Switch::intrinsicValue(), Sawyer::CommandLine::Switch::intrinsicValue(), and Sawyer::CommandLine::Switch::intrinsicValue().