WvStreams
wvargs.h
1 /* -*- Mode: C++ -*-
2  * Copyright (C) 2004-2005 Net Integration Technologies, Inc.
3  *
4  * WvStreams interface for command-line argument processing
5  */
6 #ifndef __WVARGS_H
7 #define __WVARGS_H
8 
9 #include "wvstring.h"
10 #include "wvstringlist.h"
11 #include "wvtr1.h"
12 
13 class WvArgsOption;
14 class WvArgsData;
15 
61 class WvArgs
62 {
63 public:
64 
69  typedef wv::function<bool(void*)> NoArgCallback;
74  typedef wv::function<bool(WvStringParm, void*)> ArgCallback;
75 
76 private:
77 
78  WvArgsData *data;
79  WvString args_doc;
80  WvString version;
81  WvString email;
82  WvString header;
83  WvString footer;
84 
85 public:
86 
87  WvArgs();
88  ~WvArgs();
89 
96  bool process(int argc, char **argv,
97  WvStringList *remaining_args = NULL);
98 
100  void set_version(WvStringParm version);
101 
103  void set_email(WvStringParm email);
104 
106  void set_help_header(WvStringParm header);
107 
109  void set_help_footer(WvStringParm footer);
110 
115  void print_usage(int argc, char **argv);
119  void print_help(int argc, char **argv);
120 
134  void add_set_bool_option(char short_option, WvStringParm long_option,
135  WvStringParm desc, bool &val);
149  void add_reset_bool_option(char short_option, WvStringParm long_option,
150  WvStringParm desc, bool &val);
164  void add_flip_bool_option(char short_option, WvStringParm long_option,
165  WvStringParm desc, bool &val);
166 
180  void add_option(char short_option, WvStringParm long_option,
181  WvStringParm desc, WvStringParm arg_desc, int &val);
195  void add_option(char short_option, WvStringParm long_option,
196  WvStringParm desc, WvStringParm arg_desc, long &val);
210  void add_option(char short_option, WvStringParm long_option,
211  WvStringParm desc, WvStringParm arg_desc, float &val);
225  void add_option(char short_option, WvStringParm long_option,
226  WvStringParm desc, WvStringParm arg_desc, double &val);
240  void add_option(char short_option, WvStringParm long_option,
241  WvStringParm desc, WvStringParm arg_desc, WvString &val);
256  void add_option(char short_option, WvStringParm long_option,
257  WvStringParm desc, WvStringParm arg_desc,
258  WvStringList &val);
273  void add_option(char short_option, WvStringParm long_option,
274  WvStringParm desc, NoArgCallback cb, void *ud = NULL);
291  void add_option(char short_option, WvStringParm long_option,
292  WvStringParm desc, WvStringParm arg_desc,
293  ArgCallback cb, void *ud = NULL);
294 
301  void add_required_arg(WvStringParm desc, bool multiple = false);
307  void add_optional_arg(WvStringParm desc, bool multiple = false);
308 
315  void remove_option(char short_option);
322  void remove_option(WvStringParm long_option);
323 
327  void remove_all_options();
331  void zap()
332  {
334  }
335 
338  enum flags_t
339  {
340  NO_EXIT_ON_ERRORS, // Do not exit when an error is encountered
341  FLAGS_SIZE // Number of flags that exist
342  };
343 
347  bool get_flag(const flags_t flag) const;
348  void set_flag(const flags_t flag, const bool value);
349 
350 };
351 
352 #endif // __WVARGS_H
WvArgs::ArgCallback
wv::function< bool(WvStringParm, void *)> ArgCallback
The callback type used for switches that take a parameter It returns true if the switch was parsed co...
Definition: wvargs.h:74
WvArgsOption
Definition: wvargs.cc:22
WvArgs::set_version
void set_version(WvStringParm version)
Set the –version string.
Definition: wvargs.cc:819
WvArgs::add_option
void add_option(char short_option, WvStringParm long_option, WvStringParm desc, WvStringParm arg_desc, int &val)
Add a switch that takes an integer argument.
Definition: wvargs.cc:888
WvArgs::add_set_bool_option
void add_set_bool_option(char short_option, WvStringParm long_option, WvStringParm desc, bool &val)
Add a boolean option, which, when specified, sets the specified boolean variable to true.
Definition: wvargs.cc:856
WvArgs::get_flag
bool get_flag(const flags_t flag) const
Get and set flags.
Definition: wvargs.cc:990
WvArgs::remove_all_options
void remove_all_options()
Remove all options.
Definition: wvargs.cc:960
WvArgs::add_flip_bool_option
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 tr...
Definition: wvargs.cc:872
WvArgs::add_reset_bool_option
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.
Definition: wvargs.cc:864
WvArgs::flags_t
flags_t
These flags control the behaviour of WvArgs.
Definition: wvargs.h:338
WvString
WvString is an implementation of a simple and efficient printable-string class.
Definition: wvstring.h:329
WvArgs
WvArgs - Sane command-line argument processing for WvStreams.
Definition: wvargs.h:61
WvArgs::set_email
void set_email(WvStringParm email)
Set the e-mail address for bug reports.
Definition: wvargs.cc:825
WvArgs::set_help_header
void set_help_header(WvStringParm header)
Set the introductory help message, printed at the beginning of –help.
Definition: wvargs.cc:831
WvArgs::set_help_footer
void set_help_footer(WvStringParm footer)
Set the descriptive help message, printed at the end of –help.
Definition: wvargs.cc:837
WvArgsData
Definition: wvargs.cc:53
WvArgs::process
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_o...
Definition: wvargs.cc:784
WvArgs::zap
void zap()
An alias for remove_all_options()
Definition: wvargs.h:331
WvArgs::add_required_arg
void add_required_arg(WvStringParm desc, bool multiple=false)
Add a required argument to the list of parameters.
Definition: wvargs.cc:966
WvArgs::add_optional_arg
void add_optional_arg(WvStringParm desc, bool multiple=false)
Add an optional argument to the list of parameters.
Definition: wvargs.cc:982
WvStringList
This is a WvList of WvStrings, and is a really handy way to parse strings.
Definition: wvstringlist.h:27
WvArgs::print_usage
void print_usage(int argc, char **argv)
Output the short usage message based on the provided options.
Definition: wvargs.cc:843
WvArgs::remove_option
void remove_option(char short_option)
Remove an option by specifying its short form.
Definition: wvargs.cc:948
WvArgs::print_help
void print_help(int argc, char **argv)
Output the long usage message based on the provided options.
Definition: wvargs.cc:850
WvArgs::NoArgCallback
wv::function< bool(void *)> NoArgCallback
The callback type used for switches that do not take a parameter.
Definition: wvargs.h:69