Command Line Argument Parser
let parser = Parser::new(subcmds={
"subcmd1": SubCommand::new(
args={ "arg1": Arg::flag(short='a', help="Argument 1") },
help="Test subcommand",
),
})
let value = SimpleValue::new(parser.prog)
let help_message = parser.parse!(value, ["subcmd1", "--arg1"])
assert_eq!(help_message, None)
assert_eq!(value.name, "PROG")
assert_eq!(value.args.length(), 0)
assert_eq!(value.flags.is_empty(), true)
assert_eq!(value.positional_args.is_empty(), true)
assert_eq!(value.subcmd.unwrap().flags.get("arg1").unwrap(), true)let value = SimpleValue::new(parser.prog)
let help_message = parser.parse!(value, ["--help"]).unwrap()
let expected_help_message =
#|Usage: PROG [OPTIONS] <COMMAND>
#|
#|Commands:
#| subcmd1 Test subcommand
#|
#|Options:
#| -h, --help Print help
#|
assert_eq!(help_message, expected_help_message)impl BasicValue for Intimpl BasicValue for Int64impl BasicValue for UIntimpl BasicValue for UInt64impl BasicValue for Doubleimpl BasicValue for Stringpub(open) trait Value {
set_flag(Self, name : String, value : Bool) -> Unit raise ParserError = _
add_value(Self, name : String, value : String, positional : Bool) -> Unit raise ParserError = _
select_subcmd(Self, subcmd : String) -> Self raise ParserError = _
}pub(all) suberror ParserError {
InvalidArgumentName(String)
InvalidSubCommandName(String)
InvalidPositionalAsNamed(String)
InvalidArgumentValue(String)
InvalidArgumentValueLength(String)
TooManyArgs(String)
InvalidSpec(String)
}impl Show for ParserErrorpub(all) enum Arg {
Flag(Char, Bool, Bool, CommonFields)
Named(Char, Nargs, HashSet[String], ArrayView[String], Bool, String, CommonFields)
Positional(Nargs, HashSet[String], ArrayView[String], String, CommonFields)
}pub(all) enum Nargs {
Any
One
Fixed(UInt)
AtLeast(UInt)
AtMost(UInt)
Range(UInt, UInt)
}pub(all) struct Parser {
prog : String
args : Map[String, Arg]
subcmds : Map[String, SubCommand]
description : String
}pub(all) struct SimpleValue {
name : String
args : HashMap[String, Array[String]]
flags : HashMap[String, Bool]
positional_args : Array[String]
subcmd : SimpleValue?
}impl Value for SimpleValuefn add_value(self : SimpleValue, name : String, value : String, positional : Bool) -> Unit raise ParserErrorimpl Eq for SimpleValueimpl Show for SimpleValuefn[T : BasicValue] SimpleValue::get_array(self : SimpleValue, name : String) -> Array[T] raise ParserErrorfn[T : BasicValue] SimpleValue::get_option(self : SimpleValue, name : String) -> T? raise ParserErrorpub(all) struct SubCommand {
args : Map[String, Arg]
subcmds : Map[String, SubCommand]
help : String
}fn SubCommand::new(args? : Map[String, Arg], subcmds? : Map[String, SubCommand], help? : String) -> SubCommandCommand Line Argument Parser