Print usage only for a command called wrongly (closes #41) #86

Merged
clawbot merged 1 commits from issue-41-usage-only-for-call-errors into next 2026-10-04 12:08:07 +02:00
Collaborator

Fixes #41: a failed command printed the whole usage text after its error, which buried the error. A wrong passphrase and an unreadable vault looked alike.

The root command now has a PersistentPreRunE that turns usage off for the command being run. Cobra runs that hook after it has parsed the flags and checked the number of arguments. So wrong arity, an unknown flag and a bad flag value still get usage, and an error from running the command gets none. Cobra checks required flags and flag groups (mutually exclusive, required together, one required) only after the hook, so the hook checks both first: secret import x without --source still gets usage.

Cobra still prints the error, once. Entry() is unchanged, so the order stays: print the error, purge memory, exit with code 1.

The new test runs each kind of wrong call plus one failure while running (secret get x with no vault). It checks that the command fails, that its error appears exactly once, and that usage appears only for the wrong calls.

  • Deviation: root SilenceUsage: true, which the issue suggests, is not used. In cobra v1.9.1, argument and flag errors take the same path as errors from running the command, so it would hide usage for them too.
  • Judgement call: a missing required flag or a broken flag group counts as a flag error, so it keeps usage.
  • Unverified: no command uses flag groups yet, so the test does not cover that case.
  • Trap: a subcommand that defines its own PersistentPreRun replaces the root one, and would get usage after every failure again. No subcommand does today.

Model: opus-5-5

Fixes https://git.eeqj.de/sneak/secret/issues/41: a failed command printed the whole usage text after its error, which buried the error. A wrong passphrase and an unreadable vault looked alike. The root command now has a `PersistentPreRunE` that turns usage off for the command being run. Cobra runs that hook after it has parsed the flags and checked the number of arguments. So wrong arity, an unknown flag and a bad flag value still get usage, and an error from running the command gets none. Cobra checks required flags and flag groups (mutually exclusive, required together, one required) only after the hook, so the hook checks both first: `secret import x` without `--source` still gets usage. Cobra still prints the error, once. `Entry()` is unchanged, so the order stays: print the error, purge memory, exit with code 1. The new test runs each kind of wrong call plus one failure while running (`secret get x` with no vault). It checks that the command fails, that its error appears exactly once, and that usage appears only for the wrong calls. - Deviation: root `SilenceUsage: true`, which the issue suggests, is not used. In cobra v1.9.1, argument and flag errors take the same path as errors from running the command, so it would hide usage for them too. - Judgement call: a missing required flag or a broken flag group counts as a flag error, so it keeps usage. - Unverified: no command uses flag groups yet, so the test does not cover that case. - Trap: a subcommand that defines its own `PersistentPreRun` replaces the root one, and would get usage after every failure again. No subcommand does today. Model: opus-5-5
clawbot added the needs-review label 2026-10-04 10:02:25 +02:00
clawbot self-assigned this 2026-10-04 10:02:25 +02:00
Author
Collaborator

FAIL: needs rework.

  1. internal/cli/root.go lines 51-66, the PersistentPreRunE hook and its comment: the comment says cobra has checked the arguments and flags before the hook runs, except for required flags. Cobra v1.9.1 also checks flag groups (flags marked mutually exclusive, required together, or one required) only after the hook. A wrong call that breaks one of those would print its error without usage, and the comment tells the next reader that cannot happen. No command uses flag groups today, so nothing misbehaves now, but the comment is wrong and hides that gap. Acceptable: the hook also calls cmd.ValidateFlagGroups() after cmd.ValidateRequiredFlags(), and the comment names both. Make the same sentence in the TODO.md entry and the commit message match.

Note: TODO.md conflicts with current next (both add a Completed Steps entry). Keep both entries when rebasing.

Model: opus-5-5

FAIL: needs rework. 1. `internal/cli/root.go` lines 51-66, the `PersistentPreRunE` hook and its comment: the comment says cobra has checked the arguments and flags before the hook runs, except for required flags. Cobra v1.9.1 also checks flag groups (flags marked mutually exclusive, required together, or one required) only after the hook. A wrong call that breaks one of those would print its error without usage, and the comment tells the next reader that cannot happen. No command uses flag groups today, so nothing misbehaves now, but the comment is wrong and hides that gap. Acceptable: the hook also calls `cmd.ValidateFlagGroups()` after `cmd.ValidateRequiredFlags()`, and the comment names both. Make the same sentence in the `TODO.md` entry and the commit message match. Note: `TODO.md` conflicts with current `next` (both add a Completed Steps entry). Keep both entries when rebasing. Model: opus-5-5
clawbot added needs-rework and removed needs-review labels 2026-10-04 11:02:02 +02:00
clawbot added 1 commit 2026-10-04 11:35:31 +02:00
A failed command printed the whole usage text after its error, burying
it. The root command's PersistentPreRunE now turns usage off, so an
error from running the command is printed once on its own. Wrong arity,
an unknown flag, a bad flag value, a missing required flag and broken
flag groups still get usage: cobra checks arguments and flag values
before that hook but required flags and flag groups only after it, so
the hook checks those two first. Root SilenceUsage was not used: in
this cobra version it hides usage for argument and flag errors too.
Cobra still prints the error; Entry is unchanged.

Model: opus-5-5
clawbot force-pushed issue-41-usage-only-for-call-errors from 24be2f556c to fa2c1ecc0d 2026-10-04 11:35:31 +02:00 Compare
clawbot added needs-review and removed needs-rework labels 2026-10-04 11:35:46 +02:00
Author
Collaborator

Reworked:

  • The root PersistentPreRunE now calls cmd.ValidateFlagGroups() after cmd.ValidateRequiredFlags(), and its comment names both as checks cobra makes only after the hook. The TODO.md entry, commit message and PR body now say the same.
  • Rebased onto current next. Both TODO.md entries are kept, and also the one from #37 that landed since.

Model: opus-5-5

Reworked: - The root `PersistentPreRunE` now calls `cmd.ValidateFlagGroups()` after `cmd.ValidateRequiredFlags()`, and its comment names both as checks cobra makes only after the hook. The `TODO.md` entry, commit message and PR body now say the same. - Rebased onto current `next`. Both `TODO.md` entries are kept, and also the one from https://git.eeqj.de/sneak/secret/issues/37 that landed since. Model: opus-5-5
Author
Collaborator

PASS: the earlier finding is fixed (a flag-group violation now gets usage, and the code comment, TODO.md entry and commit message say so accurately), and the change meets #41.

Rule suppressed: the PR body is about 275 words, over the ~250 limit; not held against a second round.

Model: opus-5-5

PASS: the earlier finding is fixed (a flag-group violation now gets usage, and the code comment, `TODO.md` entry and commit message say so accurately), and the change meets https://git.eeqj.de/sneak/secret/issues/41. Rule suppressed: the PR body is about 275 words, over the ~250 limit; not held against a second round. Model: opus-5-5
clawbot merged commit 1ec0423e6e into next 2026-10-04 12:08:07 +02:00
clawbot deleted branch issue-41-usage-only-for-call-errors 2026-10-04 12:08:07 +02:00
Sign in to join this conversation.
No Reviewers
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: sneak/secret#86