def test_option(): assert Option.parse('-h') == Option('-h', None) assert Option.parse('--help') == Option(None, '--help') assert Option.parse('-h --help') == Option('-h', '--help') assert Option.parse('-h, --help') == Option('-h', '--help') assert Option.parse('-h TOPIC') == Option('-h', None, 1) assert Option.parse('--help TOPIC') == Option(None, '--help', 1) assert Option.parse('-h TOPIC --help TOPIC') == Option('-h', '--help', 1) assert Option.parse('-h TOPIC, --help TOPIC') == Option('-h', '--help', 1) assert Option.parse('-h TOPIC, --help=TOPIC') == Option('-h', '--help', 1) assert Option.parse('-h Description...') == Option('-h', None) assert Option.parse('-h --help Description...') == Option('-h', '--help') assert Option.parse('-h TOPIC Description...') == Option('-h', None, 1) assert Option.parse(' -h') == Option('-h', None) assert Option.parse('-h TOPIC Descripton... [default: 2]') == \ Option('-h', None, 1, '2') assert Option.parse('-h TOPIC Descripton... [default: topic-1]') == \ Option('-h', None, 1, 'topic-1') assert Option.parse('--help=TOPIC ... [default: 3.14]') == \ Option(None, '--help', 1, '3.14') assert Option.parse('-h, --help=DIR ... [default: ./]') == \ Option('-h', '--help', 1, "./") assert Option.parse('-h TOPIC Descripton... [dEfAuLt: 2]') == \ Option('-h', None, 1, '2')
def test_option_name(): assert Option('-h', None).name == '-h' assert Option('-h', '--help').name == '--help' assert Option(None, '--help').name == '--help'
def test_option(): assert Option.parse("-h") == Option("-h", None) assert Option.parse("--help") == Option(None, "--help") assert Option.parse("-h --help") == Option("-h", "--help") assert Option.parse("-h, --help") == Option("-h", "--help") assert Option.parse("-h TOPIC") == Option("-h", None, 1) assert Option.parse("--help TOPIC") == Option(None, "--help", 1) assert Option.parse("-h TOPIC --help TOPIC") == Option("-h", "--help", 1) assert Option.parse("-h TOPIC, --help TOPIC") == Option("-h", "--help", 1) assert Option.parse("-h TOPIC, --help=TOPIC") == Option("-h", "--help", 1) assert Option.parse("-h Description...") == Option("-h", None) assert Option.parse("-h --help Description...") == Option("-h", "--help") assert Option.parse("-h TOPIC Description...") == Option("-h", None, 1) assert Option.parse(" -h") == Option("-h", None) assert Option.parse("-h TOPIC Descripton... [default: 2]") == Option("-h", None, 1, "2") assert Option.parse("-h TOPIC Descripton... [default: topic-1]") == Option("-h", None, 1, "topic-1") assert Option.parse("--help=TOPIC ... [default: 3.14]") == Option(None, "--help", 1, "3.14") assert Option.parse("-h, --help=DIR ... [default: ./]") == Option("-h", "--help", 1, "./") assert Option.parse("-h TOPIC Descripton... [dEfAuLt: 2]") == Option("-h", None, 1, "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 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_required_match(): assert Required(Option('-a')).match([Option('-a')]) == (True, [], []) assert Required(Option('-a')).match([]) == (False, [], []) assert Required(Option('-a')).match([Option('-x')]) == (False, [Option('-x')], []) assert Required(Option('-a'), Option('-b')).match([Option('-a')]) == (False, [Option('-a')], []) assert Optional(Option('-a'), Option('-b')).match( [Option('-b'), Option('-x'), Option('-a')]) == (True, [Option('-x')], [])
def test_optional_match(): assert Optional(Option('-a')).match([Option('-a')]) == (True, [], []) assert Optional(Option('-a')).match([]) == (True, [], []) assert Optional(Option('-a')).match([Option('-x')]) == (True, [Option('-x')], []) assert Optional(Option('-a'), Option('-b')).match([Option('-a')]) == (True, [], []) assert Optional(Option('-a'), Option('-b')).match([Option('-b')]) == (True, [], []) assert Optional(Option('-a'), Option('-b')).match([Option('-x')]) == (True, [Option('-x')], []) assert Optional(Argument('N')).match([Argument(None, 9) ]) == (True, [], [Argument('N', 9)])
def test_option_match(): assert Option('-a').match([Option('-a')]) == (True, [], []) assert Option('-a').match([Option('-x')]) == (False, [Option('-x')], []) assert Option('-a').match([Argument('N')]) == (False, [Argument('N')], []) assert Option('-a').match([Option('-x'), Option('-a'), Argument('N')]) == \ (True, [Option('-x'), Argument('N')], []) assert Option('-a', None, False).match([Option('-a', None, False)]) == \ (True, [], [])
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'))