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

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
This commit is contained in:
Kelly Brazil
2021-03-26 09:28:03 -07:00
parent c1b0d27752
commit f07620afc7
3 changed files with 18 additions and 7 deletions

View File

@ -191,6 +191,7 @@ The JSON output can be compact (default) or pretty formatted with the `-p` optio
### Options ### Options
- `-a` about `jc`. Prints information about `jc` and the parsers (in JSON, of course!) - `-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) - `-d` debug mode. Prints trace messages if parsing issues are encountered (use `-dd` for verbose debugging)
- `-h` `jc` help
- `-m` monochrome JSON output - `-m` monochrome JSON output
- `-p` pretty format the JSON output - `-p` pretty format the JSON output
- `-q` quiet mode. Suppresses parser warning messages - `-q` quiet mode. Suppresses parser warning messages

View File

@ -69,3 +69,4 @@ Module Example:
""" """
name = 'jc' name = 'jc'
__version__ = '1.15.0'

View File

@ -17,11 +17,12 @@ from pygments.style import Style
from pygments.token import (Name, Number, String, Keyword) from pygments.token import (Name, Number, String, Keyword)
from pygments.lexers import JsonLexer from pygments.lexers import JsonLexer
from pygments.formatters import Terminal256Formatter from pygments.formatters import Terminal256Formatter
import jc
import jc.appdirs as appdirs import jc.appdirs as appdirs
class info(): class info():
version = '1.15.0' version = jc.__version__
description = 'JSON CLI output utility' description = 'JSON CLI output utility'
author = 'Kelly Brazil' author = 'Kelly Brazil'
author_email = 'kellyjonbrazil@gmail.com' 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""" """Return the help text with the list of parsers"""
parsers_string = parsers_text(indent=12, pad=17) parsers_string = parsers_text(indent=12, pad=17)
helptext_string = f''' helptext_string = f'''
jc: {message} jc converts the output of many commands to JSON for easier parsing in scripts.
Usage: COMMAND | jc PARSER [OPTIONS] Usage: COMMAND | jc PARSER [OPTIONS]
@ -302,6 +303,7 @@ def helptext(message):
Options: Options:
-a about jc -a about jc
-d debug - show traceback (-dd for verbose traceback) -d debug - show traceback (-dd for verbose traceback)
-h jc help
-m monochrome output -m monochrome output
-p pretty print output -p pretty print output
-q quiet - suppress parser warnings -q quiet - suppress parser warnings
@ -400,11 +402,13 @@ def magic():
elif run_command is None: elif run_command is None:
return return
else: 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) sys.exit(1)
def main(): def main():
import jc.utils
# break on ctrl-c keyboard interrupt # break on ctrl-c keyboard interrupt
signal.signal(signal.SIGINT, ctrlc) signal.signal(signal.SIGINT, ctrlc)
@ -429,10 +433,15 @@ def main():
debug = 'd' in options debug = 'd' in options
verbose_debug = True if options.count('d') > 1 else False verbose_debug = True if options.count('d') > 1 else False
mono = 'm' in options mono = 'm' in options
help_me = 'h' in options
pretty = 'p' in options pretty = 'p' in options
quiet = 'q' in options quiet = 'q' in options
raw = 'r' in options raw = 'r' in options
if help_me:
print(helptext())
sys.exit(0)
if verbose_debug: if verbose_debug:
import jc.tracebackplus import jc.tracebackplus
jc.tracebackplus.enable(context=11) jc.tracebackplus.enable(context=11)
@ -442,7 +451,7 @@ def main():
sys.exit(0) sys.exit(0)
if sys.stdin.isatty(): if sys.stdin.isatty():
print(helptext('missing piped data'), file=sys.stderr) jc.utils.error_message('missing piped data')
sys.exit(1) sys.exit(1)
data = sys.stdin.read() data = sys.stdin.read()
@ -471,7 +480,7 @@ def main():
sys.exit(1) sys.exit(1)
if not found: if not found:
print(helptext('missing or incorrect arguments'), file=sys.stderr) jc.utils.error_message('missing or incorrect arguments')
sys.exit(1) sys.exit(1)
print(json_out(result, pretty=pretty, env_colors=jc_colors, mono=mono, piped_out=piped_output())) print(json_out(result, pretty=pretty, env_colors=jc_colors, mono=mono, piped_out=piped_output()))