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
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
// Copyright (C) 2024 Takayuki Sato. All Rights Reserved.
// This program is free software under MIT License.
// See the file LICENSE in this distribution for more details.

pub mod validators;

use crate::errors::InvalidOption;
use std::fmt;

/// Represents an option configuration for how to parse command line arguments.
///
/// And this is also used when creating the help text for command line arguments.
pub struct OptCfg {
    /// Is the key to store option value(s) in the option map in a `Cmd` instance.
    /// If this key is not specified or empty, the first element of the `names` field is used
    /// instead.
    pub store_key: String,

    /// Is the vector for specifying the option name and the aliases.
    /// The order of the `names` in this array are used in a help text.
    pub names: Vec<String>,

    /// Is the flag which allow the option to take option arguments.
    pub has_arg: bool,

    /// Is the flag which allow the option to take multiple option arguments.
    pub is_array: bool,

    /// Is the `Option` of the vector to specify default value(s) for when the comand option is not
    /// given in command line arguments.
    /// If this value is `None`, the default value(s) is not specified.
    pub defaults: Option<Vec<String>>,

    /// Is the string field to set the description of the option which is used in a help text.
    pub desc: String,

    /// Is the field to set a display string of the option argument(s) in a help text.
    /// An example of the display is like: `-o, --option <value>`.
    pub arg_in_help: String,

    /// Is the function pointer to validate the option argument(s).
    /// If the option argument is invalid, this funciton returns a
    /// `InvalidOption::OptionArgIsInvalid` instance.
    pub validator: fn(store_key: &str, name: &str, arg: &str) -> Result<(), InvalidOption>,
}

impl fmt::Debug for OptCfg {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> Result<(), fmt::Error> {
        f.debug_struct("OptCfg")
            .field("store_key", &self.store_key)
            .field("names", &self.names)
            .field("has_arg", &self.has_arg)
            .field("is_array", &self.is_array)
            .field("defaults", &self.defaults)
            .field("desc", &self.desc)
            .field("arg_in_help", &self.arg_in_help)
            .finish()
    }
}

impl OptCfg {
    /// Creates a `OptCfg` instance in a manner similar to named parameters.
    ///
    /// ```rust
    ///   use cliargs::OptCfg;
    ///   use cliargs::OptCfgParam::{names, has_arg, desc};
    ///
    ///   let cfg = OptCfg::with(&[
    ///       names(&["foo-bar", "f"]),
    ///       has_arg(true),
    ///       desc("this is foo-bar option."),
    ///   ]);
    /// ```
    pub fn with(params: &[OptCfgParam]) -> OptCfg {
        let empty_string = String::from("");
        let empty_vec = Vec::with_capacity(0);

        let mut init = OptCfgInit {
            store_key: &empty_string,
            names: &empty_vec,
            has_arg: false,
            is_array: false,
            defaults: None,
            desc: &empty_string,
            arg_in_help: &empty_string,
            validator: |_, _, _| Ok(()),
        };

        for param in params.iter() {
            init.edit(param);
        }

        OptCfg {
            store_key: init.store_key.to_string(),
            names: init.names.iter().map(|s| s.to_string()).collect(),
            has_arg: init.has_arg,
            is_array: init.is_array,
            defaults: if let Some(sl) = init.defaults {
                Some(sl.iter().map(|s| s.to_string()).collect())
            } else {
                None
            },
            desc: init.desc.to_string(),
            arg_in_help: init.arg_in_help.to_string(),
            validator: init.validator,
        }
    }
}

struct OptCfgInit<'a> {
    store_key: &'a str,
    names: &'a [&'a str],
    has_arg: bool,
    is_array: bool,
    defaults: Option<&'a [&'a str]>,
    desc: &'a str,
    arg_in_help: &'a str,
    validator: fn(store_key: &str, name: &str, arg: &str) -> Result<(), InvalidOption>,
}

