ArgParser

command line argument parser

utils
command line
parser
moon add Yoorkin/ArgParser@0.2.1
Download zip
Author
Version
0.2.1
License
Apache-2.0
Last updated
6 months ago
Downloads
102K

Dependencies

README

#ArgParser

A simple command-line parser for MoonBit. It uses a declarative style to specify the command-line interface.

#Usage

using @ArgParser {parse}
let verbose : Ref[Bool] = @ref.new(false)
let output : Ref[String] = @ref.new("output")
let files : Array[String] = []
let spec : Array[(String, String, Spec, String)] = [
("--verbose", "-v", Set(verbose), "enable verbose message"),
("--output", "-o", Set_string(output), "output file name"),
]
let usage =
#| Simple CLI tool
#| usage:
#| mytool [options] <file1> [<file2>] ... -o <output>
#|

///|
test {
let argv = ["-o", "out.mbt", "file1", "file2", "--verbose"]
parse(spec, file => files.push(file), usage, argv)
inspect(verbose.val, content="true")
inspect(output.val, content="out.mbt")
inspect(files[0], content="file1")
inspect(files[1], content="file2")
}

#help options

ArgParser will automatically generate --help and -h options.

///|
test {
let argv = ["--help"]
parse(spec, file => files.push(file), usage, argv) catch {
// errors raised from callbacks
@ArgParser.ErrorMsg(msg) => println(msg)
// errors raised from callbacks
e => println(e)
}
}

#Related Libraries

Looking for more powerful CLI tool? Check out clap.

#
ErrorMsg

pub suberror ErrorMsg String

#
Spec

pub(all) enum Spec {
Unit(() -> Unit raise)
String((String) -> Unit raise)
Set_string(Ref[String])
Set(Ref[Bool])
Clear(Ref[Bool])
}

Matched option handler that used to interpret options.

Unit - handle with callback

String - handle associated value with callback

Set_string - set option to associated value

Set - set reference to true

Clear - set reference to false

#
parse

fn parse(speclist : Array[(String, String, Spec, String)], rest : (String) -> Unit raise, usage_msg : String, argv : Array[String]) -> Unit raise

Parse argument list for CLI tools

Match OptionName or LongOptionName then interpret with Spec and use Description to generate -h or --help options message.

Parameters

  • speclist: (LongOptionName, OptionName, Spec, Description) of Array
  • rest: rest arguments handler
  • usage_msg: synopsis usage
  • argv: input argument list

Exception

If the input arguments contains help option or invalid input, the parse function will raise ErrorMsg error.

Source Files

Powered by MoonBit

Site sourceReport issuePackagesBuild queueSkillsStatistics

© 2026 mooncakes.io