opengui.Cli

class opengui.Cli(values: dict = None, fields: 'list[dict]' = None, engine: yaes.Engine = None)

Class for answering fields at a cli

Parameters:
  • values (dict) – Field values to use, key by name

  • fields (list[dict]) – Field to use in dict form, not instances

  • engine (Engine) – Yaes Engine to use for cli()

engine: bool

Yaes Engine to use

fields: list[dict]

Field to use in dict form, not instances

values: dict

Field values to use, key by name

ask() dict

Returns dict of values from getting input from the cli

Return type:

dict

Usage

Taken from its unittest:

@unittest.mock.patch("builtins.print")
@unittest.mock.patch("builtins.input")
def test_ask(self, mock_input, mock_print):

    cli = opengui.Cli(
        fields=[
            {
                "name": "basic",
                "description": "be basic",
                "default": "badass",
                "validation": "^bitch$"
            },
            {
                "name": "single",
                "options": ["yin", "yang"],
                "labels": {
                    "yin": "Yin",
                    "yang": "Yang"
                },
                "default": "yin"
            },
            {
                "name": "multi-option",
                "multi": True,
                "options": "{[ fs ]}",
                "default": ["fun", "foe"]
            },
            {
                "name": "multi-text",
                "multi": True,
                "default": ["pun", "poe"]
            },
            {
                "name": "yah",
                "bool": True,
                "default": True
            },
            {
                "name": "sure",
                "bool": True
            },
            {
                "name": "nah",
                "bool": True
            }
        ],
        values={
            "fs": ["fee", "fie", "foe", "fum"]
        }
    )

    mock_input.side_effect = [
        "",
        "bitch",
        "fish",
        "0",
        "3",
        "1",
        "fish 0 6",
        "",
        "1 3",
        "pun crow",
        "",
        "y",
        "n"
    ]


    self.assertEqual(cli.ask(), {
        "basic": "bitch",
        "single": "yin",
        "multi-option": ["fee", "foe"],
        "multi-text": ["pun", "crow"],
        "yah": True,
        "sure": True,
        "nah": False,
        "fs": ["fee", "fie", "foe", "fum"]
    })

    mock_print.assert_has_calls([
        unittest.mock.call('  be basic'),
        unittest.mock.call("must match '^bitch$'"),
        unittest.mock.call('[1] Yin'),
        unittest.mock.call('[2] Yang'),
        unittest.mock.call('invalid choice: fish'),
        unittest.mock.call('[1] Yin'),
        unittest.mock.call('[2] Yang'),
        unittest.mock.call('invalid choice: 0'),
        unittest.mock.call('[1] Yin'),
        unittest.mock.call('[2] Yang'),
        unittest.mock.call('invalid choice: 3'),
        unittest.mock.call('[1] Yin'),
        unittest.mock.call('[2] Yang'),
        unittest.mock.call('[1] fee'),
        unittest.mock.call('[2] fie'),
        unittest.mock.call('[3] foe'),
        unittest.mock.call('[4] fum'),
        unittest.mock.call("invalid choices: ['fish', '0', '6']"),
        unittest.mock.call('[1] fee'),
        unittest.mock.call('[2] fie'),
        unittest.mock.call('[3] foe'),
        unittest.mock.call('[4] fum'),
        unittest.mock.call("invalid values ['fun']"),
        unittest.mock.call('[1] fee'),
        unittest.mock.call('[2] fie'),
        unittest.mock.call('[3] foe'),
        unittest.mock.call('[4] fum')
    ])

    mock_input.assert_has_calls([
        unittest.mock.call('basic: '),
        unittest.mock.call('enter index - single: '),
        unittest.mock.call('enter index - single: '),
        unittest.mock.call('enter index - single: '),
        unittest.mock.call('enter index - single: '),
        unittest.mock.call('enter multiple indexes, separated by spaces - multi-option: '),
        unittest.mock.call('enter multiple indexes, separated by spaces - multi-option: '),
        unittest.mock.call('enter multiple indexes, separated by spaces - multi-option: '),
        unittest.mock.call('enter multiple values, separated by one or more spaces ,'" etc - multi-text: '),
        unittest.mock.call('enter value y/n - yah: '),
        unittest.mock.call('enter value y/n - sure: '),
        unittest.mock.call('enter value y/n - nah: ')
    ])
input(field, prompt=None, default=None)

Get inputs

Parameters:
  • field – field

  • prompt – prompt

  • default – default

Usage

Taken from its unittest:

@unittest.mock.patch("builtins.input")
@unittest.mock.patch("readline.set_pre_input_hook")
def test_input(self, mock_hook, mock_input):

    field = opengui.Field(name="a")

    mock_input.return_value = "b"

    cli = opengui.Cli()

    self.assertEqual(cli.input(field), "b")

    mock_input.assert_has_calls([
        unittest.mock.call("a: "),
    ])

    self.assertEqual(cli.input(field, "c: "), "b")

    mock_input.assert_has_calls([
        unittest.mock.call("c: "),
    ])
question() opengui.Field

Returns teh next question, transformed by yeas

Return type:

Field

Usage

Taken from its unittest:

cli = opengui.Cli(
    fields=[
        {"name": "a", "label": "{{ lab }}", "stuff": "{[ {{ people }} ]}"},
        {"name": "b"}
    ],
    values={"lab": "A", "people": "things", "things": [1, 2, 3]}
)

self.assertEqual(cli.question().to_dict(), {
    "name": "a",
    "label": "A",
    "stuff": [1, 2, 3]
})