scenario.args module

Base module for program arguments management.

Args.getinstance() gives the only only instance of program arguments, May actually be a scenarioargs.ExecArgs or a CampaignArgs instance.

class Args

Bases: Logger, CommonConfigArgs, CommonLoggingArgs

Common program arguments management.

Handles:

  • --help option,

  • Configuration file options,

  • Logging options.

_instance

Main instance of Args.

static setinstance(instance, warn_reset=True)

Sets the main instance of Args.

Parameters:
  • instanceArgs instance.

  • warn_reset – Set to False in order to avoid the warning to be logged.

When consecutive calls occur, the latest overwrites the previous, and a warning is displayed unless warn_reset is set to False.

classmethod getinstance()

Singleton.

Returns:

Main Args instance.

Warning

The main Args instance is not created automatically by this method, and should be set with setinstance() prior to any getinstance() call.

classmethod isset()

Checks whether the single instance of Args is set and of the cls type.

Parameters:

cls – Expected type.

Returns:

True if the single Args instance is of the given type, False otherwise.

__init__(class_debugging)

Defines common program arguments.

Parameters:

class_debugging – See CommonLoggingArgs.

__arg_parser

argparse parser object.

__arg_infos

Arguments information.

parsed

Parsed flag. Tells whether arguments have been successfully parsed yet or not.

error_code

Argument parsing error code.

setprog(name)

Overwrites program name.

Parameters:

name – Program name to be displayed with usage info.

setdescription(description)

Overwrites program description.

Parameters:

description – Program description to be displayed with usage info.

addarg(member_desc, member_name, member_type)

Adds a program argument.

Parameters:
  • member_desc – Textual description of the program argument(s).

  • member_name – Corresponding member name in the owner Args instance.

  • member_type – Type of the program argument, or base type of the program arguments list, or conversion handler. When defined as a 2 items tuple, the argument feeds a dictionary: the first item of the tuple shall be str (for the dictionary keys), and the second item gives the type of the dictionary values.

Returns:

Args.ArgInfo instance whose ArgInfo.define() should be called onto.

ArgInfo.define() should be called on the ArgInfo object returned:

self.addarg("Configuration files", "config_paths", Path).define(
    "--config-file", metavar="CONFIG_PATH",
    action="append", type=str, default=[],
    help="Input configuration file path. "
         "This option may be called several times.",
)
parse(args)

Parses program arguments.

Parameters:

args – Argument list, without the program name.

Returns:

True for success, False otherwise.

_checkargs(args)

Handler for special verifications on program arguments.

Parameters:

args – The untyped object returned by argparse.ArgumentParser.parse_args().

Returns:

True for success, False otherwise.

Shall be overridden in subclasses.

class ArgInfo

Bases: object

Class that describes a single program argument (single value, list or dictionary).

Whether the program argument is a single value, or a list of value, depends on the argparse definition made through ArgInfo.define().

__init__(arg_parser, member_desc, member_name, member_type)
Parameters:
  • arg_parser – Related argparse.ArgumentParser instance.

  • member_desc – Textual description of the program argument(s).

  • member_name – Corresponding member name in the owner Args instance.

  • member_type – Base type of the program argument(s). See Args.addarg() for a detailed description of this parameter.

Args.ArgInfo.define() should be called onto each Args.ArgInfo instance newly created.

See also

Args.addarg(), Args.ArgInfo.define()

arg_parser

Related argparse.ArgumentParser instance.

member_desc

Textual description of the program argument(s).

member_name

Corresponding member name in the owner Args instance.

key_type

Key type, when the argument feeds a dictionary.

value_type

Base type of the program argument(s).

parser_arg

argparse.Action instance defined by the Args.ArgInfo.define() method.

define(*args, **kwargs)

Defines the argparse command line argument.

Parameters:
  • args – List of positional arguments.

  • kwargs – Dictionary of named arguments.

Refer to the regular argparse documentation, except for the dest parameter which should not be set. The Args.ArgInfo.member_name member will be used for the purpose.

Should be called on the Args.ArgInfo returned the Args.addarg() method.

See also

Args.addarg()

process(args_instance, parsed_args)

Process the argument value once parsed by argparse and feed the Args instance.

Parameters:
  • args_instanceArgs instance to feed.

  • parsed_args – Opaque parsed object returned by the argparse library.

Returns:

True when the operation succeeded, False otherwise.