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()
| Parameter | Type | Required | Description |
|---|---|---|---|
name | String | Yes | Program name, shown in help text and usage line. |
description | String | No | One-line description shown at the top of help. |
version | String | No | Version string printed by --version. |
parse() vs parse_arguments()
command.parse()reads the real command-line viasys.argv().command.parse_arguments(args)accepts aList[String]— useful for testing without a real binary. Note thatargs[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:
| Method | Returns | Description |
|---|---|---|
result.get_flag("name") | Bool | Returns True if the flag was set, else False. |
result.get_string("name") | String | Returns the string value. Raises if not found. |
result.get_int("name") | Int | Parses the value as an integer. Raises on error. |
result.get_float("name") | Float64 | Parses the value as a floating-point number. Raises on error. |
result.get_count("name") | Int | Returns 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") | Bool | Returns True if a value is available (including a default). |
result.was_provided("name") | Bool | Returns True only if the user really supplied it. |
result.print_summary() | None | Prints 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.