Sign in

    #clap.mbt

    Command Line Argument Parser for MoonBit

    #Features

    • Support flag/named/positional arguments
    • Support limit arguments length
    • Support argument choices/default values
    • Support global argument
    • Support env variables as default value
    • Support infinite level of sub-command
    • Support custom value type
    • Support generate clap-rs style help output
    • When named argument length full filled immediately finish current argument

    #Usage

    Here is a simple example:
    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)

    Generate the help message:
    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)

    See more examples: parser tests

    BasicValue

    pub(open) trait BasicValue {
    parse(input : String) -> Self raise
    StrConvError

    }

    impl BasicValue for Int
    impl BasicValue for Int64
    impl BasicValue for UInt

    Value

    pub(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 = _
    }

    ParserError

    pub(all) suberror ParserError {
    InvalidArgumentName(String)
    InvalidSubCommandName(String)
    InvalidPositionalAsNamed(String)
    InvalidArgumentValue(String)
    InvalidArgumentValueLength(String)
    TooManyArgs(String)
    InvalidSpec(String)
    }

    impl Show for ParserError

    Arg

    pub(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)
    }

    Arg::choices

    fn Arg::choices(self : Arg) ->
    HashSet
    [String]?

    Arg::common

    fn Arg::common(self : Arg) -> CommonFields

    Arg::defaults

    fn Arg::defaults(self : Arg) -> ArrayView[String]

    Arg::flag

    fn Arg::flag(short? : Char, store? : Bool, global? : Bool, env_var? : String, help? : String) -> Arg

    Create a flag argument

    Arg::is_global

    fn Arg::is_global(self : Arg) -> Bool

    Arg::named

    fn Arg::named(short? : Char, nargs? : Nargs, choices? :
    HashSet
    [String], defaults? : ArrayView[String], global? : Bool, value_delimiter? : String, env_var? : String, help? : String) -> Arg

    Create a named argument

    Arg::nargs

    fn Arg::nargs(self : Arg) -> Nargs?

    Arg::positional

    fn Arg::positional(nargs? : Nargs, choices? :
    HashSet
    [String], defaults? : ArrayView[String], value_delimiter? : String, env_var? : String, help? : String) -> Arg

    Create a positional argument

    Arg::short

    fn Arg::short(self : Arg) -> Char?

    Arg::value_delimiter

    fn Arg::value_delimiter(self : Arg) -> String?

    CommonFields

    pub struct CommonFields {
    env_var : String
    help : String
    }

    Nargs

    pub(all) enum Nargs {
    Any
    One
    Fixed(UInt)
    AtLeast(UInt)
    AtMost(UInt)
    Range(UInt, UInt)
    }

    impl Eq for Nargs
    impl Show for Nargs

    Nargs::is_exceeded

    fn Nargs::is_exceeded(self : Nargs, length : UInt) -> Bool

    Check if the length exceeded the max length

    Nargs::is_valid_length

    fn Nargs::is_valid_length(self : Nargs, length : UInt) -> Bool

    Check if the length is constrained by Nargs

    Parser

    pub(all) struct Parser {
    prog : String
    args : Map[String, Arg]
    subcmds : Map[String, SubCommand]
    description : String
    }

    Parser::gen_help_message

    fn Parser::gen_help_message(self : Parser, subcmd_chains : Array[String], global_args : Map[String, Arg]) -> String

    Generate the help message

    Parser::new

    fn Parser::new(prog? : String, args? : Map[String, Arg], subcmds? : Map[String, SubCommand], description? : String) -> Parser

    Create the argument parser specification

    Parser::parse

    fn[V : Value] Parser::parse(self : Parser, value : V, cli_args : ArrayView[String], env_vars? : Map[String, String]) -> String? raise ParserError

    Parse the command line arguments

    SimpleValue

    pub(all) struct SimpleValue {
    name : String
    args :
    HashMap
    [String, Array[String]]
    flags :
    HashMap
    [String, Bool]
    positional_args : Array[String]
    subcmd : SimpleValue?
    }

    impl Eq for SimpleValue
    impl Show for SimpleValue

    SimpleValue::get_array

    fn[T : BasicValue] SimpleValue::get_array(self : SimpleValue, name : String) -> Array[T] raise ParserError

    Get multiple values by the argument name

    SimpleValue::get_flag

    fn SimpleValue::get_flag(self : SimpleValue, name : String) -> Bool?

    SimpleValue::get_one

    fn[T : BasicValue] SimpleValue::get_one(self : SimpleValue, name : String) -> T raise ParserError

    Get exactly one value by the argument name

    SimpleValue::get_option

    fn[T : BasicValue] SimpleValue::get_option(self : SimpleValue, name : String) -> T? raise ParserError

    Get one/zero value by the argument name

    SimpleValue::get_positional

    fn[T : BasicValue] SimpleValue::get_positional(self : SimpleValue) -> Array[T] raise ParserError

    Get the positional argument values

    SimpleValue::new

    fn SimpleValue::new(name : String) -> SimpleValue

    Create a simple value to hold the parsed result

    SubCommand

    pub(all) struct SubCommand {
    args : Map[String, Arg]
    subcmds : Map[String, SubCommand]
    help : String
    }

    SubCommand::new

    fn SubCommand::new(args? : Map[String, Arg], subcmds? : Map[String, SubCommand], help? : String) -> SubCommand

    Create a subcommand specification

    Powered by MoonBit

    Site sourceReport issuePackagesBuild queueSkillsStatistics

    © 2026 mooncakes.io