Example #1
0
def test_add_output_single_value_option_multiplw_inputs():
    ops = Options()
    o = ops.add_output("test", value=[1, 2], nargs=1)
    assert o.hidden
    assert o.raw() == [1, 2]

    with pytest.raises(ValueError):
        o.get()
Example #2
0
def test_add_output_single_value_option_multiplw_inputs():
    ops = Options()
    o = ops.add_output("test", value=[1, 2], nargs=1)
    assert o.hidden
    assert o.raw() == [1, 2]

    with pytest.raises(ValueError):
        o.get()
Example #3
0
def test_docopt_parser_with_tabs():
    help_string = """\
Some Tool

Usage: tools [-t] [-i <input>...] <cmd>

Inputs:
	-i, --input <input>...	The input

Options:
	-t	Some boolean
	<cmd>	The command
    """
    opts = Options.from_docopt(help_string)

    assert len(opts) == 3  # the two + help
    assert opts['input'] is not None
    assert opts['input'].nargs == "*"
    assert not opts['input'].required
    assert opts['t'] is not None
    assert opts['t'].nargs == 0
    assert not opts['t'].required
    assert opts['cmd'] is not None
    assert opts['cmd'].nargs == 1
    assert opts['cmd'].required
Example #4
0
def test_docopt_parser_with_defaults():
    import sys
    help_string = """\
    Some Tool

    Usage: tools [-t <tool>] [-i <input>] [-o <output>]

    Inputs:
        -i, --input <input>    The input
                               [Default: stdin]

    Outputs:
        -o, --output <output>  The output
                               [Default: stdout]

    Options:
        -t, --tool <tool>      Some option
                               [Default: mytool]

    """
    opts = Options.from_docopt(help_string)

    assert opts['input'].option_type == TYPE_INPUT
    assert opts['output'].option_type == TYPE_OUTPUT
    assert opts['input'].get() == ""
    assert str(opts['input']) == ""
    assert opts['input'].raw() == sys.stdin
    assert opts['output'].raw() == sys.stdout
    assert opts['tool'].raw() == "mytool"
Example #5
0
def test_options_setting_as_property():
    help_string = """\
Some Tool

Usage: tools --name <input>

Options:
    -n, --name <name>    The input
    """
    opts = Options.from_docopt(help_string)
    assert opts is not None

    opts["name"].set("Test")
    assert opts["name"].get() == "Test"
    assert opts.name.get() == "Test"
    assert opts.name == "Test"

    opts["name"] = "Test2"
    assert opts["name"].get() == "Test2"
    assert opts.name.get() == "Test2"
    assert opts.name == "Test2"

    opts.name = "Test3"
    assert opts["name"].get() == "Test3"
    assert opts.name.get() == "Test3"
    assert opts.name == "Test3"
Example #6
0
def test_user_specified_option_and_default_recovery():
    from argparse import ArgumentParser
    p = ArgumentParser()
    p.add_argument("-t", "--test", action="store_true")
    p.add_argument("--int", type=int, default=1)
    p.add_argument("-i", "--input", help="The input")
    p.add_argument("-o",
                   "--output",
                   nargs="*",
                   help="The output",
                   required=True)
    opts = Options.from_argparse(p)
    opts.parse([])
    assert not opts['test'].raw()
    assert opts['input'].raw() is None
    assert opts['output'].raw() is None
    assert not opts['output'].user_specified
    assert not opts['input'].user_specified
    assert not opts['test'].user_specified
    opts.parse(['-t', '-i', 'myin', '-o', 'myout'])
    assert opts['test'].raw()
    assert opts['input'].raw() == 'myin'
    assert opts['output'].raw() == ['myout']
    assert opts['output'].user_specified
    assert opts['input'].user_specified
    assert opts['test'].user_specified
