psyinfra / onyo

text-based inventory system on top of git
ISC License
3 stars 5 forks source link

Fix help on unrecognized arguments #577

Closed bpoldrack closed 4 months ago

bpoldrack commented 4 months ago

This patch tries to workaround an issue with argparse, where an error on unrecognized arguments, is not associated with the selected subparser. See https://bugs.python.org/issue34479

Solution here is, to rely on parse_known_args instead of parse_args, returning unrecognized arguments in a dedicated list and call the subparsers print_usage in addition to reproducing the toplevel parsers' error.

The reason for doing both, is because we can't distinguish onyo -x new from onyo new -x via argparse (we'd need to reparse argv ourselves). Hence, it's not clear in all cases, whether the usage for new or onyo would be relevant to the user.

So, in the above example this path ends up with:

❱ onyo -x new                                                                                                                                                                   1 !
usage: onyo new [-h] [-t TEMPLATE] [-c CLONE] [-e] [-k KEYS [KEYS ...]] [-p PATH] [-tsv TSV] [-m MESSAGE]
usage: onyo [-h] [-C DIR] [-d] [-v] [-q] [-y] <command> ...
onyo: error: unrecognized arguments: -x

(exact same for onyo new -x)

Still: A lot better than before, I think.

(Closes #356) (Closes #408)

codecov[bot] commented 4 months ago

Codecov Report

Attention: Patch coverage is 83.33333% with 1 lines in your changes are missing coverage. Please review.

Project coverage is 95.73%. Comparing base (0bb43a2) to head (99db308).

Files Patch % Lines
onyo/main.py 83.33% 0 Missing and 1 partial :warning:
Additional details and impacted files ```diff @@ Coverage Diff @@ ## main #577 +/- ## ========================================== - Coverage 95.75% 95.73% -0.02% ========================================== Files 71 71 Lines 5581 5586 +5 Branches 1079 1081 +2 ========================================== + Hits 5344 5348 +4 Misses 139 139 - Partials 98 99 +1 ```

:umbrella: View full report in Codecov by Sentry.
:loudspeaker: Have feedback on the report? Share it here.