mlrl.util.cli module

Author: Michael Rapp (michael.rapp.ml@gmail.com)

Provides classes for configuring the arguments of a command line interface.

class mlrl.util.cli.Argument(*names: str, required: bool = False, default: Any | None = None, description: str | None = None, add_default_value_to_description: bool = True, **kwargs)

Bases: object

A single argument of a command line interface for which the user can provide a custom value.

static argument_name_to_key(name: str) str

Converts the name of an argument into its key.

Parameters:

name – The name of the argument

Returns:

The key of the argument

get_value(args: Namespace, default: Any | None = None) Any | None

Returns the value provided by the user for this argument.

Parameters:
  • args – A Namespace that provides access to the values provided by the user

  • default – The default value to be returned if no value is available

Returns:

The value provided by the user or default, if no value is available

get_value_and_options(args: Namespace, default: Any | None = None) tuple[Any | None, Options]

Returns the value provided by the user for this argument.

Parameters:
  • args – A Namespace that provides access to the values provided by the user

  • default – The default value to be returned if no value is available

Returns:

The value provided by the user or default, if no value is available

property key: str

The key of the argument in a Namespace.

static key_to_argument_name(key: str) str

Converts the key of an argument into its name.

Parameters:

key – The key of the argument

Returns:

The name of the argument

property name: str

The name of the argument.

class mlrl.util.cli.BoolArgument(*names: str, description: str | None = None, add_default_value_to_description: bool = True, default: bool | None = None, required: bool = False, true_options: set[str] | None = None, false_options: set[str] | None = None)

Bases: Argument

An argument of a command line interface for which the user can provide a custom boolean value.

get_value(args: Namespace, default: Any | None = None) Any | None

Returns the value provided by the user for this argument.

Parameters:
  • args – A Namespace that provides access to the values provided by the user

  • default – The default value to be returned if no value is available

Returns:

The value provided by the user or default, if no value is available

class mlrl.util.cli.CommandLineInterface(argument_parser: ArgumentParser, version_text: str | None = None)

Bases: object

Allows to configure a command line interface for running a program.

add_arguments(*arguments: Argument, group: str | None = None)

Adds a new argument that enables the user to provide a value to the command line interface.

Parameters:
  • arguments – The arguments to be added

  • group – The name of a group, the arguments should be added to, or None, if they should not be added to a particular group

parse_known_args() Namespace

Parses and returns the values of the arguments already added to the command line interface.

Returns:

A Namespace providing access to the values of the arguments

class mlrl.util.cli.EnumArgument(*names: str, enum: type[Enum], description: str | None = None, default: Enum | None = None, required: bool = False)

Bases: SetArgument

An argument of a command line interface for which the user can provide one out of a predefined set enum values.

get_value(args: Namespace, default: Any | None = None) Any | None

Returns the value provided by the user for this argument.

Parameters:
  • args – A Namespace that provides access to the values provided by the user

  • default – The default value to be returned if no value is available

Returns:

The value provided by the user or default, if no value is available

class mlrl.util.cli.FlagArgument(name: str, description: str | None = None)

Bases: Argument

An argument of a command line interface, which can be set by the user as a flag.

class mlrl.util.cli.FloatArgument(*names: str, description: str | None = None, add_default_value_to_description: bool = True, default: float | None = None, required: bool = False)

Bases: Argument

An argument of a command line interface for which the user can provide a custom floating point value.

get_value(args: Namespace, default: Any | None = None) Any | None

Returns the value provided by the user for this argument.

Parameters:
  • args – A Namespace that provides access to the values provided by the user

  • default – The default value to be returned if no value is available

Returns:

The value provided by the user or default, if no value is available

class mlrl.util.cli.IntArgument(*names: str, description: str | None = None, add_default_value_to_description: bool = True, default: int | None = None, required: bool = False)

Bases: Argument

An argument of a command line interface for which the user can provide a custom integer value.

get_value(args: Namespace, default: Any | None = None) Any | None

Returns the value provided by the user for this argument.

Parameters:
  • args – A Namespace that provides access to the values provided by the user

  • default – The default value to be returned if no value is available

Returns:

The value provided by the user or default, if no value is available

class mlrl.util.cli.PathArgument(*names: str, description: str | None = None, add_default_value_to_description: bool = True, default: str | None = None, required: bool = False)

Bases: StringArgument

An argument of a command line interface for which the user can provide a custom string value.

get_value(args: Namespace, default: Any | None = None) Any | None

Returns the value provided by the user for this argument.

Parameters:
  • args – A Namespace that provides access to the values provided by the user

  • default – The default value to be returned if no value is available

Returns:

The value provided by the user or default, if no value is available

class mlrl.util.cli.SetArgument(*names: str, values: set[str] | dict[str, set[str]], description: str | None = None, add_default_value_to_description: bool = True, default: str | None = None, required: bool = False, description_formatter: ~typing.Callable[[str, set[str] | dict[str, set[str]]], str | None] = <staticmethod(<function SetArgument.__format_description>)>)

Bases: Argument

An argument of a command line interface for which the user can provide one out of a predefined set of string values.

Values = set[str] | dict[str, set[str]]
get_value(args: Namespace, default: Any | None = None) Any | None

Returns the value provided by the user for this argument.

Parameters:
  • args – A Namespace that provides access to the values provided by the user

  • default – The default value to be returned if no value is available

Returns:

The value provided by the user or default, if no value is available

class mlrl.util.cli.StringArgument(*names: str, description: str | None = None, add_default_value_to_description: bool = True, default: str | None = None, required: bool = False)

Bases: Argument

An argument of a command line interface for which the user can provide a custom string value.

get_value(args: Namespace, default: Any | None = None) Any | None

Returns the value provided by the user for this argument.

Parameters:
  • args – A Namespace that provides access to the values provided by the user

  • default – The default value to be returned if no value is available

Returns:

The value provided by the user or default, if no value is available