Example #7
0
def test_docopt_parser_with_defaults():
    import sys

    help_string = """\
    Some Tool

    Usage: tools [-t <tool>] [-i <input>] [-o <output>]

    Inputs:
        -i, --input <input>    The input
                               [Default: stdin]

    Outputs:
        -o, --output <output>  The output
                               [Default: stdout]

    Options:
        -t, --tool <tool>      Some option
                               [Default: mytool]

    """
    opts = Options.from_docopt(help_string)

    assert opts["input"].option_type == TYPE_INPUT
    assert opts["output"].option_type == TYPE_OUTPUT
    assert opts["input"].get() == ""
    assert str(opts["input"]) == ""
    assert opts["input"].raw() == sys.stdin
    assert opts["output"].raw() == sys.stdout
    assert opts["tool"].raw() == "mytool"
Example #8
0
def test_docopt_parser_with_tabs():
    help_string = """\
Some Tool

Usage: tools [-t] [-i <input>...] <cmd>

Inputs:
	-i, --input <input>...	The input

Options:
	-t	Some boolean
	<cmd>	The command
    """
    opts = Options.from_docopt(help_string)

    assert len(opts) == 3  # the two + help
    assert opts["input"] is not None
    assert opts["input"].nargs == "*"
    assert not opts["input"].required
    assert opts["t"] is not None
    assert opts["t"].nargs == 0
    assert not opts["t"].required
    assert opts["cmd"] is not None
    assert opts["cmd"].nargs == 1
    assert opts["cmd"].required
Example #9
0
def test_options_setting_as_property():
    help_string = """\
Some Tool

Usage: tools --name <input>

Options:
    -n, --name <name>    The input
    """
    opts = Options.from_docopt(help_string)
    assert opts is not None

    opts['name'].set("Test")
    assert opts['name'].get() == "Test"
    assert opts.name.get() == "Test"
    assert opts.name == "Test"

    opts['name'] = "Test2"
    assert opts['name'].get() == "Test2"
    assert opts.name.get() == "Test2"
    assert opts.name == "Test2"

    opts.name = 'Test3'
    assert opts['name'].get() == "Test3"
    assert opts.name.get() == "Test3"
    assert opts.name == "Test3"
Example #10
0
def test_argparse_parser_usage():
    from argparse import ArgumentParser

    p = ArgumentParser(prog="py.test")
    p.add_argument("-t", "--test", action="store_true")
    p.add_argument("-i", "--input", help="The input")
    p.add_argument("-o", "--output", nargs="*", help="The output", required=True)
    opts = Options.from_argparse(p)
    assert opts.usage() == "usage: py.test [-h] [-t] [-i INPUT] " "-o [OUTPUT [OUTPUT ...]]"
Example #11
0
def test_argparse_parser_usage():
    from argparse import ArgumentParser
    p = ArgumentParser(prog="py.test")
    p.add_argument("-t", "--test", action="store_true")
    p.add_argument("-i", "--input", help="The input")
    p.add_argument("-o",
                   "--output",
                   nargs="*",
                   help="The output",
                   required=True)
    opts = Options.from_argparse(p)
    assert opts.usage() == "usage: py.test [-h] [-t] [-i INPUT] "\
                           "-o [OUTPUT [OUTPUT ...]]"
Example #12
0
def test_streamable_from_default_no_list():
    help_string = """\
    Some Tool

    Usage: tools [-i <input>] -o <output>

    Options:
        -i, --input <input>    The input
                               [Default: stdin]
        -o, --output <output>  The output
    """
    opts = Options.from_docopt(help_string)
    assert not opts['output'].streamable
    assert opts['input'].streamable
Example #13
0
def test_streamable_from_default_no_list():
    help_string = """\
    Some Tool

    Usage: tools [-i <input>] -o <output>

    Options:
        -i, --input <input>    The input
                               [Default: stdin]
        -o, --output <output>  The output
    """
    opts = Options.from_docopt(help_string)
    assert not opts["output"].streamable
    assert opts["input"].streamable
