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:
@ -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
|
||||
|
@ -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'
|
||||
|
21
jc/cli.py
21
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()))
|
||||
|
Reference in New Issue
Block a user