Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: better output and exit code from incomplete commands #346

Merged
merged 1 commit into from
Jul 9, 2024

Conversation

infinitewarp
Copy link
Member

@infinitewarp infinitewarp commented Jul 8, 2024

This changes the way argparse validates incomplete subparser (subcommand) inputs. By setting required=true, argparse correctly halts processing, displays a useful error message, and returns a non-zero exit code.

Old handling of incomplete commands:

$ qpc server
usage: qpc [-h] [--version] [-v] {server,cred,source,scan,report,insights} ...

positional arguments:
  {server,cred,source,scan,report,insights}

options:
  -h, --help            show this help message and exit
  --version             show program's version number and exit
  -v                    Verbose mode. Use up to -vvvv for more verbosity.

$ echo $?
0

Note that the old output simply dumps the top-level help message which does not actually help with the provided server subcommand, and it exits with 0 which wrongly indicates success.

New handling of incomplete commands:

$ qpc server
usage: qpc server [-h] {config,login,logout,status} ...
qpc server: error: the following arguments are required: action

$ echo $?
2

@infinitewarp infinitewarp marked this pull request as ready for review July 8, 2024 21:33
@infinitewarp infinitewarp requested a review from a team July 9, 2024 19:45
@infinitewarp infinitewarp enabled auto-merge (rebase) July 9, 2024 19:47
@abellotti
Copy link
Contributor

this looks great @infinitewarp, nice and surgical. Are there any doc updates needed somewhere where we show the old usage/responses ?

@infinitewarp infinitewarp merged commit 02256bb into main Jul 9, 2024
19 checks passed
@infinitewarp infinitewarp deleted the improve-argparse branch July 9, 2024 20:13
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.

3 participants