Make a mistyped subcommand under a group command an error #350

Open
clawbot wants to merge 1 commits from issue-319-unknown-subcommand into next
Collaborator

Fixes #319.

What was wrong: snapshot, config, remote, database and completion had no Run. cobra checks a command's arguments only when it has one; otherwise it prints the help and reports success. So vaultik snapshot craete, vaultik config inti and vaultik completion nosuchshell printed help and exited 0, and a crontab line with such a typo made no backup and reported no error.

What changed: each group now accepts no arguments and prints its help when given none. Any other word fails as unknown command "craete" for "vaultik snapshot" with exit 1, as vaultik bogus does at the top level. A new test covers all five groups, with a mistyped word and with the group name alone.

cobra adds the completion command only inside Execute, so NewRootCommand now adds it itself and gives it a Run.

Things the diff does not show:

  • The completion scripts go to os.Stdout as it was when NewRootCommand ran, so a later SetOut does not redirect them. Entry is unaffected.
  • A group's help now has a usage line such as vaultik snapshot [flags] above vaultik snapshot [command]. The top-level help already has the same pair.

Judgement call: the error leaves out the "Did you mean" suggestion that the top level gives. Adding one would take a hand-written argument check.

Model: opus-5-5

Fixes https://git.eeqj.de/sneak/vaultik/issues/319. **What was wrong:** `snapshot`, `config`, `remote`, `database` and `completion` had no Run. cobra checks a command's arguments only when it has one; otherwise it prints the help and reports success. So `vaultik snapshot craete`, `vaultik config inti` and `vaultik completion nosuchshell` printed help and exited 0, and a crontab line with such a typo made no backup and reported no error. **What changed:** each group now accepts no arguments and prints its help when given none. Any other word fails as `unknown command "craete" for "vaultik snapshot"` with exit 1, as `vaultik bogus` does at the top level. A new test covers all five groups, with a mistyped word and with the group name alone. cobra adds the completion command only inside Execute, so `NewRootCommand` now adds it itself and gives it a Run. **Things the diff does not show:** - The completion scripts go to `os.Stdout` as it was when `NewRootCommand` ran, so a later `SetOut` does not redirect them. `Entry` is unaffected. - A group's help now has a usage line such as `vaultik snapshot [flags]` above `vaultik snapshot [command]`. The top-level help already has the same pair. Judgement call: the error leaves out the "Did you mean" suggestion that the top level gives. Adding one would take a hand-written argument check. Model: opus-5-5
clawbot self-assigned this 2026-10-13 11:19:23 +02:00
clawbot added 1 commit 2026-10-13 11:19:23 +02:00
snapshot, config, remote, database and completion had no Run, and
cobra checks a command's arguments only when it has one, so `vaultik
snapshot craete` printed the group's help and exited 0. Each group now
prints its help only when given no argument; any other word fails as
an unknown command with exit 1, as at the top level.

cobra adds the completion command only when Execute runs, so the root
command now adds it itself to give it a Run. Its scripts then go to
os.Stdout as it was at that moment; a later SetOut does not redirect
them.

Judgement call: the error has no "Did you mean" suggestion, which the
top level gives.

Model: opus-5-5
clawbot added the needs-review label 2026-10-13 11:19:27 +02:00
Some checks are pending
check / check (push) Waiting to run
You are not authorized to merge this pull request.
This pull request can be merged automatically.
This branch is out-of-date with the base branch
View command line instructions

Checkout

From your project repository, check out a new branch and test the changes.
git fetch -u origin issue-319-unknown-subcommand:issue-319-unknown-subcommand
git checkout issue-319-unknown-subcommand
Sign in to join this conversation.