Example #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()
Example #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"))
Example #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"))
Example #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()
Example #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()
Example #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)
Example #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)
Example #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")
Example #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)
Example #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)
Example #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)
Example #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
Example #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)
Example #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)
Example #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)
Example #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
Example #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
Example #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
Example #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
Example #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
Example #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)
Example #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"))
Example #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"))
Example #24
0
def test_add_option(builder):
    opt = Option("option")

    builder.add_option(opt)

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

    builder.add_option(opt)

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

    builder.add_option(opt)

    assert builder.has_option("option")
Example #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"))
Example #28
0
def test_get_option_by_short_name(builder):
    opt = Option("option", "o")

    builder.add_option(opt)

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

    builder.add_option(opt)

    assert opt == builder.get_option("option")
Example #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"))