You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
[cli] Allow negative numbers + add range, value name, argument group validation + custom usage line (#201)
This PR enhances the `decimo` CLI calculator’s argument parsing and
help/usage UX by leveraging newer ArgMojo features, and updates project
documentation/plans accordingly.
**Changes:**
- Add ArgMojo metadata to CLI options/flags (value names, numeric range
validation, help groups) and set a custom usage line.
- Enable ArgMojo negative-number passthrough for positional expressions.
- Update docs/plans to reflect ArgMojo v0.5.0 integration and adjust
pixi tasks for fetching ArgMojo source.
1.**Basic arithmetic + High-precision + Large integers + Pipeline** (Phase 1) — These are the raison d'être of `decimo`. Decimo already provides arbitrary-precision `BigDecimal`; wiring up tokenizer → parser → evaluator gives immediate value. Pipeline/batch is nearly free once one-shot works (just loop over stdin lines).
34
34
2.**Built-in math functions** (Phase 2) — `sqrt`, `ln`, `exp`, `sin`, `cos`, `tan`, `root` already exist in the Decimo API. Adding them mostly means extending the tokenizer/parser to recognize function names.
35
-
3.**Interactive REPL + Variables/State** (Phase 3) — Valuable for exploration, but requires a read-eval-print loop, `ans` tracking, named variable storage and session-level precision management. More engineering effort, less urgency.
36
-
4.**Unit conversion / Matrix / Symbolic** (Phase 4) — Out of scope. `decimo` is a numerical calculator, not a CAS or unit library. These can be revisited if there is demand.
35
+
3.**Polish & ArgMojo integration** (Phase 3) — Error diagnostics, edge-case handling, and exploiting ArgMojo v0.5.0 features (shell completions, argument groups, numeric range validation, etc.). Mostly CLI UX refinement.
36
+
4.**Interactive REPL + Subcommands** (Phase 4) — Requires a read-eval-print loop, `ans` tracking, named variable storage, session-level precision management, and CLI restructuring with subcommands. More engineering effort, less urgency.
37
+
5.**Future enhancements** (Phase 5) — CJK full-width detection, response files, unit conversion, matrix, symbolic. Out of scope for now.
37
38
38
39
## Usage Design
39
40
@@ -161,15 +162,23 @@ Best for: interactive exploration, multi-step calculations, experimenting with p
161
162
162
163
### Layer 1: ArgMojo — CLI Argument Parsing
163
164
164
-
ArgMojo handles the outer CLI structure. No modifications to ArgMojo are needed.
165
+
ArgMojo handles the outer CLI structure via its struct-based declarative API (`Parsable` trait).
165
166
166
167
```mojo
167
-
var cmd = Command("decimo", "Arbitrary-precision CLI calculator.", version="0.1.0")
| 3.5 | Shell completion (`--completions bash\|zsh\|fish`) | ✓ | Built-in — zero code; needs documentation in user manual and README |
321
+
| 3.6 |`allow_negative_numbers()` to allow pure negative numbers | ✓ | Explicit opt-in in hybrid bridge; `decimo "-3"` works, expressions need quoting or `--`|
322
+
| 3.7 | Numeric range on `precision`| ✓ |`has_range=True, range_min=1, range_max=1000000000`; rejects `--precision 0` or `-5`|
323
+
| 3.8 | Value names for help readability | ✓ |`--precision <N>`, `--delimiter <CHAR>`, `--rounding-mode <MODE>`|
324
+
| 3.9 | Argument groups in help output | ✓ |`Computation` and `Formatting` groups in `--help`|
325
+
| 3.10 | Custom usage line | ✓ |`Usage: decimo [OPTIONS] <EXPR>`|
326
+
| 3.11 |`Parsable.run()` override | ✗ | Move eval logic into `DecimoArgs.run()` for cleaner separation |
| 3.13 | Documentation (user manual for CLI) | ✗ |`docs/user_manual_cli.md`; include shell completion setup |
329
+
| 3.14 | Build and distribute as single binary | ✗ ||
330
+
| 3.15 | Allow negative expressions | ✗ | This needs ArgMojo to regard arguments with a hyphen and followed by more than one letter as a positional argument |
331
+
332
+
### Phase 4: Interactive REPL & Subcommands
333
+
334
+
1. Restructure CLI with subcommands: `decimo eval "expr"` (default), `decimo repl`, `decimo help functions`.
335
+
2. Persistent flags (`--precision`, `--scientific`, etc.) across subcommands.
| 4.1 | Subcommand restructure | ✗ |`decimo eval "expr"` (default), `decimo repl`, `decimo help functions`; use `subcommands()` hook |
365
+
| 4.2 | Persistent flags across subcommands | ✗ |`precision`, `--scientific`, etc. as `persistent=True`; both `decimo repl -p 100` and `decimo -p 100 repl` work |
366
+
| 4.3 |`parse_full()` for subcommand dispatch | ✗ | Typed struct + `ParseResult.subcommand` for dispatching to eval/repl/help handlers |
Once ArgMojo v0.2.0 lands in pixi, apply the following changes to `decimo`:
358
-
359
-
#### 1. Auto-show help when no positional arg is given
360
-
361
-
ArgMojo v0.2.0 automatically displays help when a required positional argument is missing — no code change needed on our side. Remove the `.required()` guard if it interferes, or verify the behaviour works out of the box.
362
-
363
-
**Current (v0.1.x):** missing `expr` prints a raw error.
364
-
**After:** missing `expr` prints the full help text.
365
-
366
-
#### 2. Shell-quoting tips via `add_tip()`
367
-
368
-
Replace the inline description workaround with ArgMojo's dedicated `add_tip()` API. Tips render as a separate section at the bottom of `--help` output.
369
-
370
-
```mojo
371
-
cmd.add_tip('If your expression contains *, ( or ), wrap it in quotes:')
372
-
cmd.add_tip(' decimo "2 * (3 + 4)"')
373
-
cmd.add_tip('Or use noglob: noglob decimo 2*(3+4)')
374
-
cmd.add_tip("Or add to ~/.zshrc: alias decimo='noglob decimo'")
375
-
```
376
-
377
-
Remove the corresponding note that is currently embedded in the `Command` description string.
378
-
379
-
#### 3. Negative number passthrough
380
-
381
-
Enable `allow_negative_numbers()` so that expressions like `decimo -3+4` or `decimo -3.14` are treated as math, not as unknown CLI flags.
382
-
383
-
```mojo
384
-
cmd.allow_negative_numbers()
385
-
```
386
-
387
-
#### 4. Rename `Arg` → `Argument`
388
-
389
-
`Arg` is kept as an alias in v0.2.0, so this is optional but recommended for consistency with the new API naming.
390
-
391
-
```mojo
392
-
# Before
393
-
from argmojo import Arg, Command
394
-
# After
395
-
from argmojo import Argument, Command
396
-
```
397
-
398
-
#### 5. Colored error messages from ArgMojo
399
-
400
-
ArgMojo v0.2.0 produces ANSI-colored stderr errors for its own parse errors (e.g., unknown flags). Our custom `display.mojo` colors still handle calculator-level errors. Verify that both layers look consistent (same RED styling).
401
-
402
-
#### 6. Subcommands (Phase 4 REPL prep)
403
-
404
-
Although not needed immediately, the new `add_subcommand()` API could later support:
0 commit comments