From f07620afc7dc0d0d605eb483ef36c5979b33cdfd Mon Sep 17 00:00:00 2001 From: Kelly Brazil Date: Fri, 26 Mar 2021 09:28:03 -0700 Subject: [PATCH] move version to jc.__init__.py add -h option for help instead of always showing on error use jc.utils.error_message for the following errors: missing/incorrect arguments, parser not found, missing piped data --- README.md | 1 + jc/__init__.py | 3 ++- jc/cli.py | 21 +++++++++++++++------ 3 files changed, 18 insertions(+), 7 deletions(-) diff --git a/README.md b/README.md index de06f543..f4a1f41d 100644 --- a/README.md +++ b/README.md @@ -191,6 +191,7 @@ The JSON output can be compact (default) or pretty formatted with the `-p` optio ### Options - `-a` about `jc`. Prints information about `jc` and the parsers (in JSON, of course!) - `-d` debug mode. Prints trace messages if parsing issues are encountered (use `-dd` for verbose debugging) +- `-h` `jc` help - `-m` monochrome JSON output - `-p` pretty format the JSON output - `-q` quiet mode. Suppresses parser warning messages diff --git a/jc/__init__.py b/jc/__init__.py index a853638e..14f59d44 100644 --- a/jc/__init__.py +++ b/jc/__init__.py @@ -44,7 +44,7 @@ CLI Example: Module Example: >>> import jc.parsers.ls - >>> + >>> >>> data='''-rwxr-xr-x 1 root wheel 23648 May 3 22:26 cat ... -rwxr-xr-x 1 root wheel 30016 May 3 22:26 chmod ... -rwxr-xr-x 1 root wheel 29024 May 3 22:26 cp @@ -69,3 +69,4 @@ Module Example: """ name = 'jc' +__version__ = '1.15.0' diff --git a/jc/cli.py b/jc/cli.py index a76b9fbe..7745c436 100644 --- a/jc/cli.py +++ b/jc/cli.py @@ -17,11 +17,12 @@ from pygments.style import Style from pygments.token import (Name, Number, String, Keyword) from pygments.lexers import JsonLexer from pygments.formatters import Terminal256Formatter +import jc import jc.appdirs as appdirs class info(): - version = '1.15.0' + version = jc.__version__ description = 'JSON CLI output utility' author = 'Kelly Brazil' author_email = 'kellyjonbrazil@gmail.com' @@ -284,12 +285,12 @@ def about_jc(): } -def helptext(message): +def helptext(): """Return the help text with the list of parsers""" parsers_string = parsers_text(indent=12, pad=17) helptext_string = f''' - jc: {message} + jc converts the output of many commands to JSON for easier parsing in scripts. Usage: COMMAND | jc PARSER [OPTIONS] @@ -302,6 +303,7 @@ def helptext(message): Options: -a about jc -d debug - show traceback (-dd for verbose traceback) + -h jc help -m monochrome output -p pretty print output -q quiet - suppress parser warnings @@ -400,11 +402,13 @@ def magic(): elif run_command is None: return else: - print(helptext(f'parser not found for "{run_command}"'), file=sys.stderr) + jc.utils.error_message(f'parser not found for "{run_command}"') sys.exit(1) def main(): + import jc.utils + # break on ctrl-c keyboard interrupt signal.signal(signal.SIGINT, ctrlc) @@ -429,10 +433,15 @@ def main(): debug = 'd' in options verbose_debug = True if options.count('d') > 1 else False mono = 'm' in options + help_me = 'h' in options pretty = 'p' in options quiet = 'q' in options raw = 'r' in options + if help_me: + print(helptext()) + sys.exit(0) + if verbose_debug: import jc.tracebackplus jc.tracebackplus.enable(context=11) @@ -442,7 +451,7 @@ def main(): sys.exit(0) if sys.stdin.isatty(): - print(helptext('missing piped data'), file=sys.stderr) + jc.utils.error_message('missing piped data') sys.exit(1) data = sys.stdin.read() @@ -471,7 +480,7 @@ def main(): sys.exit(1) if not found: - print(helptext('missing or incorrect arguments'), file=sys.stderr) + jc.utils.error_message('missing or incorrect arguments') sys.exit(1) print(json_out(result, pretty=pretty, env_colors=jc_colors, mono=mono, piped_out=piped_output()))