libaki_json_pick/
lib.rs

1/*!
2The json pick out command.
3
4# Features
5
6- the json pick out command.
7- minimum support rustc 1.58.1 (db9d1b20b 2022-01-20)
8
9# Command help
10
11```text
12aki-json-pick --help
13```
14
15```text
16Usage:
17  aki-json-pick [options]
18
19The json pick out command.
20
21Options:
22      --color <when>        json colored output.
23  -s, --select <selector>   pick out json value by <selector>.
24  -p, --pretty              pretty output.
25  -r, --raw-output          raw string output without JSON double-quote.
26
27  -H, --help        display this help and exit
28  -V, --version     display version information and exit
29  -X <x-options>    x options. try -X help
30
31Option Parameters:
32  <when>        'always', 'never', or 'auto'
33  <selector>    json selector
34
35Examples:
36  pick out some.property value:
37    echo -e '{ "some": { "property": "yay!" } }' | aki-json-pick -s '"some"."property"'
38```
39
40# Examples
41
42## Example 1: root selection
43
44command line:
45```text
46cat fixtures/01.json | aki-json-pick -s '.'
47```
48
49result output:
50```text
51"This is a valid JSON text with one value"
52```
53
54## Example 2: child selection
55
56command line:
57```text
58cat fixtures/01.json | aki-json-pick -s '"some"."property"'
59```
60
61result output:
62```text
63"yay!"
64```
65
66## Example 3: index selection
67
68command line:
69```text
70cat fixtures/01.json | aki-json-pick -s '"primes".[0]'
71```
72
73result output:
74```text
757
76```
77
78command line:
79```text
80cat fixtures/01.json | aki-json-pick -s '"primes"[0]"'
81```
82
83result output:
84```text
857
86```
87
88command line:
89```text
90cat fixtures/01.json | aki-json-pick -s '"primes".[2,0]'
91```
92
93result output:
94```text
95[13,7]
96```
97
98# Reference
99
100This crate use [jql](https://crates.io/crates/jql). The `selector` is comatible.
101
102# Library example
103
104See [`fn execute()`] for this library examples.
105
106[`fn execute()`]: crate::execute
107*/
108#[macro_use]
109extern crate anyhow;
110
111mod conf;
112mod run;
113mod util;
114
115use flood_tide::HelpVersion;
116use runnel::RunnelIoe;
117use std::io::Write;
118
119const TRY_HELP_MSG: &str = "Try --help for help.";
120
121/// execute stats
122///
123/// params:
124///   - sioe: stream in/out/err
125///   - program: program name. etc. "json-pick"
126///   - args: parameter arguments.
127///
128/// return:
129///   - ok: ()
130///   - err: anyhow
131///
132/// # Examples
133///
134/// ## Example 1: root selection
135///
136/// ```rust
137/// use runnel::RunnelIoeBuilder;
138///
139/// let r = libaki_json_pick::execute(&RunnelIoeBuilder::new().build(),
140///     "json-pick", &["-s", "."]);
141/// ```
142///
143pub fn execute(sioe: &RunnelIoe, prog_name: &str, args: &[&str]) -> anyhow::Result<()> {
144    let conf = match conf::parse_cmdopts(prog_name, args) {
145        Ok(conf) => conf,
146        Err(errs) => {
147            for err in errs.iter().take(1) {
148                if err.is_help() || err.is_version() {
149                    let _r = sioe.pout().lock().write_fmt(format_args!("{err}\n"));
150                    return Ok(());
151                }
152            }
153            return Err(anyhow!("{}\n{}", errs, TRY_HELP_MSG));
154        }
155    };
156    run::run(sioe, &conf)
157}