impl<'a> OptCfgInit<'a> {
    fn edit(&mut self, param: &'a OptCfgParam) {
        match param {
            OptCfgParam::store_key(s) => self.store_key = s,
            OptCfgParam::names(v) => self.names = v,
            OptCfgParam::has_arg(b) => self.has_arg = *b,
            OptCfgParam::is_array(b) => self.is_array = *b,
            OptCfgParam::defaults(v) => self.defaults = Some(v),
            OptCfgParam::desc(s) => self.desc = s,
            OptCfgParam::arg_in_help(s) => self.arg_in_help = s,
            OptCfgParam::validator(f) => self.validator = *f,
        }
    }
}

/// Enables to create a `OptCfg` instance in a manner similar to named parameters.
#[allow(non_camel_case_types)]
pub enum OptCfgParam<'a> {
    /// Holds the value for `OptCfg#store_key`.
    store_key(&'a str),

    /// Holds the value for `OptCfg#names`.
    names(&'a [&'a str]),

    /// Holds the value for `OptCfg#has_arg`.
    has_arg(bool),

    /// Holds the value for `OptCfg#is_array`.
    is_array(bool),

    /// Holds the value for `OptCfg#defaults`.
    defaults(&'a [&'a str]),

    /// Holds the value for `OptCfg#desc`.
    desc(&'a str),

    /// Holds the value for `OptCfg#arg_in_help`.
    arg_in_help(&'a str),

    /// Holds the value for `OptCfg#validator`.
    validator(fn(&str, &str, &str) -> Result<(), InvalidOption>),
}

#[cfg(test)]
mod tests_of_opt_cfg {
    use super::*;

    mod tests_of_named_param {
        use super::*;

        #[test]
        fn test_of_store_key() {
            let cfg = OptCfg::with(&[OptCfgParam::store_key("fooBar")]);

            assert_eq!(cfg.store_key, "fooBar");
            assert_eq!(cfg.names, Vec::<String>::new());
            assert_eq!(cfg.has_arg, false);
            assert_eq!(cfg.is_array, false);
            assert_eq!(cfg.defaults, None);
            assert_eq!(cfg.desc, "");
            assert_eq!(cfg.arg_in_help, "");

            assert_eq!((cfg.validator)("a", "b", "c"), Ok(()));
        }

        #[test]
        fn test_of_names() {
            let cfg = OptCfg::with(&[OptCfgParam::names(&["foo-bar", "f"])]);

            assert_eq!(cfg.store_key, "");
            assert_eq!(cfg.names, vec!["foo-bar".to_string(), "f".to_string()]);
            assert_eq!(cfg.has_arg, false);
            assert_eq!(cfg.is_array, false);
            assert_eq!(cfg.defaults, None);
            assert_eq!(cfg.desc, "");
            assert_eq!(cfg.arg_in_help, "");

            assert_eq!((cfg.validator)("a", "b", "c"), Ok(()));
        }

        #[test]
        fn test_of_has_arg() {
            let cfg = OptCfg::with(&[OptCfgParam::has_arg(true)]);

            assert_eq!(cfg.store_key, "");
            assert_eq!(cfg.names, Vec::<String>::new());
            assert_eq!(cfg.has_arg, true);
            assert_eq!(cfg.is_array, false);
            assert_eq!(cfg.defaults, None);
            assert_eq!(cfg.desc, "");
            assert_eq!(cfg.arg_in_help, "");

            assert_eq!((cfg.validator)("a", "b", "c"), Ok(()));
        }

        #[test]
        fn test_of_is_array() {
            let cfg = OptCfg::with(&[OptCfgParam::is_array(true)]);

            assert_eq!(cfg.store_key, "");
            assert_eq!(cfg.names, Vec::<String>::new());
            assert_eq!(cfg.has_arg, false);
            assert_eq!(cfg.is_array, true);
            assert_eq!(cfg.defaults, None);
            assert_eq!(cfg.desc, "");
            assert_eq!(cfg.arg_in_help, "");

            assert_eq!((cfg.validator)("a", "b", "c"), Ok(()));
        }

        #[test]
        fn test_of_defaults() {
            let cfg = OptCfg::with(&[OptCfgParam::defaults(&["123", "456"])]);

            assert_eq!(cfg.store_key, "");
            assert_eq!(cfg.names, Vec::<String>::new());
            assert_eq!(cfg.has_arg, false);
            assert_eq!(cfg.is_array, false);
            assert_eq!(
                cfg.defaults,
                Some(vec!["123".to_string(), "456".to_string()])
            );
            assert_eq!(cfg.desc, "");
            assert_eq!(cfg.arg_in_help, "");

            assert_eq!((cfg.validator)("a", "b", "c"), Ok(()));
        }

        #[test]
        fn test_of_desc() {
            let cfg = OptCfg::with(&[OptCfgParam::desc("description")]);

            assert_eq!(cfg.store_key, "");
            assert_eq!(cfg.names, Vec::<String>::new());
            assert_eq!(cfg.has_arg, false);
            assert_eq!(cfg.is_array, false);
            assert_eq!(cfg.defaults, None);
            assert_eq!(cfg.desc, "description");
            assert_eq!(cfg.arg_in_help, "");

            assert_eq!((cfg.validator)("a", "b", "c"), Ok(()));
        }

        #[test]
        fn test_of_arg_in_help() {
            let cfg = OptCfg::with(&[OptCfgParam::arg_in_help("<num>")]);

            assert_eq!(cfg.store_key, "");
            assert_eq!(cfg.names, Vec::<String>::new());
            assert_eq!(cfg.has_arg, false);
            assert_eq!(cfg.is_array, false);
            assert_eq!(cfg.defaults, None);
            assert_eq!(cfg.desc, "");
            assert_eq!(cfg.arg_in_help, "<num>");

            assert_eq!((cfg.validator)("a", "b", "c"), Ok(()));
        }

        #[test]
        fn test_of_validator() {
            let cfg = OptCfg::with(&[OptCfgParam::validator(|key, name, arg| {
                Err(InvalidOption::OptionArgIsInvalid {
                    store_key: key.to_string(),
                    option: name.to_string(),
                    opt_arg: arg.to_string(),
                    details: "fail to parse integer".to_string(),
                })
            })]);

            assert_eq!(cfg.store_key, "");
            assert_eq!(cfg.names, Vec::<String>::new());
            assert_eq!(cfg.has_arg, false);
            assert_eq!(cfg.is_array, false);
            assert_eq!(cfg.defaults, None);
            assert_eq!(cfg.desc, "");
            assert_eq!(cfg.arg_in_help, "");

            match (cfg.validator)("a", "b", "c") {
                Ok(_) => assert!(false),
                Err(InvalidOption::OptionArgIsInvalid {
                    store_key,
                    option,
                    opt_arg,
                    details,
                }) => {
                    assert_eq!(store_key, "a");
                    assert_eq!(option, "b");
                    assert_eq!(opt_arg, "c");
                    assert_eq!(details, "fail to parse integer");
                }
                Err(_) => assert!(false),
            }
        }

        #[test]
        fn test_of_debug() {
            let cfg = OptCfg {
                store_key: "fooBar".to_string(),
                names: vec!["foo-bar".to_string(), "baz".to_string()],
                has_arg: true,
                is_array: true,
                defaults: Some(vec![123.to_string(), 456.to_string()]),
                desc: "option description".to_string(),
                arg_in_help: "<num>".to_string(),
                validator: |_, _, _| Ok(()),
            };

            assert_eq!(
                format!("{cfg:?}"),
                "OptCfg { store_key: \"fooBar\", names: [\"foo-bar\", \"baz\"], has_arg: true, \
                 is_array: true, defaults: Some([\"123\", \"456\"]), desc: \"option description\", \
                 arg_in_help: \"<num>\" }"
            );
        }
    }
}