diff --git a/README.md b/README.md index 91352111..c6fa2030 100644 --- a/README.md +++ b/README.md @@ -292,7 +292,9 @@ option. ### Exit Codes Any fatal errors within `jc` will generate an exit code of `100`, otherwise the -exit code will be `0`. When using the "magic" syntax (e.g. `jc ifconfig eth0`), +exit code will be `0`. + +When using the "magic" syntax (e.g. `jc ifconfig eth0`), `jc` will store the exit code of the program being parsed and add it to the `jc` exit code. This way it is easier to determine if an error was from the parsed program or `jc`. @@ -306,6 +308,44 @@ Consider the following examples using `ifconfig`: | `0` | `100` | `100` | Error in `jc` | | `1` | `100` | `101` | Error in both `ifconfig` and `jc` | +When using the "magic" syntax you can also retrieve the exit code of the called +program by using the `--meta-out` or `-M` option. This will append a `_jc_meta` +object to the output that will include the magic command information, including +the exit code. + +Here is an example with `ping`: +```bash +$ jc --meta-out -p ping -c2 192.168.1.252 +{ + "destination_ip": "192.168.1.252", + "data_bytes": 56, + "pattern": null, + "destination": "192.168.1.252", + "packets_transmitted": 2, + "packets_received": 0, + "packet_loss_percent": 100.0, + "duplicates": 0, + "responses": [ + { + "type": "timeout", + "icmp_seq": 0, + "duplicate": false + } + ], + "_jc_meta": { + "parser": "ping", + "timestamp": 1661357115.27949, + "magic_command": [ + "ping", + "-c2", + "192.168.1.252" + ], + "magic_command_exit": 2 + } +} +$ echo $? +2 +``` ### Setting Custom Colors via Environment Variable You can specify custom colors via the `JC_COLORS` environment variable. The diff --git a/docs/lib.md b/docs/lib.md index 73c50f46..25c571bc 100644 --- a/docs/lib.md +++ b/docs/lib.md @@ -34,11 +34,22 @@ Parse the string data using the supplied parser module. This function provides a high-level API to simplify parser use. This function will call built-in parsers and custom plugin parsers. -Example: +Example (standard parsers): >>> import jc - >>> jc.parse('date', 'Tue Jan 18 10:23:07 PST 2022') - {'year': 2022, 'month': 'Jan', 'month_num': 1, 'day'...} + >>> date_obj = jc.parse('date', 'Tue Jan 18 10:23:07 PST 2022') + >>> print(f'The year is: {date_obj["year"]}') + The year is: 2022 + +Example (streaming parsers): + + >>> import jc + >>> ping_gen = jc.parse('ping_s', ping_output.splitlines()) + >>> for item in ping_gen: + >>> print(f'Response time: {item["time_ms"]} ms') + Response time: 102 ms + Response time: 109 ms + ... To get a list of available parser module names, use `parser_mod_list()`. diff --git a/docs/readme.md b/docs/readme.md index c87a0766..2c0d7e6d 100644 --- a/docs/readme.md +++ b/docs/readme.md @@ -11,12 +11,17 @@ and file-types to dictionaries and lists of dictionaries. ## Interactive Documentation +Using `jc` in your python programs: + >>> help('jc') >>> help('jc.lib') + >>> jc.get_help('parser_module_name') + +Developing `jc` parsers: + >>> help('jc.utils') >>> help('jc.streaming') >>> help('jc.parsers.universal') - >>> jc.get_help('parser_module_name') ## Online Documentation diff --git a/jc/__init__.py b/jc/__init__.py index bbf8ab97..fd297d59 100644 --- a/jc/__init__.py +++ b/jc/__init__.py @@ -7,12 +7,17 @@ and file-types to dictionaries and lists of dictionaries. ## Interactive Documentation +Using `jc` in your python programs: + >>> help('jc') >>> help('jc.lib') + >>> jc.get_help('parser_module_name') + +Developing `jc` parsers: + >>> help('jc.utils') >>> help('jc.streaming') >>> help('jc.parsers.universal') - >>> jc.get_help('parser_module_name') ## Online Documentation diff --git a/jc/lib.py b/jc/lib.py index 2675a43f..9356323d 100644 --- a/jc/lib.py +++ b/jc/lib.py @@ -197,11 +197,22 @@ def parse( This function provides a high-level API to simplify parser use. This function will call built-in parsers and custom plugin parsers. - Example: + Example (standard parsers): >>> import jc - >>> jc.parse('date', 'Tue Jan 18 10:23:07 PST 2022') - {'year': 2022, 'month': 'Jan', 'month_num': 1, 'day'...} + >>> date_obj = jc.parse('date', 'Tue Jan 18 10:23:07 PST 2022') + >>> print(f'The year is: {date_obj["year"]}') + The year is: 2022 + + Example (streaming parsers): + + >>> import jc + >>> ping_gen = jc.parse('ping_s', ping_output.splitlines()) + >>> for item in ping_gen: + >>> print(f'Response time: {item["time_ms"]} ms') + Response time: 102 ms + Response time: 109 ms + ... To get a list of available parser module names, use `parser_mod_list()`. diff --git a/man/jc.1 b/man/jc.1 index 2e9f1711..8febd814 100644 --- a/man/jc.1 +++ b/man/jc.1 @@ -1,4 +1,4 @@ -.TH jc 1 2022-08-21 1.21.0 "JSON Convert" +.TH jc 1 2022-08-24 1.21.0 "JSON Convert" .SH NAME \fBjc\fP \- JSON Convert JSONifies the output of many CLI tools and file-types .SH SYNOPSIS @@ -682,7 +682,9 @@ Generate Bash shell completion script Generate Zsh shell completion script .SH EXIT CODES -Any fatal errors within \fBjc\fP will generate an exit code of \fB100\fP, otherwise the exit code will be \fB0\fP. When using the "Magic" syntax (e.g. \fBjc ifconfig eth0\fP), \fBjc\fP will store the exit code of the program being parsed and add it to the \fBjc\fP exit code. This way it is easier to determine if an error was from the parsed program or \fBjc\fP. +Any fatal errors within \fBjc\fP will generate an exit code of \fB100\fP, otherwise the exit code will be \fB0\fP. + +When using the "magic" syntax (e.g. \fBjc ifconfig eth0\fP), \fBjc\fP will store the exit code of the program being parsed and add it to the \fBjc\fP exit code. This way it is easier to determine if an error was from the parsed program or \fBjc\fP. Consider the following examples using \fBifconfig\fP: @@ -696,6 +698,47 @@ ifconfig exit code = \fB0\fP, jc exit code = \fB100\fP, combined exit code = \fB ifconfig exit code = \fB1\fP, jc exit code = \fB100\fP, combined exit code = \fB101\fP (error in both ifconfig and jc) .RE +When using the "magic" syntax you can also retrieve the exit code of the called +program by using the \fB--meta-out\fP or \fB-M\fP option. This will append a \fB_jc_meta\fP +object to the output that will include the magic command information, including +the exit code. + +Here is an example with \fBping\fP: +.RS +.nf +$ jc --meta-out -p ping -c2 192.168.1.252 +{ + "destination_ip": "192.168.1.252", + "data_bytes": 56, + "pattern": null, + "destination": "192.168.1.252", + "packets_transmitted": 2, + "packets_received": 0, + "packet_loss_percent": 100.0, + "duplicates": 0, + "responses": [ + { + "type": "timeout", + "icmp_seq": 0, + "duplicate": false + } + ], + "_jc_meta": { + "parser": "ping", + "timestamp": 1661357115.27949, + "magic_command": [ + "ping", + "-c2", + "192.168.1.252" + ], + "magic_command_exit": 2 + } +} +$ echo $? +2 +.fi +.RE + .SH ENVIRONMENT \fBCustom Colors\fP diff --git a/templates/manpage_template b/templates/manpage_template index 7c6c7d33..c1bfacf6 100644 --- a/templates/manpage_template +++ b/templates/manpage_template @@ -87,7 +87,9 @@ Generate Bash shell completion script Generate Zsh shell completion script .SH EXIT CODES -Any fatal errors within \fBjc\fP will generate an exit code of \fB100\fP, otherwise the exit code will be \fB0\fP. When using the "Magic" syntax (e.g. \fBjc ifconfig eth0\fP), \fBjc\fP will store the exit code of the program being parsed and add it to the \fBjc\fP exit code. This way it is easier to determine if an error was from the parsed program or \fBjc\fP. +Any fatal errors within \fBjc\fP will generate an exit code of \fB100\fP, otherwise the exit code will be \fB0\fP. + +When using the "magic" syntax (e.g. \fBjc ifconfig eth0\fP), \fBjc\fP will store the exit code of the program being parsed and add it to the \fBjc\fP exit code. This way it is easier to determine if an error was from the parsed program or \fBjc\fP. Consider the following examples using \fBifconfig\fP: @@ -101,6 +103,47 @@ ifconfig exit code = \fB0\fP, jc exit code = \fB100\fP, combined exit code = \fB ifconfig exit code = \fB1\fP, jc exit code = \fB100\fP, combined exit code = \fB101\fP (error in both ifconfig and jc) .RE +When using the "magic" syntax you can also retrieve the exit code of the called +program by using the \fB--meta-out\fP or \fB-M\fP option. This will append a \fB_jc_meta\fP +object to the output that will include the magic command information, including +the exit code. + +Here is an example with \fBping\fP: +.RS +.nf +$ jc --meta-out -p ping -c2 192.168.1.252 +{ + "destination_ip": "192.168.1.252", + "data_bytes": 56, + "pattern": null, + "destination": "192.168.1.252", + "packets_transmitted": 2, + "packets_received": 0, + "packet_loss_percent": 100.0, + "duplicates": 0, + "responses": [ + { + "type": "timeout", + "icmp_seq": 0, + "duplicate": false + } + ], + "_jc_meta": { + "parser": "ping", + "timestamp": 1661357115.27949, + "magic_command": [ + "ping", + "-c2", + "192.168.1.252" + ], + "magic_command_exit": 2 + } +} +$ echo $? +2 +.fi +.RE + .SH ENVIRONMENT \fBCustom Colors\fP diff --git a/templates/readme_template b/templates/readme_template index c4e64d89..963c8176 100644 --- a/templates/readme_template +++ b/templates/readme_template @@ -173,7 +173,9 @@ option. ### Exit Codes Any fatal errors within `jc` will generate an exit code of `100`, otherwise the -exit code will be `0`. When using the "magic" syntax (e.g. `jc ifconfig eth0`), +exit code will be `0`. + +When using the "magic" syntax (e.g. `jc ifconfig eth0`), `jc` will store the exit code of the program being parsed and add it to the `jc` exit code. This way it is easier to determine if an error was from the parsed program or `jc`. @@ -187,6 +189,44 @@ Consider the following examples using `ifconfig`: | `0` | `100` | `100` | Error in `jc` | | `1` | `100` | `101` | Error in both `ifconfig` and `jc` | +When using the "magic" syntax you can also retrieve the exit code of the called +program by using the `--meta-out` or `-M` option. This will append a `_jc_meta` +object to the output that will include the magic command information, including +the exit code. + +Here is an example with `ping`: +```bash +$ jc --meta-out -p ping -c2 192.168.1.252 +{ + "destination_ip": "192.168.1.252", + "data_bytes": 56, + "pattern": null, + "destination": "192.168.1.252", + "packets_transmitted": 2, + "packets_received": 0, + "packet_loss_percent": 100.0, + "duplicates": 0, + "responses": [ + { + "type": "timeout", + "icmp_seq": 0, + "duplicate": false + } + ], + "_jc_meta": { + "parser": "ping", + "timestamp": 1661357115.27949, + "magic_command": [ + "ping", + "-c2", + "192.168.1.252" + ], + "magic_command_exit": 2 + } +} +$ echo $? +2 +``` ### Setting Custom Colors via Environment Variable You can specify custom colors via the `JC_COLORS` environment variable. The