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"))
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"))
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)
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)
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)
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)
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)