Exemple #1
0
def test_get_options(builder):
    opt1 = Option("option1")
    opt2 = Option("option2")

    builder.add_options(opt1, opt2)

    assert {"option1": opt1, "option2": opt2} == builder.get_options()
Exemple #2
0
def test_add_option_fails_if_same_short_name_in_base_format(base_format_builder):
    base_format_builder.add_option(Option("option1", "a"))

    builder = ArgsFormatBuilder(base_format_builder.format)

    with pytest.raises(CannotAddOptionException):
        builder.add_option(Option("option2", "a"))
Exemple #3
0
def test_add_option_fails_if_same_long_name_as_command_option_in_base_format(
    base_format_builder, ):
    base_format_builder.add_option(Option("option", "a"))

    builder = ArgsFormatBuilder(base_format_builder.format)

    with pytest.raises(CannotAddOptionException):
        builder.add_option(Option("option", "b"))
Exemple #4
0
def test_add_option_preserve_existing_options(builder):
    opt1 = Option("option1")
    opt2 = Option("option2")

    builder.add_option(opt1)
    builder.add_option(opt2)

    assert {"option1": opt1, "option2": opt2} == builder.get_options()
Exemple #5
0
def test_set_options(builder):
    opt1 = Option("option1")
    opt2 = Option("option2")
    opt3 = Option("option3")

    builder.add_option(opt1)
    builder.set_options(opt2, opt3)

    assert {"option2": opt2, "option3": opt3} == builder.get_options()
Exemple #6
0
def test_has_options_with_base_format(base_format_builder):
    base_format_builder.add_option(Option("option"))
    builder = ArgsFormatBuilder(base_format_builder.format)

    assert builder.has_options()
    assert not builder.has_options(False)

    builder.add_option(Option("option2"))

    assert builder.has_options()
    assert builder.has_options(False)
Exemple #7
0
def test_get_options_with_base_format(base_format_builder):
    opt1 = Option("option1")
    opt2 = Option("option2")

    base_format_builder.add_option(opt1)

    builder = ArgsFormatBuilder(base_format_builder.format)

    builder.add_option(opt2)

    assert {"option1": opt1, "option2": opt2} == builder.get_options()
    assert {"option2": opt2} == builder.get_options(False)
Exemple #8
0
def test_get_option_by_short_name_from_base_format(base_format_builder):
    opt = Option("option", "o")

    base_format_builder.add_option(opt)

    builder = ArgsFormatBuilder(base_format_builder.format)

    assert opt == builder.get_option("o")
Exemple #9
0
def test_has_options(builder):
    assert not builder.has_options()
    assert not builder.has_options(False)

    builder.add_option(Option("option"))

    assert builder.has_options()
    assert builder.has_options(False)
Exemple #10
0
def test_get_option_fails_if_in_base_format_but_include_base_disabled(
    base_format_builder, ):
    base_format_builder.add_option(Option("option"))

    builder = ArgsFormatBuilder(base_format_builder.format)

    with pytest.raises(NoSuchOptionException):
        builder.get_option("option", False)
Exemple #11
0
def test_has_option_by_short_name_from_base_format(base_format_builder):
    opt = Option("option", "o")

    base_format_builder.add_option(opt)

    builder = ArgsFormatBuilder(base_format_builder.format)

    assert builder.has_option("o")
    assert not builder.has_option("o", False)
Exemple #12
0
def test_create():
    opt = Option("option")

    assert "option" == opt.long_name
    assert opt.short_name is None
    assert opt.is_long_name_preferred()
    assert not opt.is_short_name_preferred()
    assert not opt.accepts_value()
    assert not opt.is_value_required()
    assert not opt.is_value_optional()
    assert not opt.is_multi_valued()
    assert opt.default is None
    assert "..." == opt.value_name
Exemple #13
0
def test_parse_short_option_fails_if_missing_value(parser):
    builder = ArgsFormatBuilder()
    server = CommandName("server")
    add = CommandName("add")
    builder.add_command_names(server, add)
    builder.add_option(Option("option", "o", flags=Option.REQUIRED_VALUE))
    fmt = builder.format

    with pytest.raises(CannotParseArgsException) as e:
        parser.parse(StringArgs("server add -o"), fmt)

    assert 'The "--option" option requires a value.' == str(e.value)
