aslip 0.1.11

a simple command line argument parser.
Documentation
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
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
use color_print::{cformat, cstr};
use owo_colors::OwoColorize;
use std::{process::exit, str::FromStr};

// -=-----------------------------------------------------------------------------------

/// ## 所有使用 `#[command]` 标记的函数的参数类型都需要实现 `FromArgStr` trait.
/// #### Example:
/// ```rust
/// use aslip::FromArgStr;
/// use aslip::from_arg_sttr::ParseError;
///
/// struct CustomType {}
/// impl FromArgStr for CustomType {
///     fn from_arg_str(s: &str) -> Result<Self, ParseError> {
///          Ok(Self{})
///     }
/// }
/// ```
///
pub trait FromArgStr: Sized {
    fn from_arg_str(s: &str) -> Result<Self, ParseError>;
}

#[derive(Debug, Clone, PartialEq)]
pub struct ParseError {
    /// 提示为什么出错。
    pub err_msg: String,

    /// 提示如何填写正确的参数。
    pub tips: String,
}
impl std::fmt::Display for ParseError {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        write!(f, "{}\n{}", self.err_msg, self.tips)
    }
}
impl ParseError {
    pub const fn empty() -> Self {
        Self {
            err_msg: String::new(),
            tips: String::new(),
        }
    }
}

