Exemple #1
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 #2
0
def test_get_argument_by_position_from_base_definition(base_format_builder):
    arg = Argument("argument")
    base_format_builder.add_argument(arg)

    builder = ArgsFormatBuilder(base_format_builder.format)

    assert arg == builder.get_argument(0)
Exemple #3
0
def test_has_required_argument_with_base_definition(base_format_builder):
    base_format_builder.add_argument(Argument("argument", Argument.REQUIRED))

    builder = ArgsFormatBuilder(base_format_builder.format)

    assert builder.has_required_argument()
    assert not builder.has_required_argument(False)
Exemple #4
0
def test_has_optional_argument_with_base_definition(base_format_builder):
    base_format_builder.add_argument(Argument("argument", Argument.OPTIONAL))

    builder = ArgsFormatBuilder(base_format_builder.format)

    assert builder.has_optional_argument()
    assert not builder.has_optional_argument(False)
Exemple #5
0
def test_has_multi_valued_argument_with_base_definition(base_format_builder):
    base_format_builder.add_argument(Argument("argument", Argument.MULTI_VALUED))

    builder = ArgsFormatBuilder(base_format_builder.format)

    assert builder.has_multi_valued_argument()
    assert not builder.has_multi_valued_argument(False)
Exemple #6
0
def test_fail_if_adding_argument_with_existing_name_in_base_definition(
    base_format_builder, ):
    base_format_builder.add_argument(Argument("argument", Argument.REQUIRED))

    builder = ArgsFormatBuilder(base_format_builder.format)

    with pytest.raises(CannotAddArgumentException):
        builder.add_argument(Argument("argument", Argument.OPTIONAL))
Exemple #7
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 #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_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 #10
0
def test_fail_if_adding_required_argument_after_optional_argument_in_base_definition(
    base_format_builder, ):
    base_format_builder.add_argument(Argument("argument1"))

    builder = ArgsFormatBuilder(base_format_builder.format)

    with pytest.raises(CannotAddArgumentException):
        builder.add_argument(Argument("argument2", Argument.REQUIRED))
Exemple #11
0
def test_get_argument_fails_if_in_base_definition_but_include_base_disabled(
    base_format_builder, ):
    arg = Argument("argument")
    base_format_builder.add_argument(arg)

    builder = ArgsFormatBuilder(base_format_builder.format)

    with pytest.raises(NoSuchArgumentException):
        builder.get_argument("argument", False)
Exemple #12
0
def test_fail_if_adding_optional_argument_after_multi_valued_argument_in_base_definition(
    base_format_builder, ):
    base_format_builder.add_argument(
        Argument("argument1", Argument.MULTI_VALUED))

    builder = ArgsFormatBuilder(base_format_builder.format)

    with pytest.raises(CannotAddArgumentException):
        builder.add_argument(Argument("argument2"))
Exemple #13
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 #14
0
def test_option_fails_if_invalid_option(parser):
    builder = ArgsFormatBuilder()
    server = CommandName("server")
    add = CommandName("add")
    builder.add_command_names(server, add)
    fmt = builder.format

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

    assert 'The "--foo" option does not exist.' == str(e.value)
Exemple #15
0
def test_parse_option_stops_parsing_if_invalid_option_and_lenient(parser):
    builder = ArgsFormatBuilder()
    server = CommandName("server")
    add = CommandName("add")
    builder.add_command_names(server, add)
    fmt = builder.format

    args = parser.parse(StringArgs("server add --foo bar"), fmt, True)

    assert {} == args.arguments(False)
    assert {} == args.options(False)
Exemple #16
0
def test_parse_command_names(parser):
    builder = ArgsFormatBuilder()
    server = CommandName("server")
    add = CommandName("add")
    builder.add_command_names(server, add)
    fmt = builder.format

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

    assert [server, add] == args.command_names
    assert {} == args.arguments(False)
    assert {} == args.options(False)
Exemple #17
0
def test_parse_ignores_missing_command_names(parser):
    builder = ArgsFormatBuilder()
    server = CommandName("server", ["server-alias"])
    add = CommandName("add", ["add-alias"])
    builder.add_command_names(server, add)
    fmt = builder.format

    args = parser.parse(StringArgs(""), fmt)

    assert [server, add] == args.command_names
    assert {} == args.arguments(False)
    assert {} == args.options(False)
