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.
- static setinstance(instance, warn_reset=True)¶
Sets the main instance of
Args
.- Parameters:
instance –
Args
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 toFalse
.
- classmethod getinstance()¶
Singleton.
- Returns:
Main
Args
instance.
Warning
The main
Args
instance is not created automatically by this method, and should be set withsetinstance()
prior to anygetinstance()
call.
- classmethod isset()¶
Checks whether the single instance of
Args
is set and of thecls
type.- Parameters:
cls – Expected type.
- Returns:
True
if the singleArgs
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 whoseArgInfo.define()
should be called onto.
ArgInfo.define()
should be called on theArgInfo
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 throughArgInfo.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 eachArgs.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).
- 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 theArgs.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 thedest
parameter which should not be set. TheArgs.ArgInfo.member_name
member will be used for the purpose.Should be called on the
Args.ArgInfo
returned theArgs.addarg()
method.See also