Example #14
0
def test_get_usage_from_docopt():
    help_string = """\
    Some Tool

    Usage: tools [-i <input>] [-o <output>]

    Options:
        -i, --input <input>    The input
                               [Default: stdin]
        -o, --output <output>  The output
                               [Default: stdout]
    """
    opts = Options.from_docopt(help_string)
    assert opts.usage() == "Usage: tools [-i <input>] [-o <output>]"
Example #15
0
def test_get_usage_from_docopt():
    help_string = """\
    Some Tool

    Usage: tools [-i <input>] [-o <output>]

    Options:
        -i, --input <input>    The input
                               [Default: stdin]
        -o, --output <output>  The output
                               [Default: stdout]
    """
    opts = Options.from_docopt(help_string)
    assert opts.usage() == "Usage: tools [-i <input>] [-o <output>]"
Example #16
0
def test_call_to_help():
    help_string = """\
    Some Tool

    Usage: tools [-i <input>] [-o <output>]

    Options:
        -i, --input <input>    The input
                               [Default: stdin]
        -o, --output <output>  The output
                               [Default: stdout]
    """
    opts = Options.from_docopt(help_string)
    with pytest.raises(ParserException):
        opts.parse(["-h"])
Example #17
0
def test_parsing_args():
    help_string = """\
    Some Tool

    Usage: tools [-i <input>] [-o <output>]

    Options:
        -i, --input <input>    The input
                               [Default: stdin]
        -o, --output <output>  The output
                               [Default: stdout]
    """
    opts = Options.from_docopt(help_string)
    opts.parse(["-i", "testme"])
    assert opts["input"].raw() == "testme"
Example #18
0
def test_unkonw_argument():
    help_string = """\
    Some Tool

    Usage: tools [-i <input>] [-o <output>]

    Options:
        -i, --input <input>    The input
                               [Default: stdin]
        -o, --output <output>  The output
                               [Default: stdout]
    """
    opts = Options.from_docopt(help_string)
    with pytest.raises(ParserException):
        opts.parse(['-x', 'testme'])
Example #19
0
def test_call_to_help():
    help_string = """\
    Some Tool

    Usage: tools [-i <input>] [-o <output>]

    Options:
        -i, --input <input>    The input
                               [Default: stdin]
        -o, --output <output>  The output
                               [Default: stdout]
    """
    opts = Options.from_docopt(help_string)
    with pytest.raises(ParserException):
        opts.parse(['-h'])
Example #20
0
def test_parsing_args():
    help_string = """\
    Some Tool

    Usage: tools [-i <input>] [-o <output>]

    Options:
        -i, --input <input>    The input
                               [Default: stdin]
        -o, --output <output>  The output
                               [Default: stdout]
    """
    opts = Options.from_docopt(help_string)
    opts.parse(['-i', 'testme'])
    assert opts['input'].raw() == 'testme'
Example #21
0
def test_argparse_parser():
    from argparse import ArgumentParser

    p = ArgumentParser()
    p.add_argument("-t", "--test", action="store_true")
    p.add_argument("-i", "--input", help="The input")
    p.add_argument("-o", "--output", nargs="*", help="The output", required=True)
    opts = Options.from_argparse(p)
    assert len(opts) == 4  # teh two + help
    assert opts["input"] is not None
    assert opts["input"].nargs == 1
    assert opts["test"] is not None
    assert opts["test"].nargs == 0
    assert opts["output"] is not None
    assert opts["output"].nargs == "*"
    assert opts["output"].required
Example #22
0
def test_list_arguments_for_fan_out():
    help_string = """\
    Some Tool

    Usage: tools [-i <input>] [-o <output>]

    Options:
        -i, --input <input>    The input
                               [Default: stdin]
        -o, --output <output>  The output
                               [Default: stdout]
    """
    opts = Options.from_docopt(help_string)
    opts.parse(['-i', 'testme', 'testme2'])
    assert opts['input'].raw() == ['testme', 'testme2']
    with pytest.raises(ValueError):
        opts['input'].get()