Exemple #14
0
def test_parse_short_option_with_value2(parser):
    builder = ArgsFormatBuilder()
    server = CommandName("server")
    add = CommandName("add")
    builder.add_command_names(server, add)
    builder.add_option(Option("option", "o", flags=Option.OPTIONAL_VALUE))
    fmt = builder.format

    args = parser.parse(StringArgs("server add -ofoo"), fmt)

    assert {} == args.arguments(False)
    assert {"option": "foo"} == args.options(False)
Exemple #15
0
def test_parse_short_option_without_value(parser):
    builder = ArgsFormatBuilder()
    server = CommandName("server")
    add = CommandName("add")
    builder.add_command_names(server, add)
    builder.add_option(Option("option", "o"))
    fmt = builder.format

    args = parser.parse(StringArgs("server add -o"), fmt)

    assert {} == args.arguments(False)
    assert {"option": True} == args.options(False)
Exemple #16
0
def test_required_value_with_default():
    opt = Option("option", flags=Option.REQUIRED_VALUE, default="Default")

    assert opt.accepts_value()
    assert opt.is_value_required()
    assert not opt.is_value_optional()
    assert not opt.is_multi_valued()
    assert "Default" == opt.default
Exemple #17
0
def test_multi_valued():
    opt = Option("option", flags=Option.MULTI_VALUED)

    assert opt.accepts_value()
    assert opt.is_value_required()
    assert not opt.is_value_optional()
    assert opt.is_multi_valued()
    assert [] == opt.default
Exemple #18
0
def test_optional_value():
    opt = Option("option", flags=Option.OPTIONAL_VALUE)

    assert opt.accepts_value()
    assert not opt.is_value_required()
    assert opt.is_value_optional()
    assert not opt.is_multi_valued()
    assert opt.default is None
Exemple #19
0
def test_required_value():
    opt = Option("option", flags=Option.REQUIRED_VALUE)

    assert opt.accepts_value()
    assert opt.is_value_required()
    assert not opt.is_value_optional()
    assert not opt.is_multi_valued()
    assert opt.default is None
Exemple #20
0
def test_multi_valued_with_default():
    opt = Option("option", flags=Option.MULTI_VALUED, default=["foo", "bar"])

    assert opt.accepts_value()
    assert opt.is_value_required()
    assert not opt.is_value_optional()
    assert opt.is_multi_valued()
    assert ["foo", "bar"] == opt.default
Exemple #21
0
def option(
    long_name,
    short_name=None,
    description=None,
    flag=True,
    value_required=True,
    multiple=False,
    default=None,
):  # type: (str, Optional[str], Optional[str], bool, bool, bool, Optional[Any]) -> Option
    if flag:
        flags = Option.NO_VALUE
    elif value_required:
        flags = Option.REQUIRED_VALUE
    else:
        flags = Option.OPTIONAL_VALUE

    if multiple and not flag:
        flags |= Option.MULTI_VALUED

    return Option(long_name, short_name, flags, description, default)
Exemple #22
0
def test_add_option_fails_if_same_long_name_as_command_option_alias(builder):
    builder.add_command_option(CommandOption("option", "a", ["alias"]))

    with pytest.raises(CannotAddOptionException):
        builder.add_option(Option("alias", "b"))
Exemple #23
0
def test_add_command_fails_if_option_with_same_long_name_as_other_option(
        builder):
    builder.add_option(Option("option", "a"))

    with pytest.raises(CannotAddOptionException):
        builder.add_command_option(CommandOption("option", "b"))
Exemple #24
0
def test_add_option(builder):
    opt = Option("option")

    builder.add_option(opt)

    assert {"option": opt} == builder.get_options()
Exemple #25
0
def test_has_option_by_short_name(builder):
    opt = Option("option", "o")

    builder.add_option(opt)

    assert builder.has_option("o")
Exemple #26
0
def test_has_option(builder):
    opt = Option("option")

    builder.add_option(opt)

    assert builder.has_option("option")
Exemple #27
0
def test_add_option_fails_if_same_long_name(builder):
    builder.add_option(Option("option", "a"))

    with pytest.raises(CannotAddOptionException):
        builder.add_option(Option("option", "b"))
Exemple #28
0
def test_get_option_by_short_name(builder):
    opt = Option("option", "o")

    builder.add_option(opt)

    assert opt == builder.get_option("o")
Exemple #29
0
def test_get_option(builder):
    opt = Option("option")

    builder.add_option(opt)

    assert opt == builder.get_option("option")
Exemple #30
0
def test_add_option_fails_if_same_short_name_as_command_option(builder):
    builder.add_option(CommandOption("option1", "a"))

    with pytest.raises(CannotAddOptionException):
        builder.add_option(Option("option1", "a"))