(or rather Getopt mark III, since I've already rewritten the current
one, although no functionality was changed then)
I've been working a bit on a new Argument parser, and would like some
input. In reality there will be two different way of using the argument
parser, which really doesn't share any code but is convenient to do at
the same time. The first one is a "I just want to get this over with"
kind of interface:
void main(int num, array argv)
{
mapping args = Arg.parse(argv);
argv = args[Arg.REST];
}
The output will contain ([ "foo":1 ]) if the argument is --foo and ([
"foo":"bar" ]) if the argument is --foo=bar. There is no fancy stuff to
handle default values (just or it with another mapping) or options that
may or may not have values (e.g. -bar=foo will produce ([ "b":1, "a":1,
"r":"foo" ]) and --foo x --bar will just produce ([ "foo":1 ]) and then
stop processing the line).
The other mode is the (supposedly) easy yet powerful mode that you would
use for more mature applications.
class Parser
{
inherit Arg.Options;
Arg verbose = NoArg("-v")|NoArg("--verbose")|Env("VERBOSE");
Arg level = HasArg("-l")|HasArg("--level");
}
void main(int n, array argv)
{
Parser p = Parser(argv);
// Index magic. Some like it, some not so much.
if( p->verbose )
werror("Level %s\n", p->level);
}
Which is extended in
class Parser
{
// The name for now.
inherit Arg.OptionsSupreme;
Arg verbose = NoArg("-v")|NoArg("--verbose")|Env("VERBOSE");
// These are used in the automatically generated --help response.
string help_pre = "This program is like awesum.";
string verbose_help = "Increases the verbosity of the program.";
// Called when/if verbose is set to a value.
void verbose_cb(string arg)
{
werror("Verbosity is on.\n");
}
}
Does this match the long list of requirements that I've heard from
others?