#include <wvargs.h>
WvArgs allows you to specify a series of typed or callback-enabled command-line arguments. Once all of these arguments are specified, the WvArgs::process(..) function can be called to perform the actual argument processing.
Sample usage:
#include "wvargs.h" static void callback(void *userdata, WvStringParm value) { wvout->print("callback value = %s, userdata = %s\n", value, (const char *)userdata); } int main(int argc, char **argv) { WvString str_opt = "default"; bool bool_opt = false; int num_opt = 0; WvArgs args; args.add_option('s', "str", "Pass a string option", "string", str_opt); args.add_set_bool_option('b', "bool", "Set a boolean option", bool_opt); args.add_option('n', "num", "Pass a numeric option", "integer", num_opt); args.add_option('c', "callback", "Callback option", WvArgs::ArgCallback(callback), (void *)"demo"); WvStringList remaining_args; args.process(argc, argv, &remaining_args); wvout->print("str_opt=%s, bool_opt=%s, num_opt=%s\n", str_opt, bool_opt, num_opt); WvStringList::Iter i(remaining_args); for (i.rewind(); i.next(); ) wvout->print("rem: %s\n", *i); return 0; }
Definition at line 62 of file wvargs.h.
Public Types | |
typedef WvCallback< bool, void * > | NoArgCallback |
The callback type used for switches that do not take a parameter. | |
typedef WvCallback< bool, WvStringParm, void * > | ArgCallback |
The callback type used for switches that take a parameter It returns true if the switch was parsed correctly. | |
enum | flags_t { NO_EXIT_ON_ERRORS, FLAGS_SIZE } |
These flags control the behaviour of WvArgs. More... | |
Public Member Functions | |
WvArgs () | |
~WvArgs () | |
bool | process (int argc, char **argv, WvStringList *remaining_args=NULL) |
Process the command line arguments passed to main() using the options provided through calls to add_option(. | |
void | set_version (WvStringParm version) |
Set the --version string. | |
void | set_email (WvStringParm email) |
Set the e-mail address for bug reports. | |
void | set_help_header (WvStringParm header) |
Set the introductory help message, printed at the beginning of --help. | |
void | set_help_footer (WvStringParm footer) |
Set the descriptive help message, printed at the end of --help. | |
void | print_usage (int argc, char **argv) |
Output the short usage message based on the provided options. | |
void | print_help (int argc, char **argv) |
Output the long usage message based on the provided options. | |
void | add_set_bool_option (char short_option, WvStringParm long_option, WvStringParm desc, bool &val) |
Add a boolean option, which, when spefied, sets the specified boolean variable to true. | |
void | add_reset_bool_option (char short_option, WvStringParm long_option, WvStringParm desc, bool &val) |
Add a boolean option, which, when spefied, sets the specified boolean variable to false. | |
void | add_flip_bool_option (char short_option, WvStringParm long_option, WvStringParm desc, bool &val) |
Add a boolean option, which, when spefied, changes the value of the boolean variable from false to true or from true to false. | |
void | add_option (char short_option, WvStringParm long_option, WvStringParm desc, WvStringParm arg_desc, int &val) |
Add a switch that takes an integer argument. | |
void | add_option (char short_option, WvStringParm long_option, WvStringParm desc, WvStringParm arg_desc, long &val) |
Add a switch that takes a long argument. | |
void | add_option (char short_option, WvStringParm long_option, WvStringParm desc, WvStringParm arg_desc, float &val) |
Add a switch that takes a float argument. | |
void | add_option (char short_option, WvStringParm long_option, WvStringParm desc, WvStringParm arg_desc, double &val) |
Add a switch that takes a double argument. | |
void | add_option (char short_option, WvStringParm long_option, WvStringParm desc, WvStringParm arg_desc, WvString &val) |
Add a switch that takes a string argument. | |
void | add_option (char short_option, WvStringParm long_option, WvStringParm desc, WvStringParm arg_desc, WvStringList &val) |
Add a switch that takes a string argument; the argument is appended to a string list. | |
void | add_option (char short_option, WvStringParm long_option, WvStringParm desc, NoArgCallback cb, void *ud=NULL) |
Add a switch which does not take an argument which invokes a callback when it is specified. | |
void | add_option (char short_option, WvStringParm long_option, WvStringParm desc, WvStringParm arg_desc, ArgCallback cb, void *ud=NULL) |
Add a switch which takes an argument which invokes a callback when it is specified. | |
void | add_required_arg (WvStringParm desc) |
Add a required argument to the list of parameters. | |
void | add_optional_arg (WvStringParm desc, bool multiple=false) |
Add an optional argument to the list of parameters. | |
void | remove_option (char short_option) |
Remove an option by specifying its short form. | |
void | remove_option (WvStringParm long_option) |
Remove an option by specifying its long form. | |
void | remove_all_options () |
Remove all options. | |
void | zap () |
An alias for remove_all_options(). | |
bool | get_flag (const flags_t flag) const |
Get and set flags. | |
void | set_flag (const flags_t flag, const bool value) |
|
The callback type used for switches that do not take a parameter. It returns true if the switch was parsed correctly. |
|
These flags control the behaviour of WvArgs. By default, they are all set to false. |
|
Process the command line arguments passed to main() using the options provided through calls to add_option(. .). If remaining_args is provided, any remaining arguments after the command line switches will be appended to this list. Definition at line 775 of file wvargs.cc. References WvArgsData::argp(), WvArgsData::argp_build(), WvArgsData::args(), argp::args_doc, WvArgsData::flags, WvArgsData::parser(), and WvArgsData::self(). Referenced by WvDaemon::run(). |
|
Output the short usage message based on the provided options. Useful when a bad value is passed as the parameter of a switch. Definition at line 834 of file wvargs.cc. References WvArgsData::argp(). |
|
Add a boolean option, which, when spefied, sets the specified boolean variable to true.
Definition at line 847 of file wvargs.cc. References WvArgsData::add(), and WvArgsData::remove(). Referenced by WvDaemon::WvDaemon(). |
|
Add a boolean option, which, when spefied, sets the specified boolean variable to false.
Definition at line 855 of file wvargs.cc. References WvArgsData::add(), and WvArgsData::remove(). Referenced by WvDaemon::WvDaemon(). |
|
Add a boolean option, which, when spefied, changes the value of the boolean variable from false to true or from true to false.
Definition at line 863 of file wvargs.cc. References WvArgsData::add(), and WvArgsData::remove(). |
|
Add a switch that takes an integer argument.
Definition at line 879 of file wvargs.cc. References WvArgsData::add(), and WvArgsData::remove(). Referenced by WvDaemon::WvDaemon(). |
|
Add a switch that takes a long argument.
Definition at line 887 of file wvargs.cc. References WvArgsData::add(), and WvArgsData::remove(). |
|
Add a switch that takes a float argument.
Definition at line 895 of file wvargs.cc. References WvArgsData::add(), and WvArgsData::remove(). |
|
Add a switch that takes a double argument.
Definition at line 903 of file wvargs.cc. References WvArgsData::add(), and WvArgsData::remove(). |
|
Add a switch that takes a string argument.
Definition at line 911 of file wvargs.cc. References WvArgsData::add(), and WvArgsData::remove(). |
|
Add a switch that takes a string argument; the argument is appended to a string list.
Definition at line 920 of file wvargs.cc. References WvArgsData::add(), and WvArgsData::remove(). |
|
Add a switch which does not take an argument which invokes a callback when it is specified.
Definition at line 871 of file wvargs.cc. References WvArgsData::add(), and WvArgsData::remove(). |
|
Add a switch which takes an argument which invokes a callback when it is specified.
Definition at line 929 of file wvargs.cc. References WvArgsData::add(), and WvArgsData::remove(). |
|
Add a required argument to the list of parameters. WvArgs will return an error when run if it is not specified.
Definition at line 961 of file wvargs.cc. References WvArgsData::add_required_arg(), argp::args_doc, and WvArgsData::maximum_args. Referenced by add_optional_arg(). |
|
Add an optional argument to the list of parameters.
Definition at line 972 of file wvargs.cc. References add_required_arg(), argp::args_doc, WvArgsData::maximum_args, and WvArgsData::subtract_required_arg(). |
|
Remove an option by specifying its short form.
Definition at line 939 of file wvargs.cc. References WvFastString::null, and WvArgsData::remove(). |
|
Remove an option by specifying its long form.
Definition at line 945 of file wvargs.cc. References WvArgsData::remove(). |