impl FromArgStr for String {
    fn from_arg_str(s: &str) -> Result<Self, ParseError> {
        String::from_str(s).map_err(|e| ParseError {
            err_msg: cformat!(
                "将 <green,bold>{}</> 转换为 <cyan,bold>{}</> 时出错:{}",
                s,
                "String",
                std::any::type_name_of_val(&e)
            ),

            tips: "这种错误理论上可能出现,希望你能将如何触发这个错误的方法告诉这个程序的开发者。"
                .bold()
                .to_string(),
        })
    }
}
impl FromArgStr for bool {
    fn from_arg_str(s: &str) -> Result<Self, ParseError> {
        bool::from_str(s).map_err(|_| ParseError {
            err_msg: cformat!(
                "将 <green,bold>{}</> 转换为 <cyan,bold>{}</> 时出错.",
                s,
                "bool",
            ),

            tips: cstr!("<cyan!>bool</> 的值只有 <green,s>true</> 和 <green,s>false</>").into(),
        })
    }
}
impl FromArgStr for char {
    fn from_arg_str(s: &str) -> Result<Self, ParseError> {
        char::from_str(s).map_err(|e| ParseError {
            err_msg: cformat!(
                "将 <green,bold>{}</> 转换为 <cyan,bold>{}</> 时出错:{}",
                s,
                "char",
                std::any::type_name_of_val(&e)
            ),

            tips: format!(
                "示例: {min} {custom} {max}",
                min = "'a'".green().bold(),
                custom = "'c'".green().bold(),
                max = "'Z'".green().bold()
            ),
        })
    }
}
impl FromArgStr for i8 {
    fn from_arg_str(s: &str) -> Result<Self, ParseError> {
        i8::from_str(s).map_err(|e| ParseError {
            err_msg: cformat!(
                "将 <green,bold>{}</> 转换为 <cyan,bold>{}</> 时出错:{}",
                s,
                "i8",
                std::any::type_name_of_val(&e)
            ),

            tips: format!(
                "示例: {min} {custom} {max}",
                min = i8::MIN.green().bold(),
                custom = 123.green().bold().green().bold(),
                max = i8::MAX.green().bold()
            ),
        })
    }
}
impl FromArgStr for u8 {
    fn from_arg_str(s: &str) -> Result<Self, ParseError> {
        u8::from_str(s).map_err(|e| ParseError {
            err_msg: cformat!(
                "将 <green,bold>{}</> 转换为 <cyan,bold>{}</> 时出错:{}",
                s,
                "u8",
                std::any::type_name_of_val(&e)
            ),

            tips: format!(
                "示例: {min} {custom} {max}",
                min = u8::MIN.green().bold(),
                custom = 123.green().bold(),
                max = u8::MAX.green().bold()
            ),
        })
    }
}
impl FromArgStr for i16 {
    fn from_arg_str(s: &str) -> Result<Self, ParseError> {
        i16::from_str(s).map_err(|e| ParseError {
            err_msg: cformat!(
                "将 <green,bold>{}</> 转换为 <cyan,bold>{}</> 时出错:{}",
                s,
                "i16",
                std::any::type_name_of_val(&e)
            ),
            tips: format!(
                "示例: {min} {custom} {max}",
                min = i16::MIN.green().bold(),
                custom = 123.green().bold(),
                max = i16::MAX.green().bold(),
            ),
        })
    }
}
impl FromArgStr for u16 {
    fn from_arg_str(s: &str) -> Result<Self, ParseError> {
        u16::from_str(s).map_err(|e| ParseError {
            err_msg: cformat!(
                "将 <green,bold>{}</> 转换为 <cyan,bold>{}</> 时出错:{}",
                s,
                "u16",
                std::any::type_name_of_val(&e)
            ),

            tips: format!(
                "示例: {min} {custom} {max}",
                min = u16::MIN.green().bold(),
                custom = 123.green().bold(),
                max = u16::MAX.green().bold()
            ),
        })
    }
}
impl FromArgStr for i32 {
    fn from_arg_str(s: &str) -> Result<Self, ParseError> {
        i32::from_str(s).map_err(|e| ParseError {
            err_msg: cformat!(
                "将 <green,bold>{}</> 转换为 <cyan,bold>{}</> 时出错:{}",
                s,
                "i32",
                std::any::type_name_of_val(&e)
            ),

            tips: format!(
                "示例: {min} {custom} {max}",
                min = i32::MIN.green().bold(),
                custom = 123.green().bold(),
                max = i32::MAX
            ),
        })
    }
}
impl FromArgStr for u32 {
    fn from_arg_str(s: &str) -> Result<Self, ParseError> {
        u32::from_str(s).map_err(|_e| ParseError {
            err_msg: cformat!(
                "将 <green,bold>{}</> 转换为 <cyan,bold>{}</> 时出错.",
                s,
                "u32",
            ),

            tips: format!(
                "示例: {min} {custom} {max}",
                min = u32::MIN.green().bold(),
                custom = 123.green().bold(),
                max = u32::MAX.green().bold()
            ),
        })
    }
}
impl FromArgStr for f32 {
    fn from_arg_str(s: &str) -> Result<Self, ParseError> {
        f32::from_str(s).map_err(|_e| ParseError {
            err_msg: cformat!(
                "将 <green,bold>{}</> 转换为 <cyan,bold>{}</> 时出错.",
                s,
                "f32",
            ),

            tips: format!(
                "示例: {min} {custom} {max}",
                min = f32::MIN.green().bold(),
                custom = 123.green().bold(),
                max = f32::MAX.green().bold()
            ),
        })
    }
}
impl FromArgStr for i64 {
    fn from_arg_str(s: &str) -> Result<Self, ParseError> {
        i64::from_str(s).map_err(|_e| ParseError {
            err_msg: cformat!(
                "将 <green,bold>{}</> 转换为 <cyan,bold>{}</> 时出错.",
                s,
                "i64",
            ),

            tips: format!(
                "示例: {min} {custom} {max}",
                min = i64::MIN.green().bold(),
                custom = 123.green().bold(),
                max = i64::MAX.green().bold()
            ),
        })
    }
}
impl FromArgStr for u64 {
    fn from_arg_str(s: &str) -> Result<Self, ParseError> {
        u64::from_str(s).map_err(|_e| ParseError {
            err_msg: cformat!(
                "将 <green,bold>{}</> 转换为 <cyan,bold>{}</> 时出错.",
                s,
                "u64",
            ),

            tips: format!(
                "示例: {min} {custom} {max}",
                min = u64::MIN.green().bold(),
                custom = 123.green().bold(),
                max = u64::MAX.green().bold()
            ),
        })
    }
}
impl FromArgStr for f64 {
    fn from_arg_str(s: &str) -> Result<Self, ParseError> {
        f64::from_str(s).map_err(|_e| ParseError {
            err_msg: cformat!(
                "将 <green,bold>{}</> 转换为 <cyan,bold>{}</> 时出错.",
                s,
                "f64",
            ),

            tips: format!(
                "示例: {min} {custom} {max}",
                min = f64::MIN.green().bold(),
                custom = 123.green().bold(),
                max = f64::MAX.green().bold()
            ),
        })
    }
}
impl FromArgStr for i128 {
    fn from_arg_str(s: &str) -> Result<Self, ParseError> {
        i128::from_str(s).map_err(|_e| ParseError {
            err_msg: cformat!(
                "将 <green,bold>{}</> 转换为 <cyan,bold>{}</> 时出错.",
                s,
                "i128",
            ),

            tips: format!(
                "示例: {min} {custom} {max}",
                min = i128::MIN.green().bold(),
                custom = 123,
                max = i128::MAX.green().bold()
            ),
        })
    }
}
impl FromArgStr for u128 {
    fn from_arg_str(s: &str) -> Result<Self, ParseError> {
        u128::from_str(s).map_err(|_e| ParseError {
            err_msg: cformat!(
                "将 <green,bold>{}</> 转换为 <cyan,bold>{}</> 时出错.",
                s,
                "u128",
            ),

            tips: format!(
                "示例: {min} {custom} {max}",
                min = u128::MIN.green().bold(),
                custom = 123.green().bold(),
                max = u128::MAX.green().bold()
            ),
        })
    }
}
impl FromArgStr for isize {
    fn from_arg_str(s: &str) -> Result<Self, ParseError> {
        isize::from_str(s).map_err(|_e| ParseError {
            err_msg: cformat!(
                "将 <green,bold>{}</> 转换为 <cyan,bold>{}</> 时出错.",
                s,
                "isize",
            ),

            tips: format!(
                "示例: {min} {custom} {max}",
                min = isize::MIN.green().bold(),
                custom = 123.green().bold(),
                max = isize::MAX.green().bold()
            ),
        })
    }
}
impl FromArgStr for usize {
    fn from_arg_str(s: &str) -> Result<Self, ParseError> {
        usize::from_str(s).map_err(|_e| ParseError {
            err_msg: cformat!(
                "将 <green,bold>{}</> 转换为 <cyan,bold>{}</> 时出错.",
                s,
                "usize",
            ),

            tips: format!(
                "示例: {min} {custom} {max}",
                min = usize::MIN.green().bold(),
                custom = 123.green().bold(),
                max = usize::MAX.green().bold()
            ),
        })
    }
}
impl FromArgStr for std::ffi::CString {
    fn from_arg_str(s: &str) -> Result<Self, ParseError> {
        std::ffi::CString::from_str(s).map_err(|_e| ParseError {
            err_msg: cformat!(
                "将 <green,bold>{}</> 转换为 <cyan,bold>{}</> 时出错.",
                s,
                "std::ffi::CString",
            ),

            tips: "".to_string(),
        })
    }
}
impl FromArgStr for std::ffi::OsString {
    fn from_arg_str(s: &str) -> Result<Self, ParseError> {
        std::ffi::OsString::from_str(s).map_err(|_e| ParseError {
            err_msg: cformat!(
                "将 <green,bold>{}</> 转换为 <cyan,bold>{}</> 时出错.",
                s,
                "std::ffi::OsString",
            ),

            tips: "".to_string(),
        })
    }
}
impl FromArgStr for std::net::IpAddr {
    fn from_arg_str(s: &str) -> Result<Self, ParseError> {
        std::net::IpAddr::from_str(s).map_err(|_e| ParseError {
            err_msg: cformat!(
                "将 <green,bold>{}</> 转换为 <cyan,bold>{}</> 时出错.",
                s,
                "std::net::IpAddr",
            ),

            tips: "".to_string(),
        })
    }
}
impl FromArgStr for std::net::Ipv4Addr {
    fn from_arg_str(s: &str) -> Result<Self, ParseError> {
        std::net::Ipv4Addr::from_str(s).map_err(|_e| ParseError {
            err_msg: cformat!(
                "将 <green,bold>{}</> 转换为 <cyan,bold>{}</> 时出错.",
                s,
                "std::net::Ipv4Addr",
            ),

            tips: "".to_string(),
        })
    }
}
impl FromArgStr for std::net::Ipv6Addr {
    fn from_arg_str(s: &str) -> Result<Self, ParseError> {
        std::net::Ipv6Addr::from_str(s).map_err(|_e| ParseError {
            err_msg: cformat!(
                "将 <green,bold>{}</> 转换为 <cyan,bold>{}</> 时出错.",
                s,
                "std::net::Ipv6Addr",
            ),

            tips: "".to_string(),
        })
    }
}
impl FromArgStr for std::net::SocketAddr {
    fn from_arg_str(s: &str) -> Result<Self, ParseError> {
        std::net::SocketAddr::from_str(s).map_err(|_e| ParseError {
            err_msg: cformat!(
                "将 <green,bold>{}</> 转换为 <cyan,bold>{}</> 时出错.",
                s,
                "std::net::SocketAddr",
            ),

            tips: "".to_string(),
        })
    }
}
impl FromArgStr for std::net::SocketAddrV4 {
    fn from_arg_str(s: &str) -> Result<Self, ParseError> {
        std::net::SocketAddrV4::from_str(s).map_err(|_e| ParseError {
            err_msg: cformat!(
                "将 <green,bold>{}</> 转换为 <cyan,bold>{}</> 时出错.",
                s,
                "std::net::SocketAddrV4",
            ),

            tips: "".to_string(),
        })
    }
}
impl FromArgStr for std::net::SocketAddrV6 {
    fn from_arg_str(s: &str) -> Result<Self, ParseError> {
        std::net::SocketAddrV6::from_str(s).map_err(|_e| ParseError {
            err_msg: cformat!(
                "将 <green,bold>{}</> 转换为 <cyan,bold>{}</> 时出错.",
                s,
                "std::net::SocketAddrV6",
            ),

            tips: "".to_string(),
        })
    }
}
impl FromArgStr for std::path::PathBuf {
    fn from_arg_str(s: &str) -> Result<Self, ParseError> {
        std::path::PathBuf::from_str(s).map_err(|_e| ParseError {
            err_msg: cformat!(
                "将 <green,bold>{}</> 转换为 <cyan,bold>{}</> 时出错.",
                s,
                "std::path::PathBuf",
            ),

            tips: "".to_string(),
        })
    }
}

