-
Notifications
You must be signed in to change notification settings - Fork 128
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
Organize subcommands #1002
Merged
Merged
Organize subcommands #1002
Changes from all commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
d952763
Start the shared `argparse_` module
joverlee521 7e4b216
Allow commands to add their own subparser
joverlee521 311cccb
refactor: rename `import.py` to `import_.py`
joverlee521 d531d48
Move `HideAsFalseAction` to `argparse_` module
joverlee521 dc4f58b
Refactor measurements module to a package
joverlee521 1f0d823
Refactor import_ and import_beast to a package
joverlee521 7de1fcd
Refactor `export` to use `add_command_subparsers`
joverlee521 9b6107d
measurements: rename parser group variables
joverlee521 888ba09
Remove `utils.first_line` from modules with one line docstrings
joverlee521 6df2066
export: create help message from module docstrings
joverlee521 60ba754
Update changelog
joverlee521 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
""" | ||
Custom helpers for the argparse standard library. | ||
""" | ||
from argparse import Action, ArgumentDefaultsHelpFormatter | ||
|
||
|
||
def add_default_command(parser): | ||
""" | ||
Sets the default command to run when none is provided. | ||
""" | ||
class default_command(): | ||
def run(args): | ||
parser.print_help() | ||
return 2 | ||
|
||
parser.set_defaults(__command__ = default_command) | ||
|
||
|
||
def add_command_subparsers(subparsers, commands): | ||
""" | ||
Add subparsers for each command module. | ||
|
||
Parameters | ||
---------- | ||
subparsers: argparse._SubParsersAction | ||
The special subparsers action object created by the parent parser | ||
via `parser.add_subparsers()`. | ||
|
||
commands: list[ModuleType] | ||
A list of modules that are commands that require their own subparser. | ||
Each module is required to have a `register_parser` function to add its own | ||
subparser and arguments. | ||
""" | ||
for command in commands: | ||
# Allow each command to register its own subparser | ||
subparser = command.register_parser(subparsers) | ||
|
||
# Allows us to run commands directly with `args.__command__.run()` | ||
subparser.set_defaults(__command__ = command) | ||
|
||
# Use the same formatting class for every command for consistency. | ||
# Set here to avoid repeating it in every command's register_parser(). | ||
subparser.formatter_class = ArgumentDefaultsHelpFormatter | ||
|
||
if not subparser.description and command.__doc__: | ||
subparser.description = command.__doc__ | ||
|
||
# If a command doesn't have its own run() function, then print its help when called. | ||
if not getattr(command, "run", None): | ||
add_default_command(subparser) | ||
|
||
|
||
class HideAsFalseAction(Action): | ||
""" | ||
Custom argparse Action that stores False for arguments passed as `--hide*` | ||
and stores True for all other argument patterns. | ||
""" | ||
def __call__(self, parser, namespace, values, option_string=None): | ||
setattr(namespace, self.dest, option_string[2:6] != 'hide') |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,22 +1,21 @@ | ||
""" | ||
Export JSON files suitable for visualization with auspice. | ||
""" | ||
from .export_v1 import run_v1, register_arguments_v1 | ||
from .export_v2 import run_v2, register_arguments_v2 | ||
from .argparse_ import add_command_subparsers | ||
from . import export_v1, export_v2 | ||
|
||
SUBCOMMANDS = [ | ||
export_v1, | ||
export_v2, | ||
] | ||
|
||
def register_arguments(parser): | ||
|
||
def register_parser(parent_subparsers): | ||
parser = parent_subparsers.add_parser("export", help=__doc__) | ||
# Add subparsers for subcommands | ||
metavar_msg ="Augur export now needs you to define the JSON version " + \ | ||
"you want, e.g. `augur export v2`." | ||
subparsers = parser.add_subparsers(title="JSON SCHEMA", | ||
metavar=metavar_msg) | ||
subparsers.required = True | ||
register_arguments_v2(subparsers) | ||
register_arguments_v1(subparsers) | ||
|
||
|
||
def run(args): | ||
if "v1" in args: | ||
return run_v1(args) | ||
else: | ||
return run_v2(args) | ||
add_command_subparsers(subparsers, SUBCOMMANDS) | ||
return parser |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ideally, I would like to add the subparser's help message here instead of requiring each module to import
utils.first_line
. However, I have not found a way to addhelp
outside of theadd_parser
call.When I tried:
I get an error
AttributeError: 'ArgumentParser' object has no attribute 'help'
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ah,
argparse
uses private methods to add the help message behind the scenes.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah.
help=
becomes a property of a choices action on the parser's subparsers object, not the subparser itself.It's retrievable, e.g.
but may not want to do that here!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hmm, seems a bit hacky to do this...
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah, agreed!