Skip to content

robot.utils.argumentparser

ArgumentParser

ArgumentParser(
    usage,
    name=None,
    version=None,
    arg_limits=None,
    validator=None,
    env_options=None,
    auto_help=True,
    auto_version=True,
    auto_pythonpath="DEPRECATED",
    auto_argumentfile=True,
)

Available options and tool name are read from the usage.

Tool name is got from the first row of the usage. It is either the whole row or anything before first ' -- '.

Source code in src/robot/utils/argumentparser.py
def __init__(self, usage, name=None, version=None, arg_limits=None,
             validator=None, env_options=None, auto_help=True,
             auto_version=True, auto_pythonpath='DEPRECATED',
             auto_argumentfile=True):
    """Available options and tool name are read from the usage.

    Tool name is got from the first row of the usage. It is either the
    whole row or anything before first ' -- '.
    """
    if not usage:
        raise FrameworkError('Usage cannot be empty')
    self.name = name or usage.splitlines()[0].split(' -- ')[0].strip()
    self.version = version or get_full_version()
    self._usage = usage
    self._arg_limit_validator = ArgLimitValidator(arg_limits)
    self._validator = validator
    self._auto_help = auto_help
    self._auto_version = auto_version
    if auto_pythonpath == 'DEPRECATED':
        auto_pythonpath = False
    else:
        warnings.warn("ArgumentParser option 'auto_pythonpath' is deprecated "
                      "since Robot Framework 5.0.")
    self._auto_pythonpath = auto_pythonpath
    self._auto_argumentfile = auto_argumentfile
    self._env_options = env_options
    self._short_opts = ''
    self._long_opts = []
    self._multi_opts = []
    self._flag_opts = []
    self._short_to_long = {}
    self._expected_args = ()
    self._create_options(usage)

parse_args

parse_args(args)

Parse given arguments and return options and positional arguments.

Arguments must be given as a list and are typically sys.argv[1:].

Options are returned as a dictionary where long options are keys. Value is a string for those options that can be given only one time (if they are given multiple times the last value is used) or None if the option is not used at all. Value for options that can be given multiple times (denoted with '*' in the usage) is a list which contains all the given values and is empty if options are not used. Options not taken arguments have value False when they are not set and True otherwise.

Positional arguments are returned as a list in the order they are given.

If 'check_args' is True, this method will automatically check that correct number of arguments, as parsed from the usage line, are given. If the last argument in the usage line ends with the character 's', the maximum number of arguments is infinite.

Possible errors in processing arguments are reported using DataError.

Some options have a special meaning and are handled automatically if defined in the usage and given from the command line:

--argumentfile can be used to automatically read arguments from a specified file. When --argumentfile is used, the parser always allows using it multiple times. Adding '*' to denote that is thus recommend. A special value 'stdin' can be used to read arguments from stdin instead of a file.

--pythonpath can be used to add extra path(s) to sys.path. This functionality was deprecated in Robot Framework 5.0.

--help and --version automatically generate help and version messages. Version is generated based on the tool name and version -- see init for information how to set them. Help contains the whole usage given to init. Possible text in the usage is replaced with the given version. Both help and version are wrapped to Information exception.

Source code in src/robot/utils/argumentparser.py
def parse_args(self, args):
    """Parse given arguments and return options and positional arguments.

    Arguments must be given as a list and are typically sys.argv[1:].

    Options are returned as a dictionary where long options are keys. Value
    is a string for those options that can be given only one time (if they
    are given multiple times the last value is used) or None if the option
    is not used at all. Value for options that can be given multiple times
    (denoted with '*' in the usage) is a list which contains all the given
    values and is empty if options are not used. Options not taken
    arguments have value False when they are not set and True otherwise.

    Positional arguments are returned as a list in the order they are given.

    If 'check_args' is True, this method will automatically check that
    correct number of arguments, as parsed from the usage line, are given.
    If the last argument in the usage line ends with the character 's',
    the maximum number of arguments is infinite.

    Possible errors in processing arguments are reported using DataError.

    Some options have a special meaning and are handled automatically
    if defined in the usage and given from the command line:

    --argumentfile can be used to automatically read arguments from
    a specified file. When --argumentfile is used, the parser always
    allows using it multiple times. Adding '*' to denote that is thus
    recommend. A special value 'stdin' can be used to read arguments from
    stdin instead of a file.

    --pythonpath can be used to add extra path(s) to sys.path.
    This functionality was deprecated in Robot Framework 5.0.

    --help and --version automatically generate help and version messages.
    Version is generated based on the tool name and version -- see __init__
    for information how to set them. Help contains the whole usage given to
    __init__. Possible <VERSION> text in the usage is replaced with the
    given version. Both help and version are wrapped to Information
    exception.
    """
    args = self._get_env_options() + list(args)
    args = [system_decode(a) for a in args]
    if self._auto_argumentfile:
        args = self._process_possible_argfile(args)
    opts, args = self._parse_args(args)
    if self._auto_argumentfile and opts.get('argumentfile'):
        raise DataError("Using '--argumentfile' option in shortened format "
                        "like '--argumentf' is not supported.")
    opts, args = self._handle_special_options(opts, args)
    self._arg_limit_validator(args)
    if self._validator:
        opts, args = self._validator(opts, args)
    return opts, args