Example #23
0
def test_list_arguments_for_fan_out():
    help_string = """\
    Some Tool

    Usage: tools [-i <input>] [-o <output>]

    Options:
        -i, --input <input>    The input
                               [Default: stdin]
        -o, --output <output>  The output
                               [Default: stdout]
    """
    opts = Options.from_docopt(help_string)
    opts.parse(["-i", "testme", "testme2"])
    assert opts["input"].raw() == ["testme", "testme2"]
    with pytest.raises(ValueError):
        opts["input"].get()
Example #24
0
def test_docopt_parser_shorts():
    help_string = """\
    Some Tool

    Usage: tools [-t] [--input <input>...] <cmd>
    """
    opts = Options.from_docopt(help_string)

    assert len(opts) == 3  # the two + help
    assert opts["input"] is not None
    assert opts["input"].nargs == "*"
    assert not opts["input"].required
    assert opts["t"] is not None
    assert opts["t"].nargs == 0
    assert not opts["t"].required
    assert opts["cmd"] is not None
    assert opts["cmd"].nargs == 1
    assert opts["cmd"].required
Example #25
0
def test_docopt_parser_shorts():
    help_string = """\
    Some Tool

    Usage: tools [-t] [--input <input>...] <cmd>
    """
    opts = Options.from_docopt(help_string)

    assert len(opts) == 3  # the two + help
    assert opts['input'] is not None
    assert opts['input'].nargs == "*"
    assert not opts['input'].required
    assert opts['t'] is not None
    assert opts['t'].nargs == 0
    assert not opts['t'].required
    assert opts['cmd'] is not None
    assert opts['cmd'].nargs == 1
    assert opts['cmd'].required
Example #26
0
def test_docopt_type_from_defaults():
    import sys
    help_string = """\
    Some Tool

    Usage: tools [-i <input>] [-o <output>]

    Options:
        -i, --input <input>    The input
                               [Default: stdin]
        -o, --output <output>  The output
                               [Default: stdout]
    """
    opts = Options.from_docopt(help_string)

    assert opts['input'].option_type == TYPE_INPUT
    assert opts['output'].option_type == TYPE_OUTPUT
    assert opts['input'].raw() == sys.stdin
    assert opts['output'].raw() == sys.stdout
Example #27
0
def test_argparse_parser():
    from argparse import ArgumentParser
    p = ArgumentParser()
    p.add_argument("-t", "--test", action="store_true")
    p.add_argument("-i", "--input", help="The input")
    p.add_argument("-o",
                   "--output",
                   nargs="*",
                   help="The output",
                   required=True)
    opts = Options.from_argparse(p)
    assert len(opts) == 4  # teh two + help
    assert opts['input'] is not None
    assert opts['input'].nargs == 1
    assert opts['test'] is not None
    assert opts['test'].nargs == 0
    assert opts['output'] is not None
    assert opts['output'].nargs == "*"
    assert opts['output'].required
Example #28
0
def test_docopt_type_from_defaults():
    import sys
    help_string = """\
    Some Tool

    Usage: tools [-i <input>] [-o <output>]

    Options:
        -i, --input <input>    The input
                               [Default: stdin]
        -o, --output <output>  The output
                               [Default: stdout]
    """
    opts = Options.from_docopt(help_string)

    assert opts['input'].option_type == TYPE_INPUT
    assert opts['output'].option_type == TYPE_OUTPUT
    assert opts['input'].raw() == sys.stdin
    assert opts['output'].raw() == sys.stdout
