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