mirror of
https://github.com/kellyjonbrazil/jc.git
synced 2025-06-21 00:19:42 +02:00
reimpliment magic() based on the dictionary approach suggested by philippeitis
This commit is contained in:
64
jc/cli.py
64
jc/cli.py
@ -188,63 +188,53 @@ def magic():
|
|||||||
if len(sys.argv) <= 1 or sys.argv[1].startswith('--'):
|
if len(sys.argv) <= 1 or sys.argv[1].startswith('--'):
|
||||||
return
|
return
|
||||||
|
|
||||||
|
magic_dict = {}
|
||||||
parser_info = about_jc()['parsers']
|
parser_info = about_jc()['parsers']
|
||||||
|
|
||||||
|
# Create a dictionary of magic_commands to their respective parsers.
|
||||||
|
for entry in parser_info:
|
||||||
|
# Update the dict with all of the magic commands for this parser, if they exist.
|
||||||
|
magic_dict.update({mc: entry['argument'] for mc in entry.get('magic_commands', [])})
|
||||||
|
|
||||||
# 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()
|
||||||
options = []
|
options = []
|
||||||
found_parser = None
|
|
||||||
|
|
||||||
# find the options
|
# find the options
|
||||||
if args_given[0].startswith('-'):
|
popped = 0
|
||||||
p = 0
|
for i, arg in list(enumerate(args_given)):
|
||||||
for i, arg in list(enumerate(args_given)):
|
# parser found - use standard syntax
|
||||||
# parser found - use standard syntax
|
if arg.startswith('--'):
|
||||||
if arg.startswith('--'):
|
return
|
||||||
return
|
|
||||||
# option found - populate option list
|
|
||||||
elif arg.startswith('-'):
|
|
||||||
options.append(args_given.pop(i - p)[1:])
|
|
||||||
p += 1
|
|
||||||
# command found if iterator didn't already stop - stop iterating
|
|
||||||
else:
|
|
||||||
break
|
|
||||||
|
|
||||||
|
# option found - populate option list
|
||||||
|
elif arg.startswith('-'):
|
||||||
|
options.append(args_given.pop(i - popped)[1:])
|
||||||
|
popped += 1
|
||||||
|
|
||||||
|
# command found if iterator didn't already stop - stop iterating
|
||||||
|
else:
|
||||||
|
break
|
||||||
|
|
||||||
|
# all options popped and no command found - for case like 'jc -a'
|
||||||
if len(args_given) == 0:
|
if len(args_given) == 0:
|
||||||
# This was a call to jc with just options and no commands.
|
|
||||||
return
|
return
|
||||||
|
|
||||||
# find the command and parser
|
# find the command and parser
|
||||||
command = ' '.join(args_given[0:2])
|
one_word_command = args_given[0]
|
||||||
for parser in parser_info:
|
two_word_command = ' '.join(args_given[0:2])
|
||||||
if 'magic_commands' not in parser:
|
|
||||||
continue
|
|
||||||
|
|
||||||
# first pass for two word commands: e.g. 'pip list'
|
# Try to get a parser for two_word_command, otherwise get one for one_word_command
|
||||||
if command in parser['magic_commands']:
|
found_parser = magic_dict.get(two_word_command, magic_dict.get(one_word_command))
|
||||||
try:
|
|
||||||
found_parser = parser['argument']
|
|
||||||
break
|
|
||||||
# No command found - go to next loop (for cases like 'jc -a')
|
|
||||||
except KeyError:
|
|
||||||
pass
|
|
||||||
|
|
||||||
# second pass for one word commands: e.g. 'ls'
|
|
||||||
elif args_given[0] in parser['magic_commands']:
|
|
||||||
try:
|
|
||||||
found_parser = parser['argument']
|
|
||||||
break
|
|
||||||
# No command found - use standard syntax (for cases like 'jc -a')
|
|
||||||
except KeyError:
|
|
||||||
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:
|
||||||
cmd_options = '-' + ''.join(options) if options else ''
|
cmd_options = '-' + ''.join(options) if options else ''
|
||||||
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)
|
||||||
exit()
|
exit()
|
||||||
|
|
||||||
else:
|
else:
|
||||||
helptext(f'parser not found for "{run_command}"')
|
helptext(f'parser not found for "{run_command}"')
|
||||||
sys.exit(1)
|
sys.exit(1)
|
||||||
|
Reference in New Issue
Block a user