Example #29
0
def test_user_specified_option_and_default_recovery():
    from argparse import ArgumentParser

    p = ArgumentParser()
    p.add_argument("-t", "--test", action="store_true")
    p.add_argument("--int", type=int, default=1)
    p.add_argument("-i", "--input", help="The input")
    p.add_argument("-o", "--output", nargs="*", help="The output", required=True)
    opts = Options.from_argparse(p)
    opts.parse([])
    assert not opts["test"].raw()
    assert opts["input"].raw() is None
    assert opts["output"].raw() is None
    assert not opts["output"].user_specified
    assert not opts["input"].user_specified
    assert not opts["test"].user_specified
    opts.parse(["-t", "-i", "myin", "-o", "myout"])
    assert opts["test"].raw()
    assert opts["input"].raw() == "myin"
    assert opts["output"].raw() == ["myout"]
    assert opts["output"].user_specified
    assert opts["input"].user_specified
    assert opts["test"].user_specified
Example #30
0
def test_docopt_parser_with_opts():
    help_string = """\
    Some Tool

    Usage: tools [-t] [-i <input>...] [-o <output>] <cmd>

    Inputs:
        -i, --input <input>    The input

    Outputs:
        -o, --output <output>  The output

    Options:
        -t, --test             Some option
        -h, --help             Show help

    """
    opts = Options.from_docopt(help_string)

    assert len(opts) == 4  # the two + help
    assert opts["input"] is not None
    assert opts["input"].nargs == "*"
    assert opts["input"].option_type == TYPE_INPUT
    assert not opts["input"].required
    assert opts["output"] is not None
    assert opts["output"].nargs == 1
    assert opts["output"].option_type == TYPE_OUTPUT
    assert not opts["output"].required
    assert opts["test"] is not None
    assert opts["test"].nargs == 0
    assert not opts["test"].required
    assert opts["test"].option_type == TYPE_OPTION
    assert opts["cmd"] is not None
    assert opts["cmd"].nargs == 1
    assert opts["cmd"].required
    assert opts["cmd"].option_type == TYPE_OPTION
Example #31
0
def test_docopt_parser_with_opts():
    help_string = """\
    Some Tool

    Usage: tools [-t] [-i <input>...] [-o <output>] <cmd>

    Inputs:
        -i, --input <input>    The input

    Outputs:
        -o, --output <output>  The output

    Options:
        -t, --test             Some option
        -h, --help             Show help

    """
    opts = Options.from_docopt(help_string)

    assert len(opts) == 4  # the two + help
    assert opts['input'] is not None
    assert opts['input'].nargs == "*"
    assert opts['input'].option_type == TYPE_INPUT
    assert not opts['input'].required
    assert opts['output'] is not None
    assert opts['output'].nargs == 1
    assert opts['output'].option_type == TYPE_OUTPUT
    assert not opts['output'].required
    assert opts['test'] is not None
    assert opts['test'].nargs == 0
    assert not opts['test'].required
    assert opts['test'].option_type == TYPE_OPTION
    assert opts['cmd'] is not None
    assert opts['cmd'].nargs == 1
    assert opts['cmd'].required
    assert opts['cmd'].option_type == TYPE_OPTION
Example #32
0
def test_add_output_list_option():
    ops = Options()
    o = ops.add_output("test", [1, 2, 3])
    assert o.hidden
    assert o.raw() == [1, 2, 3]
Example #33
0
def test_add_output_single_value_option_none_value():
    ops = Options()
    o = ops.add_output("test")
    assert o.hidden
    assert o.raw() is None
Example #34
0
def test_add_output_single_value_option():
    ops = Options()
    o = ops.add_output("test", "myfile.txt")
    assert o.hidden
    assert o.raw() == 'myfile.txt'
Example #35
0
def test_add_output_single_value_option():
    ops = Options()
    o = ops.add_output("test", "myfile.txt")
    assert o.hidden
    assert o.raw() == "myfile.txt"
Example #36
0
def test_add_output_single_value_option_none_value():
    ops = Options()
    o = ops.add_output("test")
    assert o.hidden
    assert o.raw() is None
Example #37
0
def test_add_output_list_option():
    ops = Options()
    o = ops.add_output("test", [1, 2, 3])
    assert o.hidden
    assert o.raw() == [1, 2, 3]