Imagine we're building a utility for joining MP3 files, something like MP3cat.
We want the user to supply the file names as a list of command line arguments.
We also want to support an --out/-o
option so the user can specify an output filename and a --quiet/-q
flag for turning down the program's verbosity.
let mut parser = ArgParser::new() .helptext("Usage: mp3cat...") .version("1.0") .flag("quiet q") .option("out o", "output.mp3");
That's it, we're done specifying our interface. Now we can parse the program's arguments:
if let Err(err) = parser.parse() { err.exit(); }
This will exit with a suitable error message for the user if something goes wrong. Now we can recover our filenames from the parser:
for filename in parser.args { do_stuff(); }
We can also check if the --quiet
flag was found:
if parser.found("quiet") { do_stuff(); }
And determine our output filepath:
let filepath = parser.value("out");
The default value "output.mp3"
will be returned if the user hasn't specified a custom value for
the --out
option.