Exemple #18
0
    def _render_help(self, layout):  # type: (BlockLayout) -> None
        help = self._application.config.help
        commands = self._application.named_commands
        global_args_format = self._application.global_args_format

        builder = ArgsFormatBuilder()
        builder.add_argument(
            Argument("command", Argument.REQUIRED, "The command to execute"))
        builder.add_argument(
            Argument("arg", Argument.MULTI_VALUED,
                     "The arguments of the command"))
        builder.add_options(*global_args_format.get_options().values())

        args_format = builder.format

        self._render_name(layout, self._application)
        self._render_usage(layout, self._application, args_format)
        self._render_arguments(layout, args_format.get_arguments().values())

        if args_format.has_options():
            self._render_global_options(layout,
                                        args_format.get_options().values())

        if not commands.is_empty():
            self._render_commands(layout, commands)

        if help:
            self._render_description(layout, help)
Exemple #19
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 #20
0
def test_has_command_names_with_base_definition(base_format_builder):
    base_format_builder.add_command_name(CommandName("server"))

    builder = ArgsFormatBuilder(base_format_builder.format)

    assert builder.has_command_names()
    assert not builder.has_command_names(False)

    builder.add_command_name(CommandName("add"))

    assert builder.has_command_names()
    assert builder.has_command_names(False)
Exemple #21
0
def test_has_arguments_with_base_definition(base_format_builder):
    arg1 = Argument("argument1")
    arg2 = Argument("argument2")
    base_format_builder.add_argument(arg1)

    builder = ArgsFormatBuilder(base_format_builder.format)

    assert builder.has_arguments()
    assert not builder.has_arguments(False)

    builder.add_argument(arg2)

    assert builder.has_arguments()
    assert builder.has_arguments(False)
Exemple #22
0
def test_has_argument_at_position_from_base_definition(base_format_builder):
    arg1 = Argument("argument1")
    arg2 = Argument("argument2")
    base_format_builder.add_argument(arg1)

    builder = ArgsFormatBuilder(base_format_builder.format)
    builder.add_argument(arg2)

    assert builder.has_argument(0)
    assert builder.has_argument(1)
    assert not builder.has_argument(1, False)
    assert builder.has_argument(0, False)
Exemple #23
0
def test_parse_fail_if_too_many_arguments_with_missing_command_name(parser):
    builder = ArgsFormatBuilder()
    server = CommandName("server")
    add = CommandName("add")
    builder.add_command_names(server, add)
    builder.add_argument(Argument("argument1"))
    fmt = builder.format

    with pytest.raises(CannotParseArgsException):
        parser.parse(StringArgs("server foo bar"), fmt)
Exemple #24
0
    def parse(self,
              args,
              fmt,
              lenient=False):  # type: (RawArgs, ArgsFormat, bool) -> Args
        builder = ArgsFormatBuilder()
        builder.set_command_names(*fmt.get_command_names())
        builder.set_arguments(*fmt.get_arguments().values())
        fmt = builder.format

        return super(RunArgsParser, self).parse(args, fmt, True)
Exemple #25
0
def test_get_command_names_with_base_definition(base_format_builder):
    server = CommandName("server")
    add = CommandName("add")

    base_format_builder.add_command_name(server)

    builder = ArgsFormatBuilder(base_format_builder.format)
    builder.add_command_name(add)

    assert [server, add] == builder.get_command_names()
    assert [add] == builder.get_command_names(False)
Exemple #26
0
def test_parse_fails_if_missing_required_argument(parser):
    builder = ArgsFormatBuilder()
    server = CommandName("server")
    add = CommandName("add")
    builder.add_command_names(server, add)
    builder.add_argument(Argument("argument1", Argument.REQUIRED))
    builder.add_argument(Argument("argument2", Argument.REQUIRED))
    fmt = builder.format

    with pytest.raises(CannotParseArgsException):
        parser.parse(StringArgs("server add foo"), fmt)
Exemple #27
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 #28
0
def test_parse_option_up_to_invalid_option_if_lenient(parser):
    builder = ArgsFormatBuilder()
    server = CommandName("server")
    add = CommandName("add")
    builder.add_command_names(server, add)
    builder.add_argument(Argument("argument"))
    fmt = builder.format

    args = parser.parse(StringArgs("server add bar --foo"), fmt, True)

    assert {"argument": "bar"} == args.arguments(False)
    assert {} == args.options(False)
Exemple #29
0
def test_get_arguments_with_base_arguments(base_format_builder):
    arg1 = Argument("argument1")
    arg2 = Argument("argument2")

    base_format_builder.add_argument(arg1)

    builder = ArgsFormatBuilder(base_format_builder.format)

    builder.add_argument(arg2)

    assert {"argument1": arg1, "argument2": arg2} == builder.get_arguments()
    assert {"argument2": arg2} == builder.get_arguments(False)
Exemple #30
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)