1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
// -- lib.rs --

//! This crate is so simple. Just see the following example:
//! ```rust
//! let help_long = "help".to_string();
//! let config_long = "config".to_string();
//! let list_long = "list".to_string();
//! let shell_long = "shell".to_string();
//! let run_long = "run".to_string();
//! let rows_long = "rows".to_string();
//! let row_id_long = "row-id".to_string();
//! let dict_long = "dictionary".to_string();
//! 
//! let longopts = &[
//!     Opt::new(
//!         Some(help_long.clone()),
//!         help_long.chars().next(),
//!         HasArg::NoArgument,
//!         "help and version information",
//!     )
//!     .map_err(|e| on_the_spot!(e))?,
//!     Opt::new(
//!         Some(config_long.clone()),
//!         config_long.chars().next(),
//!         HasArg::NoArgument,
//!         "config file name with full path",
//!     )
//!     .map_err(|e| on_the_spot!(e))?,
//!     Opt::new(
//!         Some(list_long.clone()),
//!         list_long.chars().next(),
//!         HasArg::NoArgument,
//!         "list all dictionaries",
//!     )
//!     .map_err(|e| on_the_spot!(e))?,
//!     Opt::new(
//!         Some(shell_long.clone()),
//!         shell_long.chars().next(),
//!         HasArg::NoArgument,
//!         "run as a Lua shell",
//!     )
//!     .map_err(|e| on_the_spot!(e))?,
//!     Opt::new(
//!         Some(run_long.clone()),
//!         run_long.chars().next(),
//!         HasArg::RequiredArgument,
//!         "run a Lua script file",
//!     )
//!     .map_err(|e| on_the_spot!(e))?,
//!     Opt::new(
//!         Some(rows_long.clone()),
//!         None,
//!         HasArg::NoArgument,
//!         "query rows count in a dictionary",
//!     )
//!     .map_err(|e| on_the_spot!(e))?,
//!     Opt::new(
//!         Some(row_id_long.clone()),
//!         None,
//!         HasArg::RequiredArgument,
//!         "query a row information in a dictionary",
//!     )
//!     .map_err(|e| on_the_spot!(e))?,
//!     Opt::new(
//!         Some(dict_long.clone()),
//!         dict_long.chars().next(),
//!         HasArg::RequiredArgument,
//!         "dictionary name used to query",
//!     )
//!     .map_err(|e| on_the_spot!(e))?,
//! ];
//! let opts = getopt_long(longopts).map_err(|e| on_the_spot!(e))?;
//! 
//! let config = {
//!     let mut short = config_long.clone();
//!     short.split_off(1);
//!     let config_name = if let Some(config) = opts.args.get(&short) {
//!         config.clone()
//!     } else if let Some(config) = opts.args.get(&config_long) {
//!         config.clone()
//!     } else {
//!         "/Users/xt/.dict/config.lua".to_string()
//!     };
//!     Config::load(&config_name)?
//! };
//! 
//! let action = loop {
//!     let mut short = help_long.clone();
//!     short.split_off(1);
//!     if opts.args.get(&short).is_some() || opts.args.get(&help_long).is_some() {
//!         usage(
//!             "rdict",
//!             "query a word information from some remote dictionaries.",
//!             "0.1.0",
//!             longopts,
//!         );
//!         break Action::Quit;
//!     }
//! 
//!     short = list_long.clone();
//!     short.split_off(1);
//!     if opts.args.get(&short).is_some() || opts.args.get(&list_long).is_some() {
//!         break Action::List;
//!     }
//! 
//!     short = shell_long.clone();
//!     short.split_off(1);
//!     if opts.args.get(&short).is_some() || opts.args.get(&shell_long).is_some() {
//!         break Action::Shell;
//!     }
//! 
//!     short = run_long.clone();
//!     short.split_off(1);
//!     if let Some(script) = opts.args.get(&short) {
//!         break Action::Run {script: script.clone()}
//!     } else if let Some(script) = opts.args.get(&run_long) {
//!         break Action::Run {script: script.clone()}
//!     }
//! 
//!     short = dict_long.clone();
//!     short.split_off(1);
//!     let dict_name = if let Some(dict) = opts.args.get(&short) {
//!         dict.clone()
//!     } else if let Some(dict) = opts.args.get(&dict_long) {
//!         dict.clone()
//!     } else {
//!         config.default_dictionary_name()?
//!     };
//! 
//!     if opts.args.get(&rows_long).is_some() {
//!         break Action::Rows { dict: dict_name };
//!     }
//! 
//!     if let Some(id) = opts.args.get(&row_id_long) {
//!         let id: u32 = id
//!             .parse()
//!             .map_err(|e: <u32 as std::str::FromStr>::Err| on_the_spot!(e))?;
//!         break Action::RowId {
//!             id,
//!             dict: dict_name,
//!         };
//!     }
//! 
//!     break Action::Query {
//!         words: opts.operands.iter().map(|v| v.clone()).collect(),
//!         dict: dict_name,
//!     };
//! };
//! 
//! action.do_with_config(config).await
//! ```
//!
//! In a shell, key: 
//! ```sh
//! ./hello -h
//! ```
//! 
//! show as below:
//! ```sh 
//! Description:
//!     query a word information from some remote dictionaries.
//! Version:
//!     0.1.0
//! Usage:
//!     rdict [options [args]] [operands]
//! Options:
//!     -h, --help                         help and version information
//!     -c, --config                       config file name with full path
//!     -l, --list                         list all dictionaries
//!     -s, --shell                        run as a Lua shell.
//!     -r, --run                =Arg      run a Lua script file.
//!         --rows                         query rows count in a dictionary
//!         --row-id             =Arg      query a row information in a dictionary
//!     -d, --dictionary         =Arg      dictionary name used to query
//! ```
//! 
//! Just so. It is very simple and useful.

mod getopt_long;
mod my_glibc;

// --

pub use getopt_long::{getopt_long, usage, HasArg, Opt, OptError, OptResult, Arguments};