Exemple #1
0
def test_int_list_mixed():
    parser = janus.ArgParser()
    parser.new_int("int i")
    parser.parse(["--int", "123", "456", "-i", "789"])
    assert parser.len_list("int") == 2
    assert parser.get_list("int")[0] == 123
    assert parser.get_list("int")[1] == 789
Exemple #2
0
def test_multi_options_shortform():
    parser = janus.ArgParser()
    parser.new_flag("bool1")
    parser.new_flag("bool2 b")
    parser.new_str("string1", fallback="default1")
    parser.new_str("string2 s", fallback="default2")
    parser.new_int("int1", fallback=101)
    parser.new_int("int2 i", fallback=202)
    parser.new_float("float1", fallback=1.1)
    parser.new_float("float2 f", fallback=2.2)
    parser.parse([
        "--bool1",
        "-b",
        "--string1", "value1",
        "-s", "value2",
        "--int1", "303",
        "-i", "404",
        "--float1", "3.3",
        "-f", "4.4",
    ])
    assert parser.get("bool1") == True
    assert parser.get("bool2") == True
    assert parser.get("string1") == "value1"
    assert parser.get("string2") == "value2"
    assert parser.get("int1") == 303
    assert parser.get("int2") == 404
    assert parser.get("float1") == 3.3
    assert parser.get("float2") == 4.4
Exemple #3
0
def test_string_list_mixed():
    parser = janus.ArgParser()
    parser.new_str("string s")
    parser.parse(["--string", "foo", "bar", "-s", "baz"])
    assert parser.len_list("string") == 2
    assert parser.get_list("string")[0] == "foo"
    assert parser.get_list("string")[1] == "baz"
Exemple #4
0
def test_positional_args():
    parser = janus.ArgParser()
    parser.parse(["foo", "bar"])
    assert parser.has_args() == True
    assert parser.num_args() == 2
    assert parser.get_args()[0] == "foo"
    assert parser.get_args()[1] == "bar"
Exemple #5
0
def test_command_present():
    parser = janus.ArgParser()
    cmd_parser = parser.new_cmd("cmd", "helptext", lambda p: None)
    parser.parse(["cmd"])
    assert parser.has_cmd() == True
    assert parser.get_cmd_name() == "cmd"
    assert parser.get_cmd_parser() == cmd_parser
Exemple #6
0
def test_float_list_mixed():
    parser = janus.ArgParser()
    parser.new_float("float f")
    parser.parse(["--float", "1.1", "2.2", "-f", "3.3"])
    assert parser.len_list("float") == 2
    assert parser.get_list("float")[0] == 1.1
    assert parser.get_list("float")[1] == 3.3
Exemple #7
0
def test_command_with_options():
    parser = janus.ArgParser()
    cmd_parser = parser.new_cmd("cmd", "helptext", lambda p: None)
    cmd_parser.new_flag("bool")
    cmd_parser.new_str("string")
    cmd_parser.new_int("int")
    cmd_parser.new_float("float")
    parser.parse([
        "cmd",
        "foo",
        "bar",
        "--string",
        "value",
        "--int",
        "202",
        "--float",
        "2.2",
    ])
    assert parser.has_cmd() == True
    assert parser.get_cmd_name() == "cmd"
    assert parser.get_cmd_parser() == cmd_parser
    assert cmd_parser.has_args() == True
    assert cmd_parser.num_args() == 2
    assert cmd_parser["string"] == "value"
    assert cmd_parser["int"] == 202
    assert cmd_parser["float"] == 2.2
Exemple #8
0
def test_condensed_options():
    parser = janus.ArgParser()
    parser.new_flag("bool b")
    parser.new_str("string s", fallback="default")
    parser.new_int("int i", fallback=101)
    parser.new_float("float f", fallback=1.1)
    parser.parse(["-bsif", "value", "202", "2.2"])
    assert parser["bool"] == True
    assert parser["string"] == "value"
    assert parser["int"] == 202
    assert parser["float"] == 2.2
Exemple #9
0
def parse():
    global parser
    parser = janus.ArgParser(helptext, ivy.__version__)

    # Fire the 'cli' event. Plugins can use this event to register their own
    # custom commands and options.
    ivy.hooks.event('cli', parser)

    # Parse the application's command line arguments.
    parser.parse()
    if not parser.has_cmd():
        parser.exit_help()
Exemple #10
0
def test_multi_options_empty():
    parser = janus.ArgParser()
    parser.new_flag("bool1")
    parser.new_flag("bool2 b")
    parser.new_str("string1", fallback="default1")
    parser.new_str("string2 s", fallback="default2")
    parser.new_int("int1", fallback=101)
    parser.new_int("int2 i", fallback=202)
    parser.new_float("float1", fallback=1.1)
    parser.new_float("float2 f", fallback=2.2)
    parser.parse([])
    assert parser.get("bool1") == False
    assert parser.get("bool2") == False
    assert parser.get("string1") == "default1"
    assert parser.get("string2") == "default2"
    assert parser.get("int1") == 101
    assert parser.get("int2") == 202
    assert parser.get("float1") == 1.1
    assert parser.get("float2") == 2.2
