diff --git a/augur/filter/__init__.py b/augur/filter/__init__.py index 5915a39b8..05357b83e 100644 --- a/augur/filter/__init__.py +++ b/augur/filter/__init__.py @@ -71,7 +71,7 @@ def register_arguments(parser): output_group.add_argument('--output-log', help="tab-delimited file with one row for each filtered strain and the reason it was filtered. Keyword arguments used for a given filter are reported in JSON format in a `kwargs` column.") output_group.add_argument( '--empty-results-reporting', - type=EmptyResultsReportingMethod, + type=EmptyResultsReportingMethod.argtype, choices=list(EmptyResultsReportingMethod), default=EmptyResultsReportingMethod.ERROR, help="How should empty filter results be reported.") diff --git a/augur/types.py b/augur/types.py index f8402177b..eccad4aa6 100644 --- a/augur/types.py +++ b/augur/types.py @@ -1,3 +1,4 @@ +from argparse import ArgumentTypeError import enum @@ -19,6 +20,22 @@ def __str__(self) -> str: """ return self.value + @classmethod + def argtype(cls, input_string): + """ + Intended to be used as the argument type converter for argparse options + that use the enum values as inputs. + + Raises a custom `argparse.ArgumentTypeError` so that the error + message can include a helpful list of the valid enum values. + """ + try: + return cls(input_string) + except ValueError: + choices = ", ".join(map(str, cls)) + raise ArgumentTypeError( + f"invalid choice: {input_string!r} (choose from {choices})") + @enum.unique class DataErrorMethod(ArgparseEnum):