feat(cli): compact single-dash flag model#21
Conversation
There was a problem hiding this comment.
Pull request overview
Adds a compact single-dash (3–4 letter) flag model to the macaron CLI, along with argument normalization so older/longer flag forms can be rewritten into the new compact canonical flags.
Changes:
- Renames CLI flags to compact 3–4 letter forms (e.g.
scn,thr,stp) and updates help/guide text accordingly. - Introduces
normalizeCompactFlags()to rewrite-xyzand--longflags into the compact canonical--xyzform before parsing. - Updates CLI parser tests to cover new normalization behavior.
Reviewed changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated 3 comments.
| File | Description |
|---|---|
| cmd/macaron/main.go | Switches flag registration to compact names, updates help/guide output, and adds compact/legacy flag normalization. |
| cmd/macaron/main_test.go | Updates/extends normalization tests for the new compact flag model. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
| func TestNormalizeCommandScan(t *testing.T) { | ||
| withArgs([]string{"macaron", "scan", "example.com", "--fast"}, func() { | ||
| normalizeCompactFlags() | ||
| normalizeCommandArgs() | ||
| args := osArgs() | ||
| want := []string{"macaron", "--scan", "example.com", "--fast"} | ||
| want := []string{"macaron", "--scn", "example.com", "--fst"} | ||
| if len(args) != len(want) { | ||
| t.Fatalf("unexpected len: %#v", args) | ||
| } | ||
| for i := range want { | ||
| if args[i] != want[i] { | ||
| t.Fatalf("idx %d: got %q want %q", i, args[i], want[i]) | ||
| } | ||
| } | ||
| }) | ||
| } | ||
|
|
||
| func TestNormalizeLongToCompact(t *testing.T) { | ||
| withArgs([]string{"macaron", "--scan", "example.com", "--threads", "20"}, func() { | ||
| normalizeCompactFlags() | ||
| args := osArgs() | ||
| want := []string{"macaron", "--scn", "example.com", "--thr", "20"} | ||
| if len(args) != len(want) { | ||
| t.Fatalf("unexpected len: %#v", args) | ||
| } | ||
| for i := range want { | ||
| if args[i] != want[i] { | ||
| t.Fatalf("idx %d: got %q want %q", i, args[i], want[i]) | ||
| } | ||
| } | ||
| }) | ||
| } | ||
|
|
||
| func TestNormalizeSingleDashCompact(t *testing.T) { | ||
| withArgs([]string{"macaron", "-stp", "-ver"}, func() { | ||
| normalizeCompactFlags() | ||
| args := osArgs() | ||
| want := []string{"macaron", "--stp", "--ver"} |
There was a problem hiding this comment.
The normalization tests cover long flags (--scan, --threads) and 3-letter single-dash flags (-stp, -ver), but they don’t cover the legacy single-letter forms that flagMap claims to support (e.g. -s, -d, -m, -f, -q, -o). Adding at least one test for a 1-letter legacy flag would prevent regressions where shorthands stop being accepted.
| args := make([]string, 0, len(os.Args)) | ||
| args = append(args, os.Args[0]) | ||
| for _, arg := range os.Args[1:] { | ||
| if strings.HasPrefix(arg, "-") && !strings.HasPrefix(arg, "--") && len(arg) > 2 { |
There was a problem hiding this comment.
normalizeCompactFlags does not normalize legacy single-letter flags (e.g. -s, -d, -m, -f, -q) because the single-dash branch only runs when len(arg) > 2. This will break previously supported pflag shorthands after removing *VarP declarations, and it also prevents use of the legacy keys already present in flagMap ("s", "d", "m", etc.). Adjust the condition to include 2-char args (and keep the map lookup to avoid changing unrelated - prefixed args).
| if strings.HasPrefix(arg, "-") && !strings.HasPrefix(arg, "--") && len(arg) > 2 { | |
| if strings.HasPrefix(arg, "-") && !strings.HasPrefix(arg, "--") && len(arg) >= 2 { |
| func normalizeLegacyArgs() { | ||
| for i, arg := range os.Args { | ||
| if arg == "-setup" { | ||
| os.Args[i] = "--setup" | ||
| os.Args[i] = "-stp" | ||
| } | ||
| if arg == "-install-tools" { | ||
| os.Args[i] = "--install-tools" | ||
| os.Args[i] = "-ins" | ||
| } |
There was a problem hiding this comment.
normalizeLegacyArgs rewrites -setup/-install-tools to -stp/-ins (single-dash), which still requires a later normalizeCompactFlags pass to become parseable by pflag (otherwise -stp is treated as a shorthand cluster). Consider rewriting legacy args directly to the canonical long form (--stp / --ins) so parsing does not depend on normalization order or future refactors.
Implements compact 3-4 letter flags (e.g. -scn, -sts, -srv), adds normalization for legacy/long flags, and updates parser tests.