/// 用来检查某个类型是否实现了 `FromArgStr` trait.
/// 主要是给 aslip_macro::command 用的。
/// # Example:
/// ```rust,ignore
/// use aslip::from_arg_sttr::from_arg_str_trait_bound_check;
/// const _: () = { from_arg_str_trait_bound_check::<u8>() }; // 生成一个空的 const 用来触发编译时检查
///
/// struct CustomType {}
/// const _: () = { from_arg_str_trait_bound_check::<CustomType>() };
/// // the trait bound `CustomType: FromArgStr` is not satisfied the following other types implement trait `FromArgStr`
/// ```
///
pub const fn from_arg_str_trait_bound_check<T: FromArgStr>() {}

pub fn single_type_converter<T: FromArgStr>(
    app: &crate::App,
    arg_name: &str,
    arg_index: usize,
) -> T {
    let arg: &String = {
        let re = app._user_inputed_cmd_args.get(arg_index);

        match re {
            Some(v) => v,
            None => {
                eprintln!("{}", format!("需要参数:<{arg_name}>"));

                if let Some(cmd) = &app._user_inputed_cmd_name {
                    app.print_cmd_quick_help_for(cmd);
                }

                exit(1)
            }
        }
    };

    use owo_colors::OwoColorize;

    let asdf = <T as FromArgStr>::from_arg_str(arg);

    match asdf {
        Ok(val) => return val,
        Err(e) => {
            eprintln!("{err_marker}: {e}", err_marker = "error: ".red().bold(),);

            std::process::exit(1);
        }
    };
}

pub fn vec_type_converter<T: FromArgStr>(args: &[String]) -> Vec<T> {
    use owo_colors::OwoColorize;

    let mut re: Vec<T> = vec![];
    for x in args {
        let sdaf = T::from_arg_str(x);
        match sdaf {
            Ok(val) => re.push(val),
            Err(e) => {
                eprintln!("{err_marker}: {e}", err_marker = "error: ".red().bold(),);

                std::process::exit(1);
            }
        }
    }

    return re;
}