ArgMojo — A CLI argument parser for Mojo, ready for use 🔥

Hey Magicans and Pythonistas!

I’m sharing ArgMojo, a command-line argument parser for Mojo, inspired by Python’s argparse, Rust’s clap, and Go’s cobra.

Version Mojo pixi User manual

If you’ve ever used Python’s argparse, Rust’s clap, or Go’s cobra, you know how essential a good arg parser is for any CLI tool. I’m working on a CLI-based Chinese character search engine and a CLI calculator for Decimo, so I needed one for Mojo — and that’s how ArgMojo was born.

Shell tab-completion powered by ArgMojo

Demo: Shell tab-completion powered by ArgMojo

Installation

Available on modular-community:


pixi add argmojo

Available on Github release page (argmojo.mojopkg)

What it does

ArgMojo provides a builder-pattern API for defining and parsing CLI arguments. Core features include:

Parsing — long/short options, boolean flags, positional args, defaults, required args, -- stop marker, short flag merging (-abc), attached short values (-ofile), prefix matching (--verb--verbose)

Value handling — choices validation, count flags (-vvv), negatable flags (--color / --no-color), append/collect (--tag x --tag y), value delimiter (--env a,b,c), multi-value nargs, key-value maps, numeric range validation

Groups — mutually exclusive, required-together, one-required, conditional requirements

Subcommands — hierarchical commands with nesting (e.g., git remote add), persistent (global) flags, auto-registered help subcommand

Help & UX — auto-generated --help with pixi-style ANSI colours, --version, metavar, hidden args, aliases, deprecated args, custom tips

Interactive.prompt() to ask for missing values, .password() for masked input, confirmation_option() for --yes/-y skip-confirmation

Shell completiongenerate_completion("bash"|"zsh"|"fish") produces tab-completion scripts

CJK support — CJK-aware 2-column-wide help alignment, full-width auto-correction (--verbose--verbose), CJK punctuation detection (——verbose--verbose)

Advanced — argument parents (shared definitions across commands), default-if-no-value / require-equals, remainder positional, partial parsing (parse_known_arguments()), allow hyphen values (stdin - convention), response files (@args.txt)

Validation — compile-time StringLiteral parameters validated at mojo build time; registration-time validation catches group constraint typos when the program starts

Demo

The repo ships with two complete example CLIs — a simulated grep (single command, showcasing all value/group features) and a simulated git (12 subcommands with nesting) — so you can see everything in action.

Here’s what ./mgit clone looks like:


The design is inspired by argparse/clap/cobra, but adapted to fit Mojo’s current capabilities. It is just structs and builder methods. I did a cross-language survey of some major CLI libraries, so the feature set is deliberate. The builder API is now fully parameterised with compile-time StringLiteral parameters.

Your feedback are very welcome. Just raise an issue if you find any bug or have any suggestion. Thanks for very much :smiley:

9 Likes

This is wonderful, arg parsing is a super-critical basis for all cli tools, great job!

1 Like

Awesome work on ArgMojo excellent CLI framework please keep up with the spirit your developments are splendid Forfudan.

1 Like

ArgMojo is updated to v0.4.0, compatible with Mojo v0.26.2.