Ejemplo n.º 1
0
def test_alias():
    @dataclass
    class Args:
        num: int = arg(aliases=["-n"])

    args = parse(Args, ["-n", "0"])
    assert args.num == 0
Ejemplo n.º 2
0
def test_count():
    @dataclass
    class Args:
        verbosity: int = field(
            default=0,
            metadata=dict(
                aliases=["-v"],
                help="Increase logging verbosity",
                action="count",
            ),
        )
    args = parse(Args, ["-vv"])
    assert args.verbosity == 2
Ejemplo n.º 3
0
def test_help():
    parser_help = "Program documentation"
    program = "My prog"
    parser = ParserTest(description=parser_help, prog=program)
    help_string = parser.format_help()
    assert parser_help in help_string
    assert program in help_string

    @dataclass
    class Args:
        flag: bool = arg(help="helpful message")

    args = parse(Args, [])
    assert not args.flag
    parser = make_parser(Args, parser)
    help_string = parser.format_help()
    assert "helpful message" in help_string
    assert parser_help in help_string
    assert program in help_string
Ejemplo n.º 4
0
def test_argsclass_on_decorator():
    """
    Does not work with attrs.
    """
    @argsclass
    @dataclass
    class TestDoubleDecorators:
        arg: str = arg(positional=True)

    args = parse(TestDoubleDecorators, ["arg"])
    assert args.arg == "arg"

    description = "desc"

    @argsclass(description=description)
    @dataclass
    class TestDoubleDecorators:
        arg: str = arg(positional=True)

    help_str = make_parser(TestDoubleDecorators).format_help()
    assert description in help_str
Ejemplo n.º 5
0
def test_decorator_with_args():
    @dataclass(repr=True)
    class Args:
        flag: bool = arg(help="helpful message")

    assert not parse(Args, []).flag
Ejemplo n.º 6
0
def test_decorator_no_args():
    @dataclass
    class Args:
        flag: bool = arg(help="helpful message")

    assert not parse(Args, []).flag
Ejemplo n.º 7
0
def parse_test(cls: Type[T], args: Sequence[str]) -> T:
    return parse(cls, args, parser=ParserTest())