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

allow condensed options (-prdq is equivalent to -p -r -d -q)

This commit is contained in:
Kelly Brazil
2020-02-13 09:47:16 -05:00
parent a7158373cd
commit dd6680efb2
2 changed files with 29 additions and 14 deletions

View File

@ -2,6 +2,9 @@ jc changelog
20200211 v1.7.3 20200211 v1.7.3
- Add alternative 'magic' syntax: e.g. `jc ls -al` - Add alternative 'magic' syntax: e.g. `jc ls -al`
- Options can now be condensed (e.g. -prq is equivalant to -p -r -q )
- TODO: check if fstab works for both command and file
- TODO: check if crontab works for both command and file
20200208 v1.7.2 20200208 v1.7.2
- Include test fixtures in wheel - Include test fixtures in wheel

View File

@ -139,11 +139,11 @@ def helptext(message):
helptext_string = f''' helptext_string = f'''
jc: {message} jc: {message}
Usage: jc PARSER [OPTIONS] Usage: COMMAND | jc PARSER [OPTIONS]
or or
jc [OPTIONS] PARSER COMMAND | jc [OPTIONS] PARSER
or magic syntax: or magic syntax:
@ -179,6 +179,7 @@ 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 and not sys.argv[1].startswith('--'):
parser_info = about_jc()['parsers'] parser_info = about_jc()['parsers']
# how can i get the literal text of the command entered instead of the argument list?
args_given = sys.argv[1:] args_given = sys.argv[1:]
options = [] options = []
found_parser = None found_parser = None
@ -187,11 +188,14 @@ def magic():
if args_given[0].startswith('-'): if args_given[0].startswith('-'):
p = 0 p = 0
for i, arg in list(enumerate(args_given)): for i, arg in list(enumerate(args_given)):
# parser found
if arg.startswith('--'): if arg.startswith('--'):
return return
# option found
elif arg.startswith('-'): elif arg.startswith('-'):
options.append(args_given.pop(i - p)) options.append(args_given.pop(i - p)[1:])
p = p + 1 p = p + 1
# command found
else: else:
break break
@ -209,16 +213,18 @@ def magic():
except Exception: except Exception:
return return
if found_parser:
run_command = ' '.join(args_given) run_command = ' '.join(args_given)
cmd_options = ' '.join(options) if found_parser:
whole_command = [run_command, '|', 'jc', found_parser, cmd_options] if options:
cmd_options = '-' + ''.join(options)
else:
cmd_options = ''
whole_command = ' '.join([run_command, '|', 'jc', found_parser, cmd_options])
os.system(' '.join(whole_command)) os.system(whole_command)
exit() exit()
else: else:
args_given_pretty = ' '.join(args_given) helptext(f'parser not found for "{run_command}"')
helptext(f'parser not found for "{args_given_pretty}"')
sys.exit(1) sys.exit(1)
@ -228,25 +234,31 @@ def main():
# try magic syntax first: jc -p ls -al # try magic syntax first: jc -p ls -al
magic() magic()
options = []
debug = False debug = False
pretty = False pretty = False
quiet = False quiet = False
raw = False raw = False
# options # options
if '-d' in sys.argv: for opt in sys.argv:
if opt.startswith('-') and not opt.startswith('--'):
for flag in opt[1:]:
options.append(flag)
if 'd' in options:
debug = True debug = True
if '-p' in sys.argv: if 'p' in options:
pretty = True pretty = True
if '-q' in sys.argv: if 'q' in options:
quiet = True quiet = True
if '-r' in sys.argv: if 'r' in options:
raw = True raw = True
if '-a' in sys.argv: if 'a' in options:
json_out(about_jc(), pretty=pretty) json_out(about_jc(), pretty=pretty)
exit() exit()