improver.argparser module¶
Common option utilities for improver CLIs.
-
class
improver.argparser.ArgParser(central_arguments=(), specific_arguments=None, **kwargs)[source]¶ Bases:
argparse.ArgumentParserArgument parser for improver CLIs.
The main purpose of this class is to make it easier to create CLIs which have arguments which are selected from centralized collections.
To fulfil these requirements, we define 2 class level dictionaries, ArgParser.CENTRALIZED_ARGUMENTS, and ArgParser.COMPULSORY_ARGUMENTS.
- For these dictionaries, each element has:
a key, which is a string representing the argument name - used internally to refer to a particular argument (which, in the case of the CENTRALIZED_ARGUMENTS may be selected from when creating an instance of the ArgParser)
- a value, which is a list containing 2 elements:
a list of strings containing the different flags which are associated with the argument (ie.: the first argument to the add_arguments() method, e.g: [‘–profile’, ‘-p’])
a dictionary containing all of the kwargs which are passed to the add_argument() method (e.g: {‘action’: ‘store_true’, ‘default’: False, ‘help’: … })
The CENTRALIZED_ARGUMENTS will be selected from, as necessary, for each of the CLIs that we create, and the COMPULSORY_ARGUMENTS will be automatically added to all CLIs (with no option to exclude them).
ArgParser.DEFAULT_CENTRALIZED_ARG_NAMES defines the centralized arguments which are to be included by default when creating instances of this class (i.e: when nothing is explicitly passed into the constructor). This is a tuple containing keys associated with the ArgParser.CENTRALIZED_ARGUMENTS dictionary.
-
CENTRALIZED_ARGUMENTS= {'input_file': (['input_filepath'], {'metavar': 'INPUT_FILE', 'help': 'A path to an input NetCDF file to be processed'}), 'output_file': (['output_filepath'], {'metavar': 'OUTPUT_FILE', 'help': 'The output path for the processed NetCDF'})}¶
-
COMPULSORY_ARGUMENTS= {'profile': (['--profile'], {'action': 'store_true', 'help': 'Switch on profiling information.'}), 'profile_file': (['--profile_file'], {'metavar': 'PROFILE_FILE', 'help': 'Dump profiling info to a file. Implies --profile.'})}¶
-
DEFAULT_CENTRALIZED_ARG_NAMES= ()¶
-
__init__(central_arguments=(), specific_arguments=None, **kwargs)[source]¶ Create an ArgParse instance, which is a subclass of argparse.ArgumentParser and automatically add all of the arguments. (Note: The ArgParse.COMPULSORY_ARGUMENTS are always added.)
- Parameters
central_arguments (list) – A list containing the centralized arguments we require. (Keys of the centralized argument dictionary). By default this is set as ArgParse.DEFAULT_CENTRALIZED_ARG_NAMES.
specific_arguments (list) – A list of argument specifications required to add arguments which are not contained within the centralized argument dictionary. The format of these argument specifications should be the same as the values in the ArgParser.CENTRALIZED_ARGUMENTS dictionary. (For more details, see the add_arguments method). Default is None, which does not add additional arguments.
**kwargs – Additional keyword arguments which are passed to the superclass constructor (argparse.ArgumentParser), e.g: the description of the ArgumentParser.
-
add_arguments(argspec_list)[source]¶ Adds a list of arguments to the ArgumentParser.
The input argspec_list is a list of argument specifications, where each element (argument specification) is a tuple/list of length 2. The first element of an argument specification is a list of strings which the name/flags used to add the argument. The second element of the argument spec shall be a dictionary containing the keyword arguments which are passed into the add_argument() method.
- Parameters
argspec_list (list) – A list containing the specifications required to add the arguments (see above)
- Raises
AttributeError – Notifies the user if any of the argument specifications has the wrong length (not 2).
-
parse_args(args=None, namespace=None)[source]¶ Wrap in order to implement some compulsory behaviour.
-
wrong_args_error(args, method)[source]¶ Raise a parser error.
Some CLI scripts have multiple methods of carrying out an action, with each method having different arguments. This method provides a standard error to be used when incompatible method-argument combinations are passed in - ie: when there are mutually exclusive groups of arguments.