Skip to content

Instantly share code, notes, and snippets.

@TheWaWaR
Last active May 15, 2025 08:04
Show Gist options
  • Save TheWaWaR/66dbec84e4f8e89990a69908d7bf1b2b to your computer and use it in GitHub Desktop.
Save TheWaWaR/66dbec84e4f8e89990a69908d7bf1b2b to your computer and use it in GitHub Desktop.
Command Line Argument Parser for MoonBit
struct Argument {
name: String
short: Option[Char]
nargs: Nargs
positional: Bool
/// If this value is empty means not limit to those value
choices: ArrayView[String]
default: ArrayView[String]
help: String
}
enum Nargs {
Any
/// n == 0 (flag argument)
None
/// n == 1
One
/// n == X
Fixed(UInt)
/// n >= X
AtLeast(UInt)
/// n <= X
AtMost(UInt)
/// X <= n < Y
Range(UInt, UInt)
}
struct SubCommand {
name: String
args: Array[Argument]
subcmds: Array[SubCommand]
help: String
}
struct Parser {
/// The program name
prog: String
args: Array[Argument]
subcmds: Array[SubCommand]
description: String
}
pub fn Parser::parse(args: ArrayView[String]) -> Value {
Value::{
args: @hashmap.new(),
flags: @hashmap.new(),
positional_args: [],
subcmd: None
}
}
struct Value {
args: @hashmap.T[String, String]
flags: @hashmap.T[String, Bool]
positional_args: Array[String]
subcmd: Option[Value]
}
struct SubCommandValue {
name: String
v: Value
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment