Parse command line arguments supporting a variety of flag formats.
In your project's zz.toml
file, add the following:
[repos]
zargv = "git://github.com/jwerle/zargv.git"
list = "git://github.com/jwerle/zz-list.git" ## peer dep
[dependencies]
zargv = "*"
using zargv::{ ArgumentOptions }
using zargv
fn main(int argc, char **argv) -> int {
int mut num = 0;
bool mut help = false;
bool mut version = false;
char * mut name;
char * mut array[3];
new args = zargv::parser();
args.int(&num, ArgumentOptions { name: "num", alias: "n" });
args.bool(&help, ArgumentOptions { name: "help", alias: "h" });
args.bool(&version, ArgumentOptions { name: "version", alias: "V" });
args.string(&name, ArgumentOptions { name: "name", alias: "N" });
args.array(&array, ArgumentOptions { name: "array", alias: "A" });
args.parse(argv, argc);
return 0;
}
Initializes a new Parser
with tail.
new+4 args = zargv::parser();
Creates an int
argument from pointer and options.
int mut num = 3000; // 3000 is the default value
args.int(&num, ArgumentOptions {
name: "port", alias: "p"
})
Creates an uint
argument from pointer and options.
uint mut num = 3000; // 3000 is the default value
args.int(&num, ArgumentOptions {
name: "port", alias: "p"
})
Creates a float
argument from pointer and options.
float mut num = 0.0;
args.float(&num, ArgumentOptions {
name: "num"
})
Creates a double
argument from pointer and options.
double mut num = 0.0;
args.doublr(&num, ArgumentOptions {
name: "num"
})
Creates a bool
argument from pointer and options.
bool mut boolean = false;
args.float(&boolean ArgumentOptions {
name: "boolean"
})
Creates a string
argument from pointer and options.
char mut *host = 0;
args.string(&host, ArgumentOptions {
name: "host"
});
Creates an array
argument from pointer and options. A max number of items
to add to this array must be given with options.max
. A type must be
specified otherwise a string type is assumed.
char * mut names[3];
args.array(&array, ArgumentOptions {
name: "name"
});
Creates a rest
argument from pointer and options where all arguments
after the --
in a argument list are added to this pointer. A max number of
items must be given with options.max
. The default is 1
. This function
assumes a pointer to a char *[]
array that can hold at least options.max
items.
char * mut rest[3];
args.array(&rest, ArgumentOptions {
max: 3
});
Creates a container for unknown arguments to be mapped to. A given pointer
must be large enough to hold options.max
unknown arguments.
char * mut unknown[3];
args.array(&unkown, ArgumentOptions {
max: 3
});
Parses argc
arguments in char **argv
mapping values to argument pointers
specified by the various argument schema functions.
// `argv` and `argc` from `main()`
args.parse(argv, argc);
MIT