Exemplo n.º 1
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)
Exemplo n.º 2
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))
Exemplo n.º 3
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))
Exemplo n.º 4
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"))
Exemplo n.º 5
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)
Exemplo n.º 6
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)
Exemplo n.º 7
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)
Exemplo n.º 8
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)
Exemplo n.º 9
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)
Exemplo n.º 10
0
def test_parse_multi_valued_arguments_ignores_missing_command_names(parser):
    builder = ArgsFormatBuilder()
    server = CommandName("server")
    add = CommandName("add")
    builder.add_command_names(server, add)
    builder.add_argument(Argument("multi", Argument.MULTI_VALUED))
    fmt = builder.format

    args = parser.parse(StringArgs("server one two three"), fmt)

    assert {"multi": ["one", "two", "three"]} == args.arguments(False)
    assert {} == args.options(False)
Exemplo n.º 11
0
def test_parse_does_not_fail_if_too_many_arguments_and_lenient(parser):
    builder = ArgsFormatBuilder()
    server = CommandName("server")
    add = CommandName("add")
    builder.add_command_names(server, add)
    builder.add_argument(Argument("argument1"))
    fmt = builder.format

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

    assert {"argument1": "foo"} == args.arguments(False)
    assert {} == args.options(False)
Exemplo n.º 12
0
def test_parse_arguments_ignores_missing_command_name_aliases(parser):
    builder = ArgsFormatBuilder()
    server = CommandName("server", ["server-alias"])
    add = CommandName("add", ["add-alias"])
    builder.add_command_names(server, add)
    builder.add_argument(Argument("argument1"))
    builder.add_argument(Argument("argument2"))
    fmt = builder.format

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

    assert {"argument1": "foo", "argument2": "bar"} == args.arguments(False)
    assert {} == args.options(False)
Exemplo n.º 13
0
def test_parse_arguments(parser):
    builder = ArgsFormatBuilder()
    server = CommandName("server")
    add = CommandName("add")
    builder.add_command_names(server, add)
    builder.add_argument(Argument("argument1"))
    builder.add_argument(Argument("argument2"))
    fmt = builder.format

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

    assert {"argument1": "foo", "argument2": "bar"} == args.arguments(False)
    assert {} == args.options(False)
Exemplo n.º 14
0
def test_parse_does_not_fail_if_missing_required_argument_and_lenient(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

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

    assert {"argument1": "foo"} == args.arguments(False)
    assert {} == args.options(False)
Exemplo n.º 15
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)