argparser#
- class PdArgumentParser(dataclass_types: DataClassType | Iterable[DataClassType], **kwargs)[source]#
Bases:
ArgumentParser
This subclass of
argparse.ArgumentParser
uses type hints on dataclasses to generate arguments.The class is designed to play well with the native argparse. In particular, you can add more (non-dataclass backed) arguments to the parser after initialization and you’ll get the output back after parsing as an additional namespace. Optional: To create sub argument groups use the
_argument_group_name
attribute in the dataclass.- parse_args_into_dataclasses(args=None, return_remaining_strings=False, look_for_args_file=True, args_filename=None) Tuple[DataClass, ...] [source]#
Parse command-line args into instances of the specified dataclass types.
This relies on argparse’s
ArgumentParser.parse_known_args
. See the doc at: docs.python.org/3.7/library/argparse.html#argparse.ArgumentParser.parse_args- Parameters:
args – List of strings to parse. The default is taken from sys.argv. (same as argparse.ArgumentParser)
return_remaining_strings – If true, also return a list of remaining argument strings.
look_for_args_file – If true, will look for a “.args” file with the same base name as the entry point script for this process, and will append its potential content to the command line args.
args_filename – If not None, will uses this file instead of the “.args” file specified in the previous argument.
- Returns:
the dataclass instances in the same order as they were passed to the initializer.abspath
if applicable, an additional namespace for more (non-dataclass backed) arguments added to the parser after initialization.
The potential list of remaining argument strings. (same as argparse.ArgumentParser.parse_known_args)
- Return type:
Tuple consisting of
- parse_json_file(json_file: str, return_remaining_strings=False) Tuple[DataClass, ...] [source]#
Alternative helper method that does not use
argparse
at all, instead loading a json file and populating the dataclass types.
- parse_json_file_and_cmd_lines(return_remaining_strings=False) Tuple[DataClass, ...] [source]#
Extend the functionality of
parse_json_file
to handle command line arguments in addition to loading a JSON file.When there is a conflict between the command line arguments and the JSON file configuration, the command line arguments will take precedence.
- Returns:
the dataclass instances in the same order as they were passed to the initializer.abspath
- Return type:
Tuple consisting of