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_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'))