parrotcode: parse long and short command line options | |
Contents | Libraries |
library/Getopt/Obj.pir - parse long and short command line options
.sub main :main .param pmc argv
.local string prog_name prog_name = shift argv load_bytecode "Getopt/Obj.pir" .local pmc getopts getopts = new "Getopt::Obj" getopts."notOptStop"(1) # these two are identical, with the exception of the call to name push getopts, "foo|f:s" $P0 = getopts."add"() $P0."name"("FooBar") $P0."long"("foo") $P0."short"("f") $P0."optarg"(1) $P0."type"('String') # these two are identical push getopts, "bar|b=s" $P0 = getopts."add"() $P0."long"("bar") $P0."short"("b") $P0."type"('String') .local pmc opts opts = getopts."get_options"(argv) .local string foo foo = opts["Foobar"] .end
This library parses options from the command line. Options may take and of these forms, currently:
--foo=bar Set foo to bar -f bar Set f to bar (with conditions) -fthis=that If f is a Hash, set key 'this' to 'that' -f this=that If f is a Hash, set key 'this' to 'that' -f Set f (if it's Boolean or has an optional argument) -fbar Set f to bar if it's not a Boolean -abc If -a -b and -c are all Booleans, set them all --foo=this=that If foo is a Hash, set key 'this' to 'that' Both equal signs are required -- Stop processing args, -- is removed from argv
__load()
Our nice little module.
init()
HASH get_options(ARRAY ARGV)
__push_string(STRING format)
push getopts, "foo|f=s"
. The format is as such.
Boolean
.
Boolean
.
Boolean
.
String
. Use i
for Integer
, f
for Float
, @
for an Array
, and %
for a Hash
.
String
, with "optarg" set to a true value.Getopt::Obj::Spec add()
STRING getNameForKey(STRING key)
INT self."notOptStop"()
INT self."notOptStop"(INT val)
MissingRequired(STRING arg)
Interal use only, at least don't do any new "Getopt::Obj::Spec" yourself... This makes an easy holder for each possible match.
init()
Boolean
.
STRING self."name"()
STRING self."name"(STRING val)
STRING self."long"()
STRING self."long"(STRING val)
STRING self."short"()
STRING self."short"(STRING val)
INT self."type"()
INT self."type"(INT val)
Boolean
-f
. Groups of Boolean
's tied together, such as -foobar
, if -f
, -o
, -b
, -a
, and -r
are all Boolean
values, then each will be set. If -f
is a Boolean
but one of the others is not, an exception is thrown.For a long argument, it takes the form --foo
. Usage such as --foo=this
or --foo=
are allowed, but it just sets it to true, so no difference.
String
--foo=bar
, -f bar
, or -fbar
.
Integer
String
.
Float
Array
-I./include -I./src/include
for example, I
will be an array of ./include
and ./src/include
. If only used once, it's a one sized array.
Hash
-Dfoo=bar
, -Dfoo
, and --define=foo=bar
(both equal signs required).INT self."optarg"()
INT self."optarg"(INT val)
bar
in --foo=bar
, is not mandatory. In -fbar
, -f
is set to bar
. In -f bar
it is NOT set.argv
and return
.Boolean
. Should it be an error?obj."type"()
.Not positive this is a bug... And relates to the -f -b issue where if the argument to -f is required, it will be given '-b'.
Boolean
options. So my little "perl pie" annoyance isn't handled, perl -pie "code"
, although perl doesn't handle it in a DWIM manner...And it doesn't have good error correction....
Joshua Isom - loneowl@ritalin.shout.net
The library/Getopt/Long.pir Parrot library.
Copyright (C) 2006-2008, The Perl Foundation.
|