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
[core] Add allow_negative_expressions() method for Command (#48)
This PR adds a new `Command.allow_negative_expressions()` mode to treat
dash-prefixed mathematical expressions (and similar tokens) as
positional arguments, expanding ArgMojo’s handling of “negative-looking”
inputs beyond numeric literals.
**Changes:**
- Introduce `_allow_negative_expressions` flag +
`allow_negative_expressions()` builder on `Command`.
- Add parsing logic to consume certain `-...` tokens as positionals in
both `parse_arguments()` and `parse_known_arguments()`.
- Add documentation and tests describing/validating the new behavior.
Copy file name to clipboardExpand all lines: docs/changelog.md
+2Lines changed: 2 additions & 0 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -4,6 +4,8 @@ This document tracks all notable changes to ArgMojo, including new features, API
4
4
5
5
<!--
6
6
Unreleased changes should be commented out from here. This file will be edited just before each release to reflect the final changelog for that version. Otherwise, the users would be confused.
7
+
8
+
- Add `allow_negative_expressions()` on `Command` — treats single-hyphen tokens as positional arguments when they don't conflict with registered short options. Handles mathematical expressions like `-1/3*pi`, `-sin(2)`, `-e^2`. Superset of `allow_negative_numbers()`.
|`allow_negative_numbers()`| Parser behavior flag |
1308
+
|`allow_negative_expressions()`| Parser behavior flag (superset of allow_negative_numbers) |
1309
+
|`add_parent()`| Cross-command inheritance |
1307
1310
1308
1311
I think this is the right call — these features describe *relationships between* arguments or *command-level* behavior, not individual argument metadata. Trying to force them into struct field attributes would create a confusing, non-composable API.
**Approach 4: `allow_negative_expressions()` (expressions and arbitrary tokens)**
2632
+
2633
+
When your CLI accepts mathematical expressions that start with `-` (e.g. `-1/3*pi`, `-sin(2)`, `-e^2`), `allow_negative_numbers()` is not enough because those tokens are not pure numeric literals. Call `allow_negative_expressions()` to treat any single-hyphen token as a positional argument, provided it doesn't conflict with a registered short option.
2634
+
2635
+
```mojo
2636
+
var command = Command("calc", "Expression calculator")
calc -e # expr = "-e" (because -e is not a registered short option)
2646
+
calc -p 10 hello # precision = "10", expr = "hello" (-p IS registered, so it's parsed as the -p short option taking 10 as its value)
2647
+
```
2648
+
2649
+
Rules:
2650
+
2651
+
- A single-hyphen token is treated as a positional **only when its first character after `-` does not match a registered short option**.
2652
+
- Examples: `-1/3*pi`, `-sin(2)`, and `-e` are positional if `-1`, `-s`, and `-e` are not registered short options.
2653
+
- If the first character **is** a registered short option, the token is parsed normally (merged shorts like `-vp` and attached values like `-p10` continue to work).
2654
+
- Long options (`--foo`) are never affected — they always parse normally.
2655
+
2656
+
> **Note:**`allow_negative_expressions()` is a superset of `allow_negative_numbers()`. You don't need to call both.
|**Works on options**| No (positionals only) | Yes (also value-taking options like `--file`) |
2683
+
|**Parsing behavior**| Enables dash-prefixed expression handling for positionals | Broadens one argument to accept hyphen-prefixed values |
2684
+
2685
+
With a **single positional**, the two are nearly interchangeable for inputs like `-1/pi*sin(3)` — and a bare `-` is already treated as a positional token in both cases (it never enters short-option parsing). `allow_hyphen_values()` is still the better fit when one specific argument should accept arbitrary hyphen-prefixed values, especially for value-taking options.
2686
+
2687
+
Choose `allow_negative_expressions()` when:
2688
+
2689
+
- Your command has **multiple positionals** and you want a single switch for all of them.
2690
+
- You want to signal intent: "this CLI handles math expressions."
2691
+
2692
+
Choose `allow_hyphen_values()` when:
2693
+
2694
+
- Only **one specific argument** should accept dash-prefixed values while others should not.
2695
+
- You need the bare `-` (stdin/stdout convention).
2696
+
- The argument is an **option** (`--file`), not a positional.
2638
2697
2639
2698
---
2640
2699
2641
2700
**What is NOT a number**
2642
2701
2643
-
Tokens like `-1abc`, `-e5`, or `-1-2` are not valid numeric patterns. They will still be parsed as short-option strings and may raise "Unknown option" errors if unregistered.
2702
+
Tokens like `-1abc`, `-e5`, or `-1-2` are not valid numeric patterns. Without `allow_negative_expressions()`, they will still be parsed as short-option strings and may raise "Unknown option" errors if unregistered. With `allow_negative_expressions()`, they are consumed as positional arguments.
0 commit comments