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

argparse: Restore --help and --help-all difference on Python ≥3.10 #259

Merged
merged 1 commit into from
Feb 28, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,14 @@ development source code and as such may not be routinely kept up to date.
`{files}`.
([#260](https://github.com/nextstrain/cli/pull/260))

* When running on Python ≥3.10, the `--help` output of `nextstrain build`,
`nextstrain view`, and `nextstrain shell` once again shows just the most
common options. All options are still shown with `--help-all`. A regression
since Python 3.10 meant that `--help` acted the same as `--help-all` before
this fix. This affected any installation on Python ≥3.10, including
standalone installations, since the standalone binaries bundle Python 3.10.
([#259](https://github.com/nextstrain/cli/pull/259))


# 6.1.0.post1 (18 January 2023)

Expand Down
11 changes: 8 additions & 3 deletions nextstrain/cli/argparse.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
"""
Custom helpers for extending the behaviour of argparse standard library.
"""
import sys
from argparse import Action, ArgumentDefaultsHelpFormatter, ArgumentTypeError, SUPPRESS
from itertools import takewhile
from textwrap import indent
Expand Down Expand Up @@ -113,9 +114,13 @@ def __call__(self, parser, namespace, values, option_string = None):

def truncate_help(self, full_help):
"""
Truncate the full help after the standard "optional arguments" listing
and before any custom argument groups.
Truncate the full help after the standard "options" (or "optional
arguments") listing and before any custom argument groups.
"""
# See <https://github.com/python/cpython/pull/23858>
# and <https://bugs.python.org/issue9694>.
heading = "options:\n" if sys.version_info >= (3, 10) else "optional arguments:\n"

seen_optional_arguments_heading = False

def before_extra_argument_groups(line):
Expand All @@ -127,7 +132,7 @@ def before_extra_argument_groups(line):
nonlocal seen_optional_arguments_heading

if not seen_optional_arguments_heading:
if line == "optional arguments:\n":
if line == heading:
seen_optional_arguments_heading = True

return not seen_optional_arguments_heading \
Expand Down