def test_pattern_fix_repeating_arguments(): assert Option('-a').fix_repeating_arguments() == Option('-a') assert Argument('N', None).fix_repeating_arguments() == Argument('N', None) assert Required(Argument('N'), Argument('N')).fix_repeating_arguments() == \ Required(Argument('N', []), Argument('N', [])) assert Either(Argument('N'), OneOrMore(Argument('N'))).fix() == \ Either(Argument('N', []), OneOrMore(Argument('N', [])))
def test_pattern_fix_repeating_arguments(): assert Required(Option("-a")).fix_repeating_arguments() == Required( Option("-a")) assert Required(Argument("N", None)).fix_repeating_arguments() == Required( Argument("N", None)) assert Required(Argument("N"), Argument("N")).fix_repeating_arguments() == Required( Argument("N", []), Argument("N", [])) assert Either(Argument("N"), OneOrMore(Argument("N"))).fix() == Either( Argument("N", []), OneOrMore(Argument("N", [])))
def test_either_match(): assert Either(Option("-a"), Option("-b")).match([Option("-a")]) == (True, [], [Option("-a")]) assert Either(Option("-a"), Option("-b")).match([Option("-a"), Option("-b")]) == (True, [Option("-b")], [Option("-a")]) assert Either(Option("-a"), Option("-b")).match([Option("-x")]) == (False, [Option("-x")], []) assert Either(Option("-a"), Option("-b"), Option("-c")).match([Option("-x"), Option("-b")]) == (True, [Option("-x")], [Option("-b")]) assert Either(Argument("M"), Required(Argument("N"), Argument("M"))).match([Argument(None, 1), Argument(None, 2)]) == ( True, [], [Argument("N", 1), Argument("M", 2)], )
def test_either_match(): assert Either(Option('-a'), Option('-b')).match( [Option('-a')]) == (True, [], []) assert Either(Option('-a'), Option('-b')).match( [Option('-a'), Option('-b')]) == (True, [Option('-b')], []) assert Either(Option('-a'), Option('-b')).match( [Option('-x')]) == (False, [Option('-x')], []) assert Either(Option('-a'), Option('-b'), Option('-c')).match( [Option('-x'), Option('-b')]) == (True, [Option('-x')], []) assert Either(Argument('M'), Required(Argument('N'), Argument('M'))).match( [Argument(None, 1), Argument(None, 2)]) == \ (True, [], [Argument('N', 1), Argument('M', 2)])
def test_pattern_either(): assert transform(Option("-a")) == Either(Required(Option("-a"))) assert transform(Argument("A")) == Either(Required(Argument("A"))) assert transform(Required(Either(Option("-a"), Option("-b")), Option("-c"))) == Either( Required(Option("-a"), Option("-c")), Required(Option("-b"), Option("-c")) ) assert transform(NotRequired(Option("-a"), Either(Option("-b"), Option("-c")))) == Either( Required(Option("-b"), Option("-a")), Required(Option("-c"), Option("-a")) ) assert transform(Either(Option("-x"), Either(Option("-y"), Option("-z")))) == Either(Required(Option("-x")), Required(Option("-y")), Required(Option("-z"))) assert transform(OneOrMore(Argument("N"), Argument("M"))) == Either(Required(Argument("N"), Argument("M"), Argument("N"), Argument("M")))
def test_pattern_either(): assert Option('-a').either == Either(Required(Option('-a'))) assert Argument('A').either == Either(Required(Argument('A'))) assert Required(Either(Option('-a'), Option('-b')), Option('-c')).either ==\ Either(Required(Option('-a'), Option('-c')), Required(Option('-b'), Option('-c'))) assert Optional(Option('-a'), Either(Option('-b'), Option('-c'))).either ==\ Either(Required(Option('-b'), Option('-a')), Required(Option('-c'), Option('-a'))) assert Either(Option('-x'), Either(Option('-y'), Option('-z'))).either == \ Either(Required(Option('-x')), Required(Option('-y')), Required(Option('-z'))) assert OneOrMore(Argument('N'), Argument('M')).either == \ Either(Required(Argument('N'), Argument('M'), Argument('N'), Argument('M')))
def test_command_match(): assert Command('c').match([Argument(None, 'c')]) == ( True, [], [Command('c', True)]) assert Command('c').match([Option('-x')]) == (False, [Option('-x')], []) assert Command('c').match([Option('-x'), Option('-a'), Argument(None, 'c')]) == ( True, [Option('-x'), Option('-a')], [Command('c', True)]) assert Either(Command('add', False), Command('rm', False)).match( [Argument(None, 'rm')]) == (True, [], [Command('rm', True)])
def test_pattern_either(): assert transform(Option('-a')) == Either(Required(Option('-a'))) assert transform(Argument('A')) == Either(Required(Argument('A'))) assert transform(Required(Either(Option('-a'), Option('-b')), Option('-c'))) == \ Either(Required(Option('-a'), Option('-c')), Required(Option('-b'), Option('-c'))) assert transform(Optional(Option('-a'), Either(Option('-b'), Option('-c')))) == \ Either(Required(Option('-b'), Option('-a')), Required(Option('-c'), Option('-a'))) assert transform(Either(Option('-x'), Either(Option('-y'), Option('-z')))) == \ Either(Required(Option('-x')), Required(Option('-y')), Required(Option('-z'))) assert transform(OneOrMore(Argument('N'), Argument('M'))) == \ Either(Required(Argument('N'), Argument('M'), Argument('N'), Argument('M')))
def test_command_match(): assert Command("c").match([Argument(None, "c")]) == (True, [], [Command("c", True)]) assert Command("c").match([Option("-x")]) == (False, [Option("-x")], []) assert Command("c").match( [Option("-x"), Option("-a"), Argument(None, "c")]) == (True, [Option("-x"), Option("-a")], [Command("c", True)]) assert Either(Command("add", False), Command("rm", False)).match([Argument(None, "rm") ]) == (True, [], [Command("rm", True)])
def test_parse_pattern(): o = [Option("-h"), Option("-v", "--verbose"), Option("-f", "--file", 1)] assert parse_pattern("[ -h ]", options=o) == Required(NotRequired(Option("-h"))) assert parse_pattern("[ ARG ... ]", options=o) == Required(NotRequired(OneOrMore(Argument("ARG")))) assert parse_pattern("[ -h | -v ]", options=o) == Required(NotRequired(Either(Option("-h"), Option("-v", "--verbose")))) assert parse_pattern("( -h | -v [ --file <f> ] )", options=o) == Required( Required(Either(Option("-h"), Required(Option("-v", "--verbose"), NotRequired(Option("-f", "--file", 1, None))))) ) assert parse_pattern("(-h|-v[--file=<f>]N...)", options=o) == Required( Required(Either(Option("-h"), Required(Option("-v", "--verbose"), NotRequired(Option("-f", "--file", 1, None)), OneOrMore(Argument("N"))))) ) assert parse_pattern("(N [M | (K | L)] | O P)", options=[]) == Required( Required( Either( Required(Argument("N"), NotRequired(Either(Argument("M"), Required(Either(Argument("K"), Argument("L")))))), Required(Argument("O"), Argument("P")), ) ) ) assert parse_pattern("[ -h ] [N]", options=o) == Required(NotRequired(Option("-h")), NotRequired(Argument("N"))) assert parse_pattern("[options]", options=o) == Required(NotRequired(OptionsShortcut())) assert parse_pattern("[options] A", options=o) == Required(NotRequired(OptionsShortcut()), Argument("A")) assert parse_pattern("-v [options]", options=o) == Required(Option("-v", "--verbose"), NotRequired(OptionsShortcut())) assert parse_pattern("ADD", options=o) == Required(Argument("ADD")) assert parse_pattern("<add>", options=o) == Required(Argument("<add>")) assert parse_pattern("add", options=o) == Required(Command("add"))
def test_parse_pattern(): o = [Option('-h'), Option('-v', '--verbose'), Option('-f', '--file', 1)] assert parse_pattern('[ -h ]', options=o) == \ Required(Optional(Option('-h', None, 0, True))) assert parse_pattern('[ ARG ... ]', options=o) == \ Required(Optional(OneOrMore(Argument('ARG')))) assert parse_pattern('[ -h | -v ]', options=o) == \ Required(Optional(Either(Option('-h', None, 0, True), Option('-v', '--verbose', 0, True)))) assert parse_pattern('( -h | -v [ --file f.txt ] )', options=o) == \ Required(Required( Either(Option('-h', None, 0, True), Required(Option('-v', '--verbose', 0, True), Optional(Option('-f', '--file', 1, 'f.txt')))))) assert parse_pattern('(-h|-v[--file=f.txt]N...)', options=o) == \ Required(Required(Either(Option('-h', None, 0, True), Required(Option('-v', '--verbose', 0, True), Optional(Option('-f', '--file', 1, 'f.txt')), OneOrMore(Argument('N')))))) assert parse_pattern('(N [M | (K | L)] | O P)', options=[]) == \ Required(Required(Either( Required(Argument('N'), Optional(Either(Argument('M'), Required(Either(Argument('K'), Argument('L')))))), Required(Argument('O'), Argument('P'))))) assert parse_pattern('[ -h ] [N]', options=o) == \ Required(Optional(Option('-h', None, 0, True)), Optional(Argument('N'))) assert parse_pattern('[options]', options=o) == Required( Optional(AnyOptions())) assert parse_pattern('[options] A', options=o) == Required( Optional(AnyOptions()), Argument('A')) assert parse_pattern('-v [options]', options=o) == Required( Option('-v', '--verbose', 0, True), Optional(AnyOptions())) assert parse_pattern('ADD', options=o) == Required(Argument('ADD')) assert parse_pattern('<add>', options=o) == Required(Argument('<add>')) assert parse_pattern('add', options=o) == Required(Command('add'))