1
0
mirror of https://github.com/kellyjonbrazil/jc.git synced 2025-06-19 00:17:51 +02:00

Simplify main(), magic() methods.

This commit is contained in:
philippeitis
2020-03-04 10:33:42 -08:00
committed by GitHub
parent d96b3a65a9
commit 22ff2964e9

View File

@ -185,7 +185,9 @@ def json_out(data, pretty=False):
def magic(): def magic():
"""Parse with magic syntax: jc -p ls -al""" """Parse with magic syntax: jc -p ls -al"""
if len(sys.argv) > 1 and not sys.argv[1].startswith('--'): if len(sys.argv) <= 1 or sys.argv[1].startswith('--'):
return
parser_info = about_jc()['parsers'] parser_info = about_jc()['parsers']
# correctly parse escape characters and spaces with shlex # correctly parse escape characters and spaces with shlex
args_given = " ".join(map(shlex.quote, sys.argv[1:])).split() args_given = " ".join(map(shlex.quote, sys.argv[1:])).split()
@ -202,42 +204,39 @@ def magic():
# option found - populate option list # option found - populate option list
elif arg.startswith('-'): elif arg.startswith('-'):
options.append(args_given.pop(i - p)[1:]) options.append(args_given.pop(i - p)[1:])
p = p + 1 p += 1
# command found if iterator didn't already stop - stop iterating # command found if iterator didn't already stop - stop iterating
else: else:
break break
# find the command and parser # find the command and parser
command = ' '.join(args_given[0:2])
for parser in parser_info: for parser in parser_info:
if 'magic_commands' in parser: if 'magic_commands' not in parser:
continue
# first pass for two word commands: e.g. 'pip list' # first pass for two word commands: e.g. 'pip list'
for magic_command in parser['magic_commands']: if command in parser['magic_commands']:
try: try:
if ' '.join(args_given[0:2]) == magic_command:
found_parser = parser['argument'] found_parser = parser['argument']
break break
# No command found - go to next loop (for cases like 'jc -a') # No command found - go to next loop (for cases like 'jc -a')
except Exception: except KeyError:
break pass
# second pass for one word commands: e.g. 'ls' # second pass for one word commands: e.g. 'ls'
if not found_parser: elif args_given[0] in parser['magic_commands']
for magic_command in parser['magic_commands']:
try: try:
if args_given[0] == magic_command:
found_parser = parser['argument'] found_parser = parser['argument']
break break
# No command found - use standard syntax (for cases like 'jc -a') # No command found - use standard syntax (for cases like 'jc -a')
except Exception: except KeyError:
return return
# construct a new command line using the standard syntax: COMMAND | jc --PARSER -OPTIONS # construct a new command line using the standard syntax: COMMAND | jc --PARSER -OPTIONS
run_command = ' '.join(args_given) run_command = ' '.join(args_given)
if found_parser: if found_parser:
if options: cmd_options = '-' + ''.join(options) if options else ''
cmd_options = '-' + ''.join(options)
else:
cmd_options = ''
whole_command = ' '.join([run_command, '|', 'jc', found_parser, cmd_options]) whole_command = ' '.join([run_command, '|', 'jc', found_parser, cmd_options])
os.system(whole_command) os.system(whole_command)
@ -255,28 +254,16 @@ def main():
magic() magic()
options = [] options = []
debug = False
pretty = False
quiet = False
raw = False
# options # options
for opt in sys.argv: for opt in sys.argv:
if opt.startswith('-') and not opt.startswith('--'): if opt.startswith('-') and not opt.startswith('--'):
for flag in opt[1:]: options.extend(opt[1:])
options.append(flag)
if 'd' in options: debug = 'd' in options
debug = True pretty = 'p' in options
quiet = 'q' in options
if 'p' in options: raw = 'r' in raw
pretty = True
if 'q' in options:
quiet = True
if 'r' in options:
raw = True
if 'a' in options: if 'a' in options:
json_out(about_jc(), pretty=pretty) json_out(about_jc(), pretty=pretty)