argparser#

class PdArgumentParser(dataclass_types: DataClassType | Iterable[DataClassType], **kwargs)[源代码]#

基类: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, ...][源代码]#

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

参数:
  • 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.

返回:

  • 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)

返回类型:

Tuple consisting of

parse_json_file(json_file: str) Tuple[DataClass, ...][源代码]#

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() Tuple[DataClass, ...][源代码]#

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.

返回:

  • the dataclass instances in the same order as they were passed to the initializer.abspath

返回类型:

Tuple consisting of

parse_dict(args: dict) Tuple[DataClass, ...][源代码]#

Alternative helper method that does not use argparse at all, instead uses a dict and populating the dataclass types.