template <typename ResultStruct>
class ArgsParser
Defined at line 115 of file ../../zircon/system/ulib/cmdline/include/lib/cmdline/args_parser.h
ArgsParser -----------------------------------------------------------
Parses a command line into a struct and a vector of string parameters.
Public Methods
void ArgsParser<ResultStruct> ()
Defined at line 117 of file ../../zircon/system/ulib/cmdline/include/lib/cmdline/args_parser.h
void ~ArgsParser<ResultStruct> ()
Defined at line 118 of file ../../zircon/system/ulib/cmdline/include/lib/cmdline/args_parser.h
void ArgsParser<ResultStruct> (const ArgsParser<ResultStruct> & )
disallow copy and assign
Defined at line 121 of file ../../zircon/system/ulib/cmdline/include/lib/cmdline/args_parser.h
ArgsParser<ResultStruct> & operator= (const ArgsParser<ResultStruct> & )
Defined at line 122 of file ../../zircon/system/ulib/cmdline/include/lib/cmdline/args_parser.h
void AddSwitch (const char * long_name, const char short_name, const char * help, bool ResultStruct::* value)
Presence detector for flags that have no values ("--enable-foo"). It sets
a boolean to true if the parameter is present on the command line. The
structure should default the boolean to false to detect a set. If a value
is present for the switch ("--enable-foo=bar") it will give an error.
If the default for the bool is true, the user can disable a boolean
switch by prefixing the long name with "no", as in: "--nofoo".
Example
struct MyOptions {
bool foo_set = false;
};
ArgsParser
<MyOptions
> parser;
parser.AddSwitch("foo", 'f', kFooHelp,
&MyOptions
::foo_set);
Defined at line 137 of file ../../zircon/system/ulib/cmdline/include/lib/cmdline/args_parser.h
void AddSwitch (const char * long_name, const char short_name, const char * help, std::optional<std::string> ResultStruct::* value, StringCallback validator)
Sets a std::optional with the value if the parameter is present. The
value will be required.
If the optional validator lambda is given, it will be called with the
string value for the parameter, and invalid values can be rejected by
returning a cmdline::Status::Error("Your message"); otherwise return
cmdline::Status::Ok().
Example
struct MyOptions {
std::optional
<std
::string> foo;
};
ArgsParser
<MyOptions
> parser;
parser.AddSwitch("foo", 'f', kFooHelp,
&MyOptions
::foo);
For optional values of other types, use the cmdline::Optional type, in
"lib/cmdline/optional.h".
Defined at line 161 of file ../../zircon/system/ulib/cmdline/include/lib/cmdline/args_parser.h
template <typename T>
void AddSwitch (const char * long_name, const char short_name, const char * help, T ResultStruct::* value, StringCallback validator)
Sets a command-line option of any type streamable to an iostream (via
operator ">>") with the value, if the parameter is present. The value
will be required.
If the optional validator lambda is given, it will be called with the
string value for the parameter, and invalid values can be rejected by
returning a cmdline::Status::Error("Your message"); otherwise return
cmdline::Status::Ok().
Example
struct MyOptions {
size_t foo; // Note the type could be int, double, std::string, ...
};
ArgsParser
<MyOptions
> parser;
parser.AddSwitch("foo", 'f', kFooHelp,
&MyOptions
::foo);
Defined at line 217 of file ../../zircon/system/ulib/cmdline/include/lib/cmdline/args_parser.h
template <typename T>
void AddSwitch (const char * long_name, const char short_name, const char * help, std::vector<T> ResultStruct::* value, StringCallback validator, char delimiter)
Collects a list of any type streamable to an iostream (via operator ">>")
with the value passed with this flag. This allows multiple flag
invocations. For example "-f foo -f bar" would produce a vector
{ "foo", "bar" }
If the optional validator lambda is given, it will be called with the
string value for the parameter, and invalid values can be rejected by
returning a cmdline::Status::Error("Your message"); otherwise return
cmdline::Status::Ok().
If the optional delimiters are given, the list can be passed in as a single
string split by delimiters. Whitespace will be trimmed from the ends of the values.
For example with the delimiter "," the argument "-f foo,bar" would produce a vector
{ "foo", "bar" }
Example
struct MyOptions {
std::vector
<std
::string> foo; // Note the type could be int, double, ...
};
ArgsParser
<MyOptions
> parser;
parser.AddSwitch("foo", 'f', kFooHelp,
&MyOptions
::foo);
Defined at line 269 of file ../../zircon/system/ulib/cmdline/include/lib/cmdline/args_parser.h
Status Parse (int argc, const char *const[] argv, ResultStruct * options, std::vector<std::string> * params)
Parses the given command line, returning the success.
Example
MyOptions options;
std::vector
<std
::string> params;
Status status = parser.Parse(argc, argv,
&options
,
¶ms
);
if (status.has_error())
error and return>
<use
options and params>
Defined at line 316 of file ../../zircon/system/ulib/cmdline/include/lib/cmdline/args_parser.h