-
Notifications
You must be signed in to change notification settings - Fork 129
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
Register subparsers recursively #1640
Draft
victorlin
wants to merge
3
commits into
master
Choose a base branch
from
victorlin/argparse-register-commands
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from 2 commits
Commits
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
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,78 @@ | ||
import argparse | ||
from augur.argparse_ import ExtendOverwriteDefault | ||
from augur.io.metadata import DEFAULT_DELIMITERS | ||
from augur.types import DataErrorMethod | ||
|
||
|
||
def create_shared_parser(): | ||
""" | ||
Creates an argparse.ArgumentParser that is intended to be used as a parent | ||
parser¹ for all `augur curate` subcommands. This should include all options | ||
that are intended to be shared across the subcommands. | ||
|
||
Note that any options strings used here cannot be used in individual subcommand | ||
subparsers unless the subparser specifically sets `conflict_handler='resolve'` ², | ||
then the subparser option will override the option defined here. | ||
|
||
Based on https://stackoverflow.com/questions/23296695/permit-argparse-global-flags-after-subcommand/23296874#23296874 | ||
|
||
¹ https://docs.python.org/3/library/argparse.html#parents | ||
² https://docs.python.org/3/library/argparse.html#conflict-handler | ||
""" | ||
shared_parser = argparse.ArgumentParser(add_help=False) | ||
|
||
shared_inputs = shared_parser.add_argument_group( | ||
title="INPUTS", | ||
description=""" | ||
Input options shared by all `augur curate` commands. | ||
If no input options are provided, commands will try to read NDJSON records from stdin. | ||
""") | ||
shared_inputs.add_argument("--metadata", | ||
help="Input metadata file. May be plain text (TSV, CSV) or an Excel or OpenOffice spreadsheet workbook file. When an Excel or OpenOffice workbook, only the first visible worksheet will be read and initial empty rows/columns will be ignored. Accepts '-' to read plain text from stdin.") | ||
shared_inputs.add_argument("--id-column", | ||
help="Name of the metadata column that contains the record identifier for reporting duplicate records. " | ||
"Uses the first column of the metadata file if not provided. " | ||
"Ignored if also providing a FASTA file input.") | ||
shared_inputs.add_argument("--metadata-delimiters", default=DEFAULT_DELIMITERS, nargs="+", action=ExtendOverwriteDefault, | ||
help="Delimiters to accept when reading a plain text metadata file. Only one delimiter will be inferred.") | ||
|
||
shared_inputs.add_argument("--fasta", | ||
help="Plain or gzipped FASTA file. Headers can only contain the sequence id used to match a metadata record. " + | ||
"Note that an index file will be generated for the FASTA file as <filename>.fasta.fxi") | ||
shared_inputs.add_argument("--seq-id-column", | ||
help="Name of metadata column that contains the sequence id to match sequences in the FASTA file.") | ||
shared_inputs.add_argument("--seq-field", | ||
help="The name to use for the sequence field when joining sequences from a FASTA file.") | ||
|
||
shared_inputs.add_argument("--unmatched-reporting", | ||
type=DataErrorMethod.argtype, | ||
choices=list(DataErrorMethod), | ||
default=DataErrorMethod.ERROR_FIRST, | ||
help="How unmatched records from combined metadata/FASTA input should be reported.") | ||
shared_inputs.add_argument("--duplicate-reporting", | ||
type=DataErrorMethod.argtype, | ||
choices=list(DataErrorMethod), | ||
default=DataErrorMethod.ERROR_FIRST, | ||
help="How should duplicate records be reported.") | ||
|
||
shared_outputs = shared_parser.add_argument_group( | ||
title="OUTPUTS", | ||
description=""" | ||
Output options shared by all `augur curate` commands. | ||
If no output options are provided, commands will output NDJSON records to stdout. | ||
""") | ||
shared_outputs.add_argument("--output-metadata", | ||
help="Output metadata TSV file. Accepts '-' to output TSV to stdout.") | ||
|
||
shared_outputs.add_argument("--output-fasta", | ||
help="Output FASTA file.") | ||
shared_outputs.add_argument("--output-id-field", | ||
help="The record field to use as the sequence identifier in the FASTA output.") | ||
shared_outputs.add_argument("--output-seq-field", | ||
help="The record field that contains the sequence for the FASTA output. " | ||
"This field will be deleted from the metadata output.") | ||
|
||
return shared_parser | ||
|
||
|
||
shared_parser = create_shared_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
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
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.
This breaks
augur curate
subcommandsbecause each subcommand's
run
function is expected to be called through augur curate's ownrun
function, but it is now called directly at the top level.Maybe recursively registering subcommands is not the right thing to do in this codebase.
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.
I remember having to work around the top level run when initially adding the
augur curate
subcommand.augur/augur/curate/__init__.py
Lines 117 to 120 in 129fd7e
Would it be weird to keep
augur curate
as a special case?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.
Two options:
augur curate
commands share functionality so calling theirrun()
from the top-level works. This could be done relatively straightforwardly.register_commands(…)
themselves.I don't think either gets the way of the functional improvements we want to make? So it's a matter of preference.
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.
Thanks for the input – I'll try option 1 above to align with Nextstrain CLI code.