Beispiel #1
0
def _add_argument(
    parser: ArgumentParser,
    name: str,
    cli_help: Optional[str],
    dest: Optional[str] = None,
    action: str = "store",
    **kwargs,
) -> None:
    parser.add_argument(
        canonical_arg_name(name),
        action=action,
        dest=dest or name,
        help=cli_help,
        **kwargs,
    )
Beispiel #2
0
def _update_answers_validate(
    question: Union[BasicQuestion, Choice],
    cli_values: CliResults,
    answers: MutableAnswers,
) -> None:
    if not question.should_ask(answers):
        return
    value = cast(str, cli_values.get(question.name))
    if value is None:
        value = to_value(question.default, answers, str)
    result = question.validate(value, answers)
    if not result.valid:
        raise CliException.invalid_value(
            value, canonical_arg_name(question.name), result.error
        )
    answers[question.name] = value
Beispiel #3
0
def test_create_parser__confirm_no_option__false():
    parser = create_parser([Confirm(SOME_NAME, SOME_STRING)])

    result = parser.parse_args([canonical_arg_name(f"no-{SOME_NAME}")])

    assert vars(result)[SOME_NAME] is False
from argparse import Namespace

from columbo._interaction import (
    BasicQuestion,
    Choice,
    Confirm,
    Question,
    canonical_arg_name,
)

SOME_BOOL = True
SOME_OTHER_BOOL = False
SOME_NAME = "my-test-value"
SOME_NAME_WITH_SPACES = "my test value"
SOME_ARG_NAME = canonical_arg_name(SOME_NAME)
SOME_STRING = "hello"
SOME_OTHER_STRING = "good-bye"
SOME_ANSWERS = {"a": "one", "b": "two"}
SOME_OPTIONS = ["x", "y", "z"]
SOME_DEFAULT = "x"
SOME_NON_DEFAULT_OPTION = "y"
SOME_INVALID_OPTION = "NOT_VALID_OPTION"
SOME_INVALID_ARG_NAME = "--NOT_VALID_OPTION"


def some_dynamic_string(answers):
    return f"--{answers['a']}--"


def some_dynamic_options(answers):
    return [f"--{x}--" for x in answers.values()]
Beispiel #5
0
def test__canonical_arg_name__nonstandard_characters__normalized(
        description, name, expected_arg):
    assert canonical_arg_name(name) == expected_arg, description