def test_stacked_processing_unpacking(): cli_args = '-abc -xyz'.split() parsed = QuickParse(cli_args=cli_args) assert tuple(parsed.commands) == tuple() assert tuple(parsed.parameters) == tuple() assert parsed.options == { '-a': True, '-b': True, '-c': True, '-x': True, '-y': True, '-z': True } assert parsed.numeric == None assert parsed.plusnumeric == None assert parsed.to_execute == None assert len(parsed.errors) == 0 cli_args = '-abc -abc'.split() parsed = QuickParse(cli_args=cli_args) assert tuple(parsed.commands) == tuple() assert tuple(parsed.parameters) == tuple() assert parsed.options == { '-a': (True, True), '-b': (True, True), '-c': (True, True) } assert parsed.numeric == None assert parsed.plusnumeric == None assert parsed.to_execute == None assert len(parsed.errors) == 0
def test_commands_config_dupe_keys(): commands_config_dupe_keys = { ('help', 'h'): show_help, ('stage', 'st', 'h'): stage_show, } with pytest.raises(ValueError): cli_args = ['h'] parsed = QuickParse(commands_config_dupe_keys, cli_args=cli_args)
def test_basics(): with pytest.raises(ValueError): cli_args = ['', 'lorem', ()] parsed = QuickParse(cli_args=cli_args) cli_args = [] parsed = QuickParse(cli_args=cli_args) assert isinstance(parsed.args, (list, tuple)) assert tuple(parsed.args) == tuple(cli_args) assert parsed.commands_config is None assert parsed.options_config is None assert isinstance(parsed.commands, (list, tuple)) assert isinstance(parsed.parameters, (list, tuple)) assert isinstance(parsed.options, dict) assert parsed.numeric == None assert parsed.plusnumeric == None assert parsed.to_execute == None assert callable(parsed.execute)
def test_options_config_unpacking_with_value(): options_config = [ ('-x', '--extra', int), ] cli_args = ['-xyz', '8'] parsed = QuickParse(options_config=options_config, cli_args=cli_args) assert tuple(parsed.commands) == tuple() assert tuple(parsed.parameters) == (8, ) assert parsed.options == {'-x': 'yz', '--extra': 'yz'} assert parsed.numeric == None assert parsed.plusnumeric == None assert parsed.to_execute == None assert parsed.errors['--extra']['type'] == 0 cli_args = ['-yxz'] parsed = QuickParse(options_config=options_config, cli_args=cli_args) assert tuple(parsed.commands) == tuple() assert tuple(parsed.parameters) == tuple() assert parsed.options == { '--extra': True, '-x': True, '-y': True, '-z': True } assert parsed.numeric == None assert parsed.plusnumeric == None assert parsed.to_execute == None assert parsed.errors['--extra']['type'] == 1 cli_args = ['-yxz', '8'] parsed = QuickParse(options_config=options_config, cli_args=cli_args) assert tuple(parsed.commands) == tuple() assert tuple(parsed.parameters) == tuple() assert parsed.options == {'--extra': 8, '-x': 8, '-y': True, '-z': True} assert parsed.numeric == None assert parsed.plusnumeric == None assert parsed.to_execute == None assert len(parsed.errors) == 0 cli_args = ['-yzx', '8'] parsed = QuickParse(options_config=options_config, cli_args=cli_args) assert tuple(parsed.commands) == tuple() assert tuple(parsed.parameters) == tuple() assert parsed.options == {'-x': 8, '--extra': 8, '-y': True, '-z': True} assert parsed.numeric == None assert parsed.plusnumeric == None assert parsed.to_execute == None assert len(parsed.errors) == 0
def test_stacked_processing_numerics(): cli_args = '-11 -12'.split() parsed = QuickParse(cli_args=cli_args) assert tuple(parsed.commands) == tuple() assert tuple(parsed.parameters) == tuple() assert tuple(parsed.options) == tuple() assert parsed.numeric == (11, 12) assert parsed.to_execute == None assert len(parsed.errors) == 0 cli_args = '+11 +12 -13'.split() parsed = QuickParse(cli_args=cli_args) assert tuple(parsed.commands) == tuple() assert tuple(parsed.parameters) == tuple() assert tuple(parsed.options) == tuple() assert parsed.numeric == 13 assert parsed.plusnumeric == (11, 12) assert parsed.to_execute == None assert len(parsed.errors) == 0
def test_commands_config_ok(): cli_args = ['h'] parsed = QuickParse(commands_config_ok, cli_args=cli_args) assert len(parsed.commands ) == 2 and 'h' in parsed.commands and 'help' in parsed.commands assert len(parsed.parameters) == 0 assert len(parsed.non_commands) == 0 assert parsed.execute() == 'show_help' assert parsed.execute('lorem') == 'show_help'
def test_default_processing_unpacking(): cli_args = ['-abc'] parsed = QuickParse(cli_args=cli_args) assert tuple(parsed.commands) == tuple() assert tuple(parsed.parameters) == tuple() assert parsed.options == {'-a': True, '-b': True, '-c': True} assert parsed.numeric == None assert parsed.plusnumeric == None assert parsed.to_execute == None assert len(parsed.errors) == 0
def test_default_processing_numerics(): cli_args = '-12 +12'.split() parsed = QuickParse(cli_args=cli_args) assert tuple(parsed.commands) == tuple() assert tuple(parsed.parameters) == tuple() assert tuple(parsed.options) == tuple() assert parsed.numeric == 12 assert parsed.plusnumeric == 12 assert parsed.to_execute == None assert len(parsed.errors) == 0
def test_default_processing_single_parameter(): cli_args = ['x'] parsed = QuickParse(cli_args=cli_args) assert tuple(parsed.commands) == tuple() assert tuple(parsed.parameters) == ('x', ) assert tuple(parsed.options) == tuple() assert parsed.numeric == None assert parsed.plusnumeric == None assert parsed.to_execute == None assert len(parsed.errors) == 0
def test_default_processing_doubleminus_key_value(): cli_args = ['--abc=xyz'] parsed = QuickParse(cli_args=cli_args) assert tuple(parsed.commands) == tuple() assert tuple(parsed.parameters) == tuple() assert parsed.options == {'--abc': 'xyz'} assert parsed.numeric == None assert parsed.plusnumeric == None assert parsed.to_execute == None assert len(parsed.errors) == 0
def test_non_commands(): cli_args = 'user add user1 -3 --list'.split() parsed = QuickParse(commands_config_ok, cli_args=cli_args) assert tuple(parsed.commands) == ('user add', ) assert set(parsed.parameters) == {'user1'} assert set(parsed.non_commands) == {'user1', '-3', '--list'} assert parsed.options == {'--list': True} assert parsed.numeric == 3 assert parsed.plusnumeric == None assert parsed.to_execute == user_add assert len(parsed.errors) == 0
def test_options_config_equivalents(): options_config = [ ('-a', '--all'), ] cli_args = ['-a'] parsed = QuickParse(options_config=options_config, cli_args=cli_args) assert tuple(parsed.commands) == tuple() assert tuple(parsed.parameters) == tuple() assert parsed.options == {'-a': True, '--all': True} assert parsed.numeric == None assert parsed.plusnumeric == None assert parsed.to_execute == None assert len(parsed.errors) == 0
def test_options_config_no_unpacking(): options_config = [ ('-abc', ), # says not to unpack it ] cli_args = ['-abc'] parsed = QuickParse(options_config=options_config, cli_args=cli_args) assert tuple(parsed.commands) == tuple() assert tuple(parsed.parameters) == tuple() assert parsed.options == {'-abc': True} assert parsed.numeric == None assert parsed.plusnumeric == None assert parsed.to_execute == None assert len(parsed.errors) == 0
def test_basics_with_configs(): cli_args = [] parsed = QuickParse(commands_config_ok, options_config, cli_args=cli_args) assert isinstance(parsed.args, (list, tuple)) assert tuple(parsed.args) == tuple(cli_args) assert isinstance(parsed.commands_config, dict) assert parsed.commands_config == commands_config_ok assert isinstance(parsed.options_config, (list, tuple)) assert parsed.options_config == options_config assert isinstance(parsed.commands, (list, tuple)) assert isinstance(parsed.parameters, (list, tuple)) assert isinstance(parsed.options, dict) assert isinstance(parsed.non_commands, (list, tuple)) assert callable(parsed.execute)
def default(self, inp): try: QuickParse(commands_config, options_config=options_config, cli_args=shlex.split(inp)).execute() except AssertionError as ae: print(f'{ae}') except EOFError as ee: sys.stdout.write(f'{ee}') return True except KeyboardInterrupt: print(f'^C') except: traceback.print_exc() return False
def test_options_config_equivalent_key_values(): options_config = [ ('-n', '-name', '--name', str), ] cli_args = ['-nFoo'] parsed = QuickParse(options_config=options_config, cli_args=cli_args) assert tuple(parsed.commands) == tuple() assert tuple(parsed.parameters) == tuple() assert parsed.options == {'-n': 'Foo', '-name': 'Foo', '--name': 'Foo'} assert parsed.numeric == None assert parsed.plusnumeric == None assert parsed.to_execute == None assert len(parsed.errors) == 0 cli_args = ['-n', 'Foo'] parsed = QuickParse(options_config=options_config, cli_args=cli_args) assert tuple(parsed.commands) == tuple() assert tuple(parsed.parameters) == tuple() assert parsed.options == {'-n': 'Foo', '-name': 'Foo', '--name': 'Foo'} assert parsed.numeric == None assert parsed.plusnumeric == None assert parsed.to_execute == None assert len(parsed.errors) == 0 cli_args = ['-n=Foo'] parsed = QuickParse(options_config=options_config, cli_args=cli_args) assert tuple(parsed.commands) == tuple() assert tuple(parsed.parameters) == tuple() assert parsed.options == {'-n': 'Foo', '-name': 'Foo', '--name': 'Foo'} assert parsed.numeric == None assert parsed.plusnumeric == None assert parsed.to_execute == None assert len(parsed.errors) == 0 cli_args = ['-name', 'Foo'] parsed = QuickParse(options_config=options_config, cli_args=cli_args) assert tuple(parsed.commands) == tuple() assert tuple(parsed.parameters) == tuple() assert parsed.options == {'-n': 'Foo', '-name': 'Foo', '--name': 'Foo'} assert parsed.numeric == None assert parsed.plusnumeric == None assert parsed.to_execute == None assert len(parsed.errors) == 0 cli_args = ['-name=Foo'] parsed = QuickParse(options_config=options_config, cli_args=cli_args) assert tuple(parsed.commands) == tuple() assert tuple(parsed.parameters) == tuple() assert parsed.options == {'-n': 'Foo', '-name': 'Foo', '--name': 'Foo'} assert parsed.numeric == None assert parsed.plusnumeric == None assert parsed.to_execute == None assert len(parsed.errors) == 0 cli_args = ['-name'] parsed = QuickParse(options_config=options_config, cli_args=cli_args) assert tuple(parsed.commands) == tuple() assert tuple(parsed.parameters) == tuple() assert parsed.options == {'-n': True, '-name': True, '--name': True} assert parsed.numeric == None assert parsed.plusnumeric == None assert parsed.to_execute == None assert parsed.errors['--name']['type'] == 1 cli_args = ['-name='] parsed = QuickParse(options_config=options_config, cli_args=cli_args) assert tuple(parsed.commands) == tuple() assert tuple(parsed.parameters) == tuple() assert parsed.options == {'-n': '', '-name': '', '--name': ''} assert parsed.numeric == None assert parsed.plusnumeric == None assert parsed.to_execute == None assert len(parsed.errors) == 0
def do_stash_list(): print('Executing \'do_stash_list\'...') commands_config = { '': do_show_help, 'commit': do_commit, 'log': do_log, 'stash': { '': do_stash, 'list': do_stash_list, } } options_config = [ ('-m', '--message', str), ('-p', '--patch'), ] parsed = QuickParse(commands_config, options_config) print('Commands:\n%s' % (pformat(parsed.commands), )) print('Parameters:\n%s' % (pformat(parsed.parameters), )) print('Options:\n%s' % (pformat(parsed.options), )) print('\'-\' numeric argument:\n%s' % (pformat(parsed.numeric), )) print('\'+\' numeric argument:\n%s' % (pformat(parsed.plusnumeric), )) print('Functions to call:\n%s' % (pformat(parsed.to_execute), )) parsed.execute()
except: traceback.print_exc() return False if __name__ == '__main__': try: init() except AssertionError as ae: print(f'{ae}') exit() if len(sys.argv) == 1: if SCRIPT_PATH is not None: os.chdir(SCRIPT_PATH) cuerepl = CueREPL() cuerepl.prompt = f"{CLR.l_blue}cue·{CLR.reset}" nothing_worse_than_keyboardinterrupt = True while (nothing_worse_than_keyboardinterrupt): nothing_worse_than_keyboardinterrupt = False try: cuerepl.cmdloop() except KeyboardInterrupt: print(f'^C') nothing_worse_than_keyboardinterrupt = True else: try: QuickParse(commands_config, options_config=options_config).execute() except AssertionError as ae: print(f'{ae}')