|
|
This is another simple command-line parser.
This one takes references to the variables which you want to fill from the command line, and
For example, if you take an input file & output file, some integer, and a boolean flag, you might declare these variables:
QString infile("foo.in"), outfile("foo.out"); int its = 10; bool hose = false; |
and register the arguments (and their descriptions) like this:
OtherSimpleCLParser args; args.add("i filename", infile, "the input file"); args.add("o filename", outfile, "the output file"); args.add("it iterations", its, "number of times to iterate"); args.add("h", hose, "hose things up"); ... args.parse(argc, argv); |
If there were errors, a usage message was generated & printed, and the program was terminated. You can do additional checking afterwards:
if (infile == outfile) args.usage("infile == outfile, you bozo"); |
When you're all done checking the command line arguments, let the OtherSimpleCLParser object go out of scope, or call its dispose() method. (The variables you gave it will continue to contain the values which were passed on the command line, of course.)
OtherSimpleCLParser ()
| OtherSimpleCLParser |
~OtherSimpleCLParser ()
| ~OtherSimpleCLParser |
template | add |
Adds a command-line flag to the list of arguments which will be looked for.
Parameters:
flag | Either just the flag, or the flag, a space, and some indication of what argument is expected (like "f filename" or "s name"). |
val | A reference to the variable which will be set to the value read from the command line. |
descr | A description of the command-line flag. This will be used in the message generated by usage(), if usage() is called. |
required | If this is true, then failing to pass the flag on the command line will cause usage() to be called. |
template | addPositional |
Adds a positional command-line argument to the list of arguments which will be looked for.
Note that all required positional arguments must precede optional positional arguments. Also, a positional argument type which takes multiple arguments (like QStringList) will cause everything after it to never be received, because all the arguments will go into the QStringList. (OK, rereading that, it doesn't even make sense to me. Sorry!)
Parameters:
name | some indication of what argument is expected (like "filename" or "name"). |
val | A reference to the variable which will be set to the value read from the command line. |
descr | A description of the command-line flag. This will be used in the message generated by usage(), if usage() is called. |
required | If this is true, then failing to pass the flag on the command line will cause usage() to be called. |
void parse (int argc, char **argv)
| parse |
Parses the given arguments and puts their values into the variables passed to add(). If there are errors, usage() is called.
void usage (const QString &msg = QString())
| usage |
Prints a formatted usage message to cerr and terminates the program. If a specific message is supplied, it's printed first.
void dispose ()
| dispose |
Cleans up the storage allocated by calls to add(). Call this when you're done using the OtherSimpleCLParser (that is, after the last point when you'd call usage()), or just let it go out of scope.
void assertGood (const QString &key, const QString &descr)
| assertGood |
Generated by: stephan on cheyenne on Mon Aug 11 14:06:52 2003, using kdoc 2.0a54. |