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

Allow repeating an option that takes multiple values #1445

Merged
merged 4 commits into from
Apr 25, 2024

Conversation

victorlin
Copy link
Member

@victorlin victorlin commented Apr 15, 2024

Description of proposed changes

Many command line arguments take multiple values. Previously, to utilize this feature, all values must be specified after a single option flag (e.g. --exclude-where 'region=A' 'region=B'). If options were set using separate option flags (e.g. --exclude-where 'region=A' --exclude-where 'region=B'), only the last flag would be used, which is unintuitive. This commit enables all option flags to be used.

Done across the codebase by adding action='extend' to all options that use nargs='+' and did not already specify an action.

A side effect of action='extend' is that it extends instead of replacing a default value defined in add_argument. Allow users to continue overriding the default value by first defaulting to an empty list constant, then setting the intended default value later during argument validation.

Related issue(s)

Checklist

  • Checks pass
  • If making user-facing changes, add a message in CHANGES.md summarizing the changes in this PR
  • Try using custom Argparse Action instead of DEFER_LIST_DEFAULT

@victorlin victorlin self-assigned this Apr 15, 2024
@victorlin victorlin marked this pull request as ready for review April 15, 2024 17:41
@victorlin victorlin requested a review from a team April 15, 2024 17:42
@joverlee521
Copy link
Contributor

Thanks for updating the behavior across all commands @victorlin! I believe there are also options that use double-quotes for nargs, i.e. nargs="+".

@victorlin
Copy link
Member Author

victorlin commented Apr 15, 2024

@joverlee521 good catch, updated in force-push and now some CI tests are failing. Taking a look now

@victorlin
Copy link
Member Author

I think the fix is d0d0bf0 applied everywhere defaults is used.

@victorlin victorlin force-pushed the victorlin/argparse-extend branch 2 times, most recently from 5fa320c to 4a25d3b Compare April 15, 2024 20:53
augur/curate/__init__.py Outdated Show resolved Hide resolved
augur/argparse_.py Outdated Show resolved Hide resolved
From PEP 8 recommendations:

    Comparisons to singletons like None should always be done with is or is
    not, never the equality operators.

<https://peps.python.org/pep-0008/#programming-recommendations>
@victorlin victorlin force-pushed the victorlin/argparse-extend branch 2 times, most recently from e1fbbfc to 3a5a6bf Compare April 22, 2024 22:48
Copy link
Member

@tsibley tsibley left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Reviewed by inspection (did not test), and this looks good to me.

Copy link
Contributor

@joverlee521 joverlee521 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Changes look good by inspection!

One final note that there are options that use nargs="*" that should probably also use action=extend.

Many command line arguments take multiple values. Previously, to utilize
this feature, all values must be specified after a single option flag
(e.g. --exclude-where 'region=A' 'region=B'). If options were set using
separate option flags (e.g. --exclude-where 'region=A' --exclude-where
'region=B'), only the last flag would be used, which is unintuitive.
This commit enables all option flags to be used.

Done across the codebase by adding action='extend' to all options that
use nargs='+' or '*' and did not already specify an action.

A side effect of action='extend' is that it extends instead of replacing
a default value defined in add_argument. For arguments that use a custom
default value, use a custom argparse action ExtendOverwriteDefault,
based on the similarly named AppendOverwriteDefault from Nextstrain
CLI¹.

¹ <https://github.com/nextstrain/cli/blob/9bf646b0c795d04658a6f6807d74428b7c173995/nextstrain/cli/argparse.py#L183-L197>
@victorlin
Copy link
Member Author

@joverlee521 good catch, included nargs='*' in force-push.

@victorlin victorlin merged commit 430a976 into master Apr 25, 2024
19 of 20 checks passed
@victorlin victorlin deleted the victorlin/argparse-extend branch April 25, 2024 16:50
@victorlin victorlin mentioned this pull request Apr 29, 2024
2 tasks
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