pytest_mh.cli

Classes

CLIBuilder(shell)

class pytest_mh.cli.CLIBuilder(shell: Shell)

Bases: object

Parameters:

shell (Shell) – Target shell.

class option(value)

Bases: Enum

Command line parameter types.

PLAIN = 1

Use plain parameter value without any modification.

VALUE = 2

Use parameter value but enclose it in quotes in script mode.

SWITCH = 3

Parameter is a switch which is enabled if value is True.

POSITIONAL = 4

Parameter is a positional argument.

command(command: str, args: dict[str, tuple[option, Any] | None]) str

Build full command line and return it as a string.

Output can be passed directly to Connection.run (host.conn.run).

Example
cli = CLIBuilder(Bash())
args: CLIBuilderArgs = {
    "password": (cli.option.VALUE, None),     # None values are ignored
    "home": (cli.option.VALUE, "/home/jdoe"), # --home '/home/jdoe'
    "enabled": (cli.option.SWITCH, True),     # --enabled
    "login": (cli.option.POSITIONAL, "jdoe"), # 'jdoe'
}

line = cli.command("add-user", args)
# add-user --home '/home/jdoe' --enabled 'jdoe'
Parameters:
  • command (str) – Command to call

  • args (CLIBuilderArgs) – Command’s arguments

Returns:

Full command line as string.

Return type:

str

argv(command: str, args: dict[str, tuple[option, Any] | None]) list[str]

Build full command line and return it it as list of arguments (full argv).

Output can be passed directly to Connection.exec (host.conn.exec).

Example
cli = CLIBuilder(Bash())
args: CLIBuilderArgs = {
    "password": (cli.option.VALUE, None),     # None values are ignored
    "home": (cli.option.VALUE, "/home/jdoe"), # --home '/home/jdoe'
    "enabled": (cli.option.SWITCH, True),     # --enabled
    "login": (cli.option.POSITIONAL, "jdoe"), # 'jdoe'
}

line = cli.argv("add-user", args)
# ["add-user", "--home", "/home/jdoe", "--enabled", "jdoe"]
Parameters:
  • command (str) – Command to call

  • args (CLIBuilderArgs) – Command’s arguments

Returns:

Full command line as argv

Return type:

list[str]

args(args: dict[str, tuple[option, Any] | None], *, quote_value=False) list[str]

Build command’s arguments and return them as a list (argv without command).

Output can be used for additional processing by the caller.

Example
cli = CLIBuilder(Bash())
args: CLIBuilderArgs = {
    "password": (cli.option.VALUE, None),     # None values are ignored
    "home": (cli.option.VALUE, "/home/jdoe"), # --home '/home/jdoe'
    "enabled": (cli.option.SWITCH, True),     # --enabled
    "login": (cli.option.POSITIONAL, "jdoe"), # 'jdoe'
}

line = cli.args(args)
# ["--home", "/home/jdoe", "--enabled", "jdoe"]

host.conn.run(f"user-add --encrypt-home {' '.join(line)}")
Parameters:
  • args (CLIBuilderArgs) – Command’s argument

  • quote_value (bool, optional) – True if values should enclosed with quotes, defaults to False

Returns:

Arguments ready to use in command line (argv without command)

Return type:

list[str]