Skip to content

Commit 6db272c

Browse files
committed
[docs] Update the documents
1 parent 0d3e072 commit 6db272c

File tree

7 files changed

+43
-36
lines changed

7 files changed

+43
-36
lines changed

LICENSE

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
21
Apache License
32
Version 2.0, January 2004
43
http://www.apache.org/licenses/

README.md

Lines changed: 30 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -12,43 +12,47 @@ A command-line argument parser library for [Mojo](https://www.modular.com/mojo).
1212
1313
## Overview
1414

15-
ArgMojo provides a builder-pattern API for defining and parsing command-line arguments in Mojo. It supports:
16-
17-
[x] **Long options**: `--verbose`, `--output file.txt`, `--output=file.txt`
18-
[x] **Short options**: `-v`, `-o file.txt`
19-
[x] **Boolean flags**: options that take no value
20-
[x] **Positional arguments**: matched by position
21-
[x] **Default values**: fallback when an argument is not provided
22-
[x] **Required arguments**: validation that mandatory args are present
23-
[x] **Auto-generated help**: `--help` / `-h` (no need to implement manually)
24-
[x] **Version display**: `--version` / `-V` (also auto-generated)
25-
[x] **`--` stop marker**: everything after `--` is treated as positional
26-
[x] **Short flag merging**: `-abc` expands to `-a -b -c`
27-
[x] **Attached short values**: `-ofile.txt` means `-o file.txt`
28-
[x] **Choices validation**: restrict values to a set (e.g., `json`, `csv`, `table`)
29-
[x] **Metavar**: custom display name for values in help text
30-
[x] **Hidden arguments**: exclude internal args from `--help` output
31-
[x] **Count flags**: `-vvv``get_count("verbose") == 3`
32-
[x] **Positional arg count validation**: reject extra positional args
33-
[x] **Negatable flags**: `--color` / `--no-color` paired flags with `.negatable()`
34-
[x] **Mutually exclusive groups**: prevent conflicting flags (e.g., `--json` vs `--yaml`)
35-
[x] **Required-together groups**: enforce that related flags are provided together (e.g., `--username` + `--password`)
36-
[x] **Long option prefix matching**: allow abbreviated options (e.g., `--verb``--verbose`). If the prefix is ambiguous (e.g., `--ver` could match both `--verbose` and `--version`), an error is raised.
15+
ArgMojo provides a builder-pattern API for defining and parsing command-line arguments in Mojo. It currently supports:
16+
17+
- **Long options**: `--verbose`, `--output file.txt`, `--output=file.txt`
18+
- **Short options**: `-v`, `-o file.txt`
19+
- **Boolean flags**: options that take no value
20+
- **Positional arguments**: matched by position
21+
- **Default values**: fallback when an argument is not provided
22+
- **Required arguments**: validation that mandatory args are present
23+
- **Auto-generated help**: `--help` / `-h` (no need to implement manually)
24+
- **Version display**: `--version` / `-V` (also auto-generated)
25+
- **`--` stop marker**: everything after `--` is treated as positional
26+
- **Short flag merging**: `-abc` expands to `-a -b -c`
27+
- **Attached short values**: `-ofile.txt` means `-o file.txt`
28+
- **Choices validation**: restrict values to a set (e.g., `json`, `csv`, `table`)
29+
- **Metavar**: custom display name for values in help text
30+
- **Hidden arguments**: exclude internal args from `--help` output
31+
- **Count flags**: `-vvv``get_count("verbose") == 3`
32+
- **Positional arg count validation**: reject extra positional args
33+
- **Negatable flags**: `--color` / `--no-color` paired flags with `.negatable()`
34+
- **Mutually exclusive groups**: prevent conflicting flags (e.g., `--json` vs `--yaml`)
35+
- **Required-together groups**: enforce that related flags are provided together (e.g., `--username` + `--password`)
36+
- **Long option prefix matching**: allow abbreviated options (e.g., `--verb``--verbose`). If the prefix is ambiguous (e.g., `--ver` could match both `--verbose` and `--version`), an error is raised.
3737

3838
---
3939

4040
I created this project to support my experiments with a CLI-based Chinese character search engine in Mojo, as well as a CLI-based calculator for [DeciMojo](https://github.com/forfudan/decimojo). It is inspired by Python's `argparse`, Rust's `clap`, Go's `cobra`, and other popular argument parsing libraries, but designed to fit Mojo's unique features and constraints.
4141

42+
My goal is to provide a Mojo-idiomatic argument parsing library that can be easily adopted by the growing Mojo community for their CLI applications. **Before Mojo v1.0** (which means it gets stable), my focus is on building core features and ensuring correctness. "Core features" refer to those who appear in `argparse`/`clap`/`cobra` and are commonly used in CLI apps. "Correctness" means that the library should handle edge cases properly, provide clear error messages, and have good test coverage. Some fancy features will depend on my time and interest.
43+
4244
## Installation
4345

44-
ArgMojo requires Mojo >= 0.26.1 and uses [pixi](https://pixi.sh) for environment management.
46+
ArgMojo requires Mojo == 0.26.1 and uses [pixi](https://pixi.sh) for environment management.
4547

4648
```bash
4749
git clone https://github.com/forfudan/argmojo.git
4850
cd argmojo
4951
pixi install
5052
```
5153

54+
I make the Mojo version strictly 0.26.1 because that's the version I developed and tested on, and Mojo is rapidly evolving. Based on my experience, the library will not work every time there's a new Mojo release.
55+
5256
## Quick Start
5357

5458
Here is a simple example of how to use ArgMojo in a Mojo program. The full example can be found in `examples/demo.mojo`.
@@ -58,7 +62,7 @@ from argmojo import Arg, Command
5862
5963
6064
fn main() raises:
61-
var cmd = Command("sou", "A CJK-aware text search tool", version="0.1.0")
65+
var cmd = Command("demo", "A CJK-aware text search tool that supports Pinyin and Yuhao Input Methods (宇浩系列輸入法).", version="0.1.0")
6266
6367
# Positional arguments
6468
cmd.add_arg(Arg("pattern", help="Search pattern").positional().required())
@@ -213,7 +217,7 @@ Some arguments are excluded from `--help` but still work at the command line (us
213217
### A mock example showing how features work together
214218

215219
```bash
216-
./demo yes ./src --li --color --no-color --usern zhu --pas 12345
220+
./demo yes ./src --verbo --color -li -d 3 --no-color --usern zhu --pas 12345
217221
```
218222

219223
This will be parsed as:
@@ -264,7 +268,7 @@ argmojo/
264268
│ ├── command.mojo # Command struct (parsing logic)
265269
│ └── result.mojo # ParseResult struct (parsed values)
266270
├── tests/
267-
│ └── test_argmojo.mojo # Tests
271+
│ └── test_argmojo.mojo # Tests
268272
├── pixi.toml # pixi configuration
269273
├── .gitignore
270274
├── LICENSE

docs/argmojo_overall_planning.md

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -156,7 +156,7 @@ fn main() raises:
156156
cmd.add_arg(Arg("path", help="Search path").positional().default("."))
157157
158158
# Optional arguments
159-
cmd.add_arg(Arg("ling", help="Use Yuho Lingming encoding").long("ling").short("l").flag())
159+
cmd.add_arg(Arg("ling", help="Use Yuhao Lingming encoding").long("ling").short("l").flag())
160160
cmd.add_arg(Arg("ignore-case", help="Case insensitive search").long("ignore-case").short("i").flag())
161161
cmd.add_arg(Arg("max-depth", help="Maximum directory depth").long("max-depth").short("d").takes_value())
162162
@@ -200,7 +200,7 @@ pattern # By order of add_arg() calls
200200
- [x] Establish module structure
201201
- [x] Implement `Arg` struct and builder methods
202202
- [x] Implement basic `Command` struct
203-
- [x] Iimplement a small demo CLI tool to test the library
203+
- [x] Implement a small demo CLI tool to test the library
204204

205205
### Phase 2: Parsing Enhancements ✓
206206

@@ -213,7 +213,7 @@ pattern # By order of add_arg() calls
213213
- [x] **`count` action**`-vvv``get_count("verbose") == 3` (argparse `-v` counting)
214214
- [x] **Clean exit for --help/--version** — use `sys.exit(0)` instead of `raise Error`
215215

216-
### Phase 3: Relationships & Validation (for v0.1)
216+
### Phase 3: Relationships & Validation (for v0.2)
217217

218218
- [x] **Mutually exclusive flags**`cmd.mutually_exclusive(["json", "yaml", "toml"])`
219219
- [x] **Flags required together**`cmd.required_together(["username", "password"])`
@@ -224,7 +224,7 @@ pattern # By order of add_arg() calls
224224
- [ ] **Aliases** for long names — `.aliases(["colour"])` for `--color`
225225
- [ ] **Deprecated arguments**`.deprecated("Use --format instead")` prints warning (argparse 3.13)
226226

227-
### Phase 4: Subcommands (maybe for v0.2)
227+
### Phase 4: Subcommands (maybe for v0.3)
228228

229229
- [ ] **Subcommand support**`app <subcommand> [args]` (cobra, argparse, clap)
230230
- [ ] **Subcommand help**`app help <subcommand>` or `app <subcommand> --help`
@@ -239,7 +239,7 @@ pattern # By order of add_arg() calls
239239

240240
### Explicitly Out of Scope
241241

242-
These will **NOT** be implemented:
242+
These will **NOT** be implemented (but who knows :D maybe in the future if there's demand):
243243

244244
- Derive/decorator-based API (no macros in Mojo)
245245
- Shell completion script generation
@@ -279,9 +279,11 @@ Input: ["demo", "yuhao", "./src", "--ling", "-i", "--max-depth", "3"]
279279
6. Return ParseResult
280280
```
281281

282-
## 8. Mojo 0.26.1 Notes
282+
## 8. Notes on Mojo versions
283283

284-
Here are some important Mojo-specific patterns used throughout this project. Mojo is rapidly evolving, so these may need to be updated in the future:
284+
Here are some important Mojo-specific patterns used throughout this project. Mojo is rapidly evolving, so these may need to be updated in the future.
285+
286+
These are all worthy being checked in [Mojo Miji](https://mojo-lang.com/miji) too.
285287

286288
| Pattern | What & Why |
287289
| ---------------------- | --------------------------------------------------- |

pixi.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ channels = [
55
"https://repo.prefix.dev/modular-community",
66
"conda-forge",
77
]
8-
description = "A command-line argument parser library for Mojo, inspired by Rust's clap"
8+
description = "A command-line argument parser library for Mojo"
99
license = "Apache-2.0"
1010
name = "argmojo"
1111
platforms = ["osx-arm64", "linux-64"]

src/argmojo/arg.mojo

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ struct Arg(Copyable, Movable, Stringable, Writable):
77
Use the builder pattern to configure the argument:
88
99
```mojo
10+
from argmojo import Arg
1011
var arg = Arg("output", help="Output file path").long("output").short("o").takes_value()
1112
```
1213
"""

src/argmojo/command.mojo

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ struct Command(Stringable, Writable):
1212
Example:
1313
1414
```mojo
15+
from argmojo import Command, Arg
1516
var cmd = Command("myapp", "A sample application")
1617
cmd.add_arg(Arg("verbose", help="Enable verbose output").long("verbose").short("v").flag())
1718
var result = cmd.parse()

tests/test_argmojo.mojo

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
"""Testss for argmojo — command-line argument parser."""
1+
"""Tests for argmojo — command-line argument parser."""
22

33
from testing import assert_true, assert_false, assert_equal, TestSuite
44
from argmojo import Arg, Command, ParseResult

0 commit comments

Comments
 (0)