Keyboard shortcuts

Press ← or → to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

ArgMojo — User Manual

Comprehensive guide to every feature of the ArgMojo command-line argument parser.

All code examples below assume that you have imported the mojo at the top of your mojo file:

from argmojo import Argument, Command

Getting Started

Creating a Command

A Command is the top-level object that holds argument definitions and runs the parser.

def main() raises:
    var command = Command("myapp", "A short description of the program", version="1.0.0")
    # ... add arguments ...
    var result = command.parse()
ParameterTypeRequiredDescription
nameStringYesProgram name, shown in help text and usage line.
descriptionStringNoOne-line description shown at the top of help.
versionStringNoVersion string printed by --version.

parse() vs parse_arguments()

  • command.parse() reads the real command-line via sys.argv().
  • command.parse_arguments(args) accepts a List[String] — useful for testing without a real binary. Note that args[0] is expected to be the program name and will be skipped, so the actual arguments should start from index 1.

Reading Parsed Results

After calling command.parse() or command.parse_arguments(), you get a ParseResult with these typed accessors:

MethodReturnsDescription
result.get_flag("name")BoolReturns True if the flag was set, else False.
result.get_string("name")StringReturns the string value. Raises if not found.
result.get_int("name")IntParses the value as an integer. Raises on error.
result.get_float("name")Float64Parses the value as a floating-point number. Raises on error.
result.get_count("name")IntReturns the count (0 if never provided).
result.get_list("name")List[String]Returns collected values (empty list if none).
result.get_map("name")Dict[String, String]Returns key-value pairs (empty dict if none).
result.has("name")BoolReturns True if a value is available (including a default).
result.was_provided("name")BoolReturns True only if the user really supplied it.
result.print_summary()NonePrints a human-readable summary of all parsed values.

get_string() works for both named options and positional arguments — positional values are looked up by the name given in Argument("name", ...).

var result = command.parse()

# Flags
if result.get_flag("verbose"):
    print("Verbose mode on")

# String values
var output = result.get_string("output")

# Integer values
var depth = result.get_int("max-depth")

# Count flags
var verbosity = result.get_count("verbose")

# List (append) values
var tags = result.get_list("tag")  # List[String]

# Check presence
if result.has("output"):
    print("Output was specified:", result.get_string("output"))

# Tell a typed value apart from a default one
if result.was_provided("format"):
    print("The user chose:", result.get_string("format"))

The two presence checks are not the same. has() tells you that a value is there to be read, and that includes a value that came from .default(). was_provided() is narrower: it is True only for a value the user typed on the command line, entered at an interactive prompt, or received through an implies() rule, and False for a default. Group constraints go through was_provided(), which is why a default never sets off a conflict on its own.