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.80.0 (051478957 2024-07-21)
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;
117
118const TRY_HELP_MSG: &str = "Try --help for help.";
119
120/// execute stats
121///
122/// params:
123/// - sioe: stream in/out/err
124/// - program: program name. etc. "json-pick"
125/// - args: parameter arguments.
126///
127/// return:
128/// - ok: ()
129/// - err: anyhow
130///
131/// # Examples
132///
133/// ## Example 1: root selection
134///
135/// ```rust
136/// use runnel::RunnelIoeBuilder;
137///
138/// let r = libaki_json_pick::execute(&RunnelIoeBuilder::new().build(),
139/// "json-pick", ["-s", "."]);
140/// ```
141///
142pub fn execute<I, S>(sioe: &RunnelIoe, prog_name: &str, args: I) -> anyhow::Result<()>
143where
144 I: IntoIterator<Item = S>,
145 S: AsRef<std::ffi::OsStr>,
146{
147 let args: Vec<String> = args
148 .into_iter()
149 .map(|s| s.as_ref().to_string_lossy().into_owned())
150 .collect();
151 let args_str: Vec<&str> = args.iter().map(|s| s.as_str()).collect();
152 //
153 match conf::parse_cmdopts(prog_name, &args_str) {
154 Ok(conf) => run::run(sioe, &conf),
155 Err(errs) => {
156 if let Some(err) = errs.iter().find(|e| e.is_help() || e.is_version()) {
157 sioe.pg_out().write_line(err.to_string())?;
158 Ok(())
159 } else {
160 Err(anyhow!("{errs}\n{TRY_HELP_MSG}"))
161 }
162 }
163 }
164}