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, decorator: Callable[[Namespace, Any | None], Any | None] | None = None, **kwargs: Any)

Bases: object

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

Decorator

alias of Callable[[Namespace, Any | None], Any | None]

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

property key: str

The key of the argument in a Namespace.

property name: str

The name of the argument.

class mlrl.util.cli.BoolArgument(*names: str, description: str | None = None, default: bool | None = None, required: bool = False, true_options: Set[str] | None = None, false_options: Set[str] | None = None, decorator: Callable[[Namespace, Any | None], Any | None] | 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)

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

Parameters:

arguments – The arguments to be added

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, decorator: Callable[[Namespace, Any | None], Any | None] | None = None)

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, default: float | None = None, required: bool = False, decorator: Callable[[Namespace, Any | None], Any | None] | None = None)

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, default: int | None = None, required: bool = False, decorator: Callable[[Namespace, Any | None], Any | None] | None = None)

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.SetArgument(*names: str, values: Set[str] | Dict[str, Set[str]], description: str | None = None, default: str | None = None, required: bool = False, decorator: Callable[[Namespace, Any | None], Any | None] | None = None)

Bases: Argument

An argument of a command line interface for which the user can provide one out of a predefined set of string 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.StringArgument(*names: str, description: str | None = None, default: str | None = None, required: bool = False, decorator: Callable[[Namespace, Any | None], Any | None] | None = None)

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