Moclap is a prototype CLI argument parser for Mojo. This project was developed as an experiment to explore Mojo's reflection capabilities.
The core objective of this project was to implement a "struct-first" CLI parser that requires zero manual mapping, similar to clap in rust. By leveraging reflection, the parser inspects the provided configuration struct at compile time and generates the necessary logic to populate it. The config struct must be Defaultable to have defaults values.
The parser currently supports:
- Basic Types:
String,Bool,Int,UInt. - Fixed-width Integers:
Int8throughInt64,UInt8throughUInt64. - Floating Point:
Float16,Float32,Float64. - Boolean Toggles: Flags flip the default state of a boolean using bitwise negation (
~). - Help Generation: Automatically generates a help menu based on the struct definition and default values provided in
__init__.
from moclap import cli_parse
@fieldwise_init
struct Config(Copyable, Defaultable, Writable):
var name: String
var port: Int
var verbose: Bool
var timeout: Float64
fn __init__(out self):
self.name = "app"
self.port = 8080
self.verbose = False
self.timeout = 30.0
fn main() raises:
# Generates a parser specialized for the Config struct
var config = cli_parse[Config]()
# Use the parsed config
print(config)Passing arguments:
mojo example.mojo --name "my-server" --port 9000 --verboseViewing the auto-generated help:
mojo example.mojo --helpOutput:
Command Line Parser Help (-h or --help)
Usage: [options]
Options:
--name : String (default: app)
--port : Int (default: 8080)
--verbose : Bool (default: False)
--timeout : Float64 (default: 30.0)