Exemple #11
0
def build_module_parser(*, module, version):
    modules = []
    functions = []
    for name, member in inspect.getmembers(module):
        if inspect.ismodule(member):
            modules.append(member)
        elif inspect.isfunction(member):
            functions.append(member)
    if not modules and not functions:
        raise ValueError(
            f"No functions or modules in {module.__name__} - can't build a CLI parser"
        )
    module_doc = (module.__doc__ or "") + "\n\nCommands:\n  " + "\n  ".join(
        [i.__name__.split('.')[-1] for i in modules + functions])
    parser = janus.ArgParser(helptext=module_doc, version=version)
    for this_module in modules:
        add_module_parser(parser, module=this_module)
    for this_function in functions:
        add_function_parser(parser, function=this_function)
    return parser
Exemple #12
0
def parse():
    p = janus.ArgParser()
    p.new_str('n name')
    p.new_str('v version', fallback='0.0.1')
    p.new_str('a author', fallback='Surf')
    p.new_str('l licence', fallback='MIT')
    p.new_str('d description', fallback='')
    p.new_str('p path', fallback='.')
    p.parse()

    commands = p.get_args()
    assert len(commands) == 1, (
        'Please provide exactly one command. '
        'The available commands are "new_ml" and "new_dl".')

    command = commands[0]
    assert command == 'new', 'Use "ocean new -n <PROJECT_NAME>" to create one'

    name = p['name']
    assert not name[0].isdigit(), ('The project name should not start '
                                   'with a numeric character.')

    version = p['version']
    author = p['author']
    licence = p['licence']
    description = p['description']
    path = os.path.abspath(p['path'])

    assert len(name.strip()) > 0, 'Please provide a correct name for a project'

    short_name = ''.join([x.capitalize() for x in name.split()])
    short_name = short_name[0].lower() + short_name[1:]

    create_project(name=name,
                   short_name=short_name,
                   author=author,
                   description=description,
                   version=version,
                   licence=licence,
                   path=path)
Exemple #13
0
def test_command_absent():
    parser = janus.ArgParser()
    cmd_parser = parser.new_cmd("cmd", "helptext", lambda p: None)
    parser.parse([])
    assert parser.has_cmd() == False
Exemple #14
0
def test_option_parsing_switch():
    parser = janus.ArgParser()
    parser.parse(["foo", "--", "--bar", "--baz"])
    assert parser.num_args() == 3
Exemple #15
0
def test_positional_args_as_floats():
    parser = janus.ArgParser()
    parser.parse(["1.1", "11.1"])
    assert parser.get_args_as_floats()[0] == 1.1
    assert parser.get_args_as_floats()[1] == 11.1
Exemple #16
0
def test_positional_args_list_syntax():
    parser = janus.ArgParser()
    parser.parse(["foo", "bar"])
    assert parser[0] == "foo"
    assert parser[1] == "bar"
Exemple #17
0
def test_float_list_missing():
    parser = janus.ArgParser()
    parser.new_float("float")
    parser.parse([])
    assert parser.len_list("float") == 0
Exemple #18
0
def test_bool_list_shortform():
    parser = janus.ArgParser()
    parser.new_flag("bool b")
    parser.parse(["-b", "-bb"])
    assert parser.len_list("bool") == 3
Exemple #19
0
def test_unrecognised_shortform_option():
    parser = janus.ArgParser()
    with pytest.raises(SystemExit):
        parser.parse(["-f"])
Exemple #20
0
def test_string_option_empty():
    parser = janus.ArgParser()
    parser.new_str("string", fallback="default")
    parser.parse([])
    assert parser.get("string") == "default"
Exemple #21
0
def test_float_option_dict_syntax():
    parser = janus.ArgParser()
    parser.new_float("float", fallback=1.1)
    parser.parse(["--float", "2.2"])
    assert parser["float"] == 2.2
Exemple #22
0
def test_bool_option_dict_syntax():
    parser = janus.ArgParser()
    parser.new_flag("bool")
    parser.parse(["--bool"])
    assert parser["bool"] == True
Exemple #23
0
def test_float_option_invalid_value():
    parser = janus.ArgParser()
    parser.new_float("float", fallback=1.1)
    with pytest.raises(SystemExit):
        parser.parse(["--float", "foo"])
Exemple #24
0
def test_bool_list_empty():
    parser = janus.ArgParser()
    parser.new_flag("bool")
    parser.parse([])
    assert parser.len_list("bool") == 0
Exemple #25
0
def test_bool_list_longform():
    parser = janus.ArgParser()
    parser.new_flag("bool")
    parser.parse(["--bool", "--bool", "--bool"])
    assert parser.len_list("bool") == 3
Exemple #26
0
def test_positional_args_empty():
    parser = janus.ArgParser()
    parser.parse([])
    assert parser.has_args() == False
Exemple #27
0
def test_bool_list_mixed():
    parser = janus.ArgParser()
    parser.new_flag("bool b")
    parser.parse(["--bool", "-bb"])
    assert parser.len_list("bool") == 3
Exemple #28
0
def test_float_option_negative_value():
    parser = janus.ArgParser()
    parser.new_float("float", fallback=1.1)
    parser.parse(["--float", "-2.2"])
    assert parser.get("float") == -2.2
Exemple #29
0
def test_string_option_missing():
    parser = janus.ArgParser()
    parser.new_str("string", fallback="default")
    parser.parse(["foo", "bar"])
    assert parser.get("string") == "default"
Exemple #30
0
def test_unrecognised_longform_option():
    parser = janus.ArgParser()
    with pytest.raises(SystemExit):
        parser.parse(["--foo"])