getopt_long/
lib.rs

1// -- lib.rs --
2
3//! This crate is so simple. Just see the following example:
4//! ```rust
5//! let help_long = "help".to_string();
6//! let config_long = "config".to_string();
7//! let list_long = "list".to_string();
8//! let shell_long = "shell".to_string();
9//! let run_long = "run".to_string();
10//! let rows_long = "rows".to_string();
11//! let row_id_long = "row-id".to_string();
12//! let dict_long = "dictionary".to_string();
13//! 
14//! let longopts = &[
15//!     Opt::new(
16//!         Some(help_long.clone()),
17//!         help_long.chars().next(),
18//!         HasArg::NoArgument,
19//!         "help and version information",
20//!     )
21//!     .map_err(|e| on_the_spot!(e))?,
22//!     Opt::new(
23//!         Some(config_long.clone()),
24//!         config_long.chars().next(),
25//!         HasArg::NoArgument,
26//!         "config file name with full path",
27//!     )
28//!     .map_err(|e| on_the_spot!(e))?,
29//!     Opt::new(
30//!         Some(list_long.clone()),
31//!         list_long.chars().next(),
32//!         HasArg::NoArgument,
33//!         "list all dictionaries",
34//!     )
35//!     .map_err(|e| on_the_spot!(e))?,
36//!     Opt::new(
37//!         Some(shell_long.clone()),
38//!         shell_long.chars().next(),
39//!         HasArg::NoArgument,
40//!         "run as a Lua shell",
41//!     )
42//!     .map_err(|e| on_the_spot!(e))?,
43//!     Opt::new(
44//!         Some(run_long.clone()),
45//!         run_long.chars().next(),
46//!         HasArg::RequiredArgument,
47//!         "run a Lua script file",
48//!     )
49//!     .map_err(|e| on_the_spot!(e))?,
50//!     Opt::new(
51//!         Some(rows_long.clone()),
52//!         None,
53//!         HasArg::NoArgument,
54//!         "query rows count in a dictionary",
55//!     )
56//!     .map_err(|e| on_the_spot!(e))?,
57//!     Opt::new(
58//!         Some(row_id_long.clone()),
59//!         None,
60//!         HasArg::RequiredArgument,
61//!         "query a row information in a dictionary",
62//!     )
63//!     .map_err(|e| on_the_spot!(e))?,
64//!     Opt::new(
65//!         Some(dict_long.clone()),
66//!         dict_long.chars().next(),
67//!         HasArg::RequiredArgument,
68//!         "dictionary name used to query",
69//!     )
70//!     .map_err(|e| on_the_spot!(e))?,
71//! ];
72//! let opts = getopt_long(longopts).map_err(|e| on_the_spot!(e))?;
73//! 
74//! let config = {
75//!     let mut short = config_long.clone();
76//!     short.split_off(1);
77//!     let config_name = if let Some(config) = opts.args.get(&short) {
78//!         config.clone()
79//!     } else if let Some(config) = opts.args.get(&config_long) {
80//!         config.clone()
81//!     } else {
82//!         "/Users/xt/.dict/config.lua".to_string()
83//!     };
84//!     Config::load(&config_name)?
85//! };
86//! 
87//! let action = loop {
88//!     let mut short = help_long.clone();
89//!     short.split_off(1);
90//!     if opts.args.get(&short).is_some() || opts.args.get(&help_long).is_some() {
91//!         usage(
92//!             "rdict",
93//!             "query a word information from some remote dictionaries.",
94//!             "0.1.0",
95//!             longopts,
96//!         );
97//!         break Action::Quit;
98//!     }
99//! 
100//!     short = list_long.clone();
101//!     short.split_off(1);
102//!     if opts.args.get(&short).is_some() || opts.args.get(&list_long).is_some() {
103//!         break Action::List;
104//!     }
105//! 
106//!     short = shell_long.clone();
107//!     short.split_off(1);
108//!     if opts.args.get(&short).is_some() || opts.args.get(&shell_long).is_some() {
109//!         break Action::Shell;
110//!     }
111//! 
112//!     short = run_long.clone();
113//!     short.split_off(1);
114//!     if let Some(script) = opts.args.get(&short) {
115//!         break Action::Run {script: script.clone()}
116//!     } else if let Some(script) = opts.args.get(&run_long) {
117//!         break Action::Run {script: script.clone()}
118//!     }
119//! 
120//!     short = dict_long.clone();
121//!     short.split_off(1);
122//!     let dict_name = if let Some(dict) = opts.args.get(&short) {
123//!         dict.clone()
124//!     } else if let Some(dict) = opts.args.get(&dict_long) {
125//!         dict.clone()
126//!     } else {
127//!         config.default_dictionary_name()?
128//!     };
129//! 
130//!     if opts.args.get(&rows_long).is_some() {
131//!         break Action::Rows { dict: dict_name };
132//!     }
133//! 
134//!     if let Some(id) = opts.args.get(&row_id_long) {
135//!         let id: u32 = id
136//!             .parse()
137//!             .map_err(|e: <u32 as std::str::FromStr>::Err| on_the_spot!(e))?;
138//!         break Action::RowId {
139//!             id,
140//!             dict: dict_name,
141//!         };
142//!     }
143//! 
144//!     break Action::Query {
145//!         words: opts.operands.iter().map(|v| v.clone()).collect(),
146//!         dict: dict_name,
147//!     };
148//! };
149//! 
150//! action.do_with_config(config).await
151//! ```
152//!
153//! In a shell, key: 
154//! ```sh
155//! ./hello -h
156//! ```
157//! 
158//! show as below:
159//! ```sh 
160//! Description:
161//!     query a word information from some remote dictionaries.
162//! Version:
163//!     0.1.0
164//! Usage:
165//!     rdict [options [args]] [operands]
166//! Options:
167//!     -h, --help                         help and version information
168//!     -c, --config                       config file name with full path
169//!     -l, --list                         list all dictionaries
170//!     -s, --shell                        run as a Lua shell.
171//!     -r, --run                =Arg      run a Lua script file.
172//!         --rows                         query rows count in a dictionary
173//!         --row-id             =Arg      query a row information in a dictionary
174//!     -d, --dictionary         =Arg      dictionary name used to query
175//! ```
176//! 
177//! Just so. It is very simple and useful.
178
179mod getopt_long;
180mod my_glibc;
181
182// --
183
184pub use getopt_long::{getopt_long, usage, HasArg, Opt, OptError, OptResult, Arguments};