Skip to content

command: prevent fall back to global usage for subcommand option errors#76

Open
HoyeonRhee wants to merge 1 commit into
mricon:masterfrom
HoyeonRhee:subcmd-usage
Open

command: prevent fall back to global usage for subcommand option errors#76
HoyeonRhee wants to merge 1 commit into
mricon:masterfrom
HoyeonRhee:subcmd-usage

Conversation

@HoyeonRhee

@HoyeonRhee HoyeonRhee commented Mar 21, 2026

Copy link
Copy Markdown

Argparse reports unknown arguments from top-level parser even after
selecting a subcommand, so command like "b4 dig --unknown-arg" prints
the global usage instead of the dig usage.

To handle, this commit set a subparser attribute for each subcommand
parser, then use parse_known_args() in cmd() to report leftover arguments
from the selected subparser instead of the top-level parser.

$ b4 dig --unknown-arg

# before:
usage: b4 [-h] [--version] [-d] [-q] [-n] [--offline-mode] [--no-stdin]
          [-c NAME=VALUE]
          {mbox,am,shazam,review,pr,ty,diff,kr,prep,trailers,send,dig} ...
b4: error: unrecognized arguments: --unknown-arg

# after:
usage: b4 dig [-h] [-c COMMITISH] [-C] [-a | -m DEST | -w]
b4 dig: error: unrecognized arguments: --unknown-arg

Fixes: #75

Argparse reports unknown arguments from top-level parser even after
selecting a subcommand, so command like "b4 dig --unknown-arg" prints
the global usage instead of the dig usage.

To handle, this commit set a subparser attribute for each subcommand
parser, then use parse_known_args() in cmd() to report leftover
arguments from the selected subparser instead of the top-level parser.

Signed-off-by: Hoyeon Lee <hoyeon.lee@suse.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

subcommand option errors fall back to top-level usage

1 participant