uymas_cli 2.2.1

the fast and minimal dependency development tool for rust language
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
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
use crate::cmd::get_os_args;
use std::collections::HashMap;
use std::env;
use std::fmt::{Debug, Display};
use std::path::{Path, PathBuf};
use std::str::FromStr;

/// 命令行解析后的参数
#[derive(Debug, Default)]
pub struct Args {
    // 命令行
    pub command: String,
    // 二级命令
    pub sub_command: String,
    // 参数选项
    pub option: Vec<String>,
    // 请求参数
    pub data: HashMap<String, Vec<String>>,
    pub raw: Vec<String>,
    pub is_extern_subc: bool, // 外部子命令
}

impl Args {
    /// 获取命令行参数,支持数组读取
    /// ```
    /// use uymas_cli::args::Args;
    /// use uymas_cli::cmd::{Cmd, CmdRunOs, CmdRunStr};
    /// let arg = Args::from_vec(vec!["app", "--options", "A", "B", "C c", "D", "-xty", "1", "2", "3", "4"]);
    /// assert_eq!(arg.get_data("options"), Some(&vec!["A".to_string(), "B".to_string(), "C c".to_string(), "D".to_string()]));
    /// assert_eq!(arg.get_data("y"), Some(&vec!["1".to_string(), "2".to_string(), "3".to_string(), "4".to_string()]));
    ///
    /// ```
    /// **get_data** 下个版本调整 key 参数类型为 Vec<&str>
    pub fn get_data(&self, key: &str) -> Option<&Vec<String>> {
        if let Some(value) = self.get_value(vec![key]) {
            return Some(value);
        }
        None
    }

    /// 获取输入的值,支持多参数
    pub fn get_value(&self, keys: Vec<&str>) -> Option<&Vec<String>> {
        if self.data.is_empty() {
            return None;
        }

        // 获取参数
        for key in keys {
            if self.data.contains_key(key) {
                return self.data.get(key);
            }
        }
        None
    }

    // 获取字符串参数
    pub fn get_value_string(&self, keys: Vec<&str>) -> String {
        let mut value = String::new();
        if let Some(v) = self.get_value(keys) {
            value = String::from(v.join(" ").trim());
        }
        value
    }

    /// 获取 `Option<String>` 类型
    #[deprecated(
        since = "2.0.1",
        note = "use the `get_option_string` instead, v2.2.x will remove"
    )]
    pub fn get_value_string_option(&self, keys: Vec<&str>) -> Option<String> {
        self.get_option_string(keys)
    }

    /// 获取 i32 数据类型
    pub fn get_value_i32(&self, keys: Vec<&str>) -> i32 {
        if let Some(v) = self.get_option::<i32>(keys) {
            return v;
        }
        0
    }

    /// 获取 i128 数据类型
    pub fn get_value_i128(&self, keys: Vec<&str>) -> i128 {
        if let Some(v) = self.get_option::<i128>(keys) {
            return v;
        }
        0
    }

    /// 获取 i64 数据类型
    pub fn get_value_i64(&self, keys: Vec<&str>) -> i64 {
        if let Some(v) = self.get_option::<i64>(keys) {
            return v;
        }
        0
    }

    /// 获取 bool 类型数据
    pub fn get_value_bool(&self, keys: Vec<&str>) -> bool {
        if let Some(v) = self.get_option::<bool>(keys) {
            v
        } else {
            false
        }
    }

    /// 获取 bool 类型数据
    pub fn get_value_f64(&self, keys: Vec<&str>) -> f64 {
        if let Some(v) = self.get_option(keys) {
            v
        } else {
            0.0
        }
    }

    /// 查看是否存在 options
    pub fn contain_opts(&self, keys: Vec<&str>) -> bool {
        for key in keys {
            if self.option.contains(&String::from(key)) {
                return true;
            }
        }
        false
    }

    // 参数解析处理过程参数
    fn _args_update_data(
        data: &HashMap<String, Vec<String>>,
        last_opt: &str,
        last_value: &[String],
    ) -> (HashMap<String, Vec<String>>, Vec<String>) {
        let mut new_data = data.clone();
        let mut new_value = last_value.to_owned();

        if !last_opt.is_empty() && !last_value.is_empty() {
            if data.contains_key(last_opt) {
                let mut exits_value = data.get(last_opt).unwrap().to_vec();
                for lv in new_value {
                    exits_value.push(lv);
                }
                new_data.insert(last_opt.to_owned(), exits_value.to_vec());
            } else {
                new_data.insert(last_opt.to_owned(), new_value);
            }
            new_value = Vec::new();
        }
        (new_data, new_value)
    }

    /// 实例化函数,解析桉树为命令行。`Cmd.data` 解析参照 url.Values 解析规则,即默认为 `Vec<String>`
    /// ```
    /// // 解析 "rustc --version" 命令
    /// use uymas_cli::cmd::{Cmd, CmdRunOs, CmdRunStr};
    /// let cmd = Cmd::new();
    /// cmd.run(vec!["rustc", "--version"]);
    /// ```
    pub fn from_args(args: &Vec<String>) -> Self {
        // 字段信息
        let mut command = String::new(); // 命令
        let mut sub_command = String::new(); // 子命令
        let mut option = Vec::new();
        let mut data: HashMap<String, Vec<String>> = HashMap::new();

        // 辅助变量
        let mut count = 0;
        let mut last_opt = String::new(); // 最新的opt缓存
        let mut last_value: Vec<String> = Vec::new(); // 最新的value缓存

        for arg in args {
            let arg = &String::from(arg.trim());
            if arg.is_empty() {
                continue;
            }
            let mut long_opt: i32 = -1;
            let mut shot_opt: i32 = -1;
            let mut equal_opt: usize = 0; // 是否含等于
            if let Some(vi) = arg.find("--") {
                long_opt = vi as i32;
            } else if let Some(vi) = arg.find('-') {
                shot_opt = vi as i32;
            };
            if let Some(vi) = arg.find('=') {
                equal_opt = vi;
            }

            let is_not_opt = shot_opt != 0 && long_opt != 0;
            if count == 0 && is_not_opt {
                // 命令解析
                command = String::from(arg);
            } else if count == 1 && !command.is_empty() && is_not_opt {
                // 子命令
                sub_command = String::from(arg);
            } else if !is_not_opt {
                let (ndt, nv) = Args::_args_update_data(&data, &last_opt, &last_value);
                data = ndt;
                last_value = nv;

                // 选项处理
                let is_log_opt = long_opt == 0;
                let split_idx = if is_log_opt { 2 } else { 1 };
                let (_, opt) = arg.split_at(split_idx);

                if is_log_opt {
                    last_opt = String::from(opt);
                } else {
                    for by in opt.chars() {
                        last_opt = String::from(by);
                        if !last_opt.contains('=') {
                            option.push(String::from(&last_opt));
                        }
                    }
                }

                if let Some(vi) = last_opt.find('=') {
                    equal_opt = vi;
                }
                if equal_opt > 0 {
                    // 处理含等于符号的选项
                    let (eq_key, eq_value) = last_opt.split_at(equal_opt);
                    last_value.push(String::from(&eq_value[1..]));
                    last_opt = String::from(eq_key);
                }

                option.push(String::from(&last_opt));
            } else {
                last_value.push(String::from(arg));
            }
            count += 1;
        }

        // 临界值(最后出现的)
        let (ndt, _) = Args::_args_update_data(&data, &last_opt, &last_value);
        data = ndt;

        Args {
            command,
            sub_command,
            option,
            data,
            raw: args.to_vec(),
            is_extern_subc: false,
        }
    }

    /// 使用参数命令行列表
    pub fn from_vec(args: Vec<&str>) -> Self {
        let v_list: Vec<String> = args.iter().map(|v| String::from(*v)).collect();
        Self::from_args(&v_list)
    }

    /// 根据 os::args 获取参数
    pub fn from_os() -> Self {
        Args::from_args(&get_os_args())
    }

    /// 根据字符串转 Args 参数
    pub fn from_str(param: &str) -> Self {
        let queue = param.split(' ').collect();
        let args: Vec<String> = into_string_vec(queue);
        Args::from_args(&args)
    }

    /// 获取
    pub fn get_option_string(&self, keys: Vec<&str>) -> Option<String> {
        if let Some(v) = self.get_value(keys) {
            let value = String::from(v.join(" ").trim());
            return Some(value);
        }
        None
    }

    /// 获取 i32 参数
    #[deprecated(
        since = "2.1.0",
        note = "use the `get_option` instead, v2.2.x will remove"
    )]
    pub fn get_option_isize(&self, keys: Vec<&str>) -> Option<isize> {
        self.get_option(keys)
    }

    /// 获取 bool 类型
    #[deprecated(
        since = "2.1.0",
        note = "use the `get_option` instead, v2.2.x will remove"
    )]
    pub fn get_option_bool(&self, keys: Vec<&str>) -> Option<bool> {
        self.get_option(keys)
    }

    /// 获取 f64 类型
    #[deprecated(
        since = "2.1.0",
        note = "use the `get_option` instead, v2.2.x will remove"
    )]
    pub fn get_option_f64(&self, keys: Vec<&str>) -> Option<f64> {
        self.get_option(keys)
    }

    /// 获取任意解析参数
    pub fn get_option<T>(&self, keys: Vec<&str>) -> Option<T>
    where
        T: FromStr,
        <T as FromStr>::Err: Debug,
    {
        if let Some(raw) = self.get_option_string(keys) {
            let value_or_err = raw.parse::<T>();
            if let Ok(value) = value_or_err {
                return Some(value);
            }
        }
        None
    }

    /// 自定义构造 Args 对象并解析为字符串参数
    ///
    /// # Example
    ///
    /// 基本使用:
    /// ```
    /// use std::collections::HashMap;
    /// use uymas_cli::args::Args;
    /// let mut data = HashMap::from([
    ///     ("output".to_string(), vec!["./bin/name".to_string()]),
    ///     ("arch".to_string(), vec!["amd64".to_string()]),
    ///     ("x".to_string(), vec!["windows".to_string(), "unix".to_string()]),
    ///     ("stage".to_string(), vec!["b1".to_string(), "t2".to_string()]),
    /// ]);
    /// let app = Args{
    ///     command: "jc".to_string(),
    ///     sub_command: "build".to_string(),
    ///     option: vec!["utf8".to_string(), "release".to_string(), "mini".to_string(), "g".to_string()],
    ///     data,
    ///     raw: vec![],
    ///     is_extern_subc: false,
    /// };
    ///
    /// let ref_msg =
    ///     String::from("jc build --output ./bin/name --arch amd64 -x windows unix --stage b1 t2 --utf8 --release --mini -g");
    /// assert_eq!(app.parse_string(), ref_msg);
    /// ```
    pub fn parse_string(&self) -> String {
        let mut raw = vec![];
        // 命令行
        if !self.command.is_empty() {
            raw.push(self.command.clone());
            if !self.sub_command.is_empty() {
                raw.push(self.sub_command.clone());
            }
        }

        // 数据还原(复原)
        for (opt, ls) in &self.data {
            let name = if opt.len() == 1 {
                format!("-{}", opt)
            } else {
                format!("--{}", opt)
            };
            raw.push(format!("{} {}", name, ls.join(" ")));
        }

        // 选项复原
        for opt in self.option.clone().into_iter() {
            if self.data.contains_key(&opt) {
                continue;
            }
            let name = if opt.len() == 1 {
                format!("-{}", opt)
            } else {
                format!("--{}", opt)
            };
            raw.push(name);
        }

        raw.join(" ")
    }

    /// 获取原始输入指定选项(参数)的下一个值
    pub fn next(self, cur: String) -> Option<String> {
        let mut inx: i32 = -1;
        let mut cur_index = -1;
        for s in &self.raw {
            inx += 1;
            if *s == cur {
                cur_index = inx + 1;
                break;
            }
        }

        let size = self.raw.len();
        if cur_index > -1 && cur_index < size as i32 {
            return Some(self.raw[cur_index as usize].clone());
        }
        None
    }

    /// 获取原始输入指定选项(参数)的上一个值
    pub fn prev(self, cur: String) -> Option<String> {
        let mut inx: i32 = -1;
        let mut cur_index = -1;
        for s in &self.raw {
            inx += 1;
            if *s == cur {
                cur_index = inx - 1;
                break;
            }
        }

        let size = self.raw.len();
        if cur_index > -1 && cur_index < size as i32 {
            return Some(self.raw[cur_index as usize].clone());
        }
        None
    }
}

pub trait ArgsFromOs {
    fn new() -> Self;
}

impl ArgsFromOs for Args {
    fn new() -> Self {
        Args::from_os()
    }
}

pub trait ArgsNew<T> {
    fn new(param: T) -> Self;
}

/// Args 使用 `Vec<String>` 初始化
/// ```
/// use uymas_cli::args::{Args, ArgsNew};
/// let cmd = Args::new(&vec![String::from("git"), String::from("remote"), String::from("-v")]);
/// ```
impl ArgsNew<&Vec<String>> for Args {
    fn new(param: &Vec<String>) -> Self {
        Args::from_args(param)
    }
}

/// Args 使用 `Vec<String>` 初始化
/// ```
/// use uymas_cli::args::{Args, ArgsNew};
/// let cmd = Args::new(vec![String::from("git"), String::from("remote"), String::from("-v")]);
/// ```
/// Args 使用 `Vec<String>` 初始化
/// ```
/// use uymas_cli::args::{Args, ArgsNew};
/// let cmd = Args::new(vec!["git", "remote", "-v"]);
/// ```
impl<T> ArgsNew<Vec<T>> for Args
where
    T: Display,
{
    fn new(param: Vec<T>) -> Self {
        let args: Vec<String> = into_string_vec(param);
        Args::from_args(&args)
    }
}

fn into_string_vec<T>(args: Vec<T>) -> Vec<String>
where
    T: Display,
{
    let mut value: Vec<String> = Vec::new();
    for arg in args {
        value.push(format!("{}", arg));
    }
    value
}

impl Clone for Args {
    fn clone(&self) -> Self {
        Args {
            command: self.command.clone(),
            sub_command: self.sub_command.clone(),
            option: self.option.clone(),
            data: self.data.clone(),
            raw: self.raw.clone(),
            is_extern_subc: self.is_extern_subc,
        }
    }
}

/// 可配置的 option解析
pub trait ArgsOptionParseDefine {
    fn get_option_vec<T>(&self, keys: Vec<&str>, split: &str) -> Option<Vec<T>>
    where
        T: FromStr,
        <T as FromStr>::Err: Debug;
}

impl ArgsOptionParseDefine for Args {
    fn get_option_vec<T>(&self, keys: Vec<&str>, split: &str) -> Option<Vec<T>>
    where
        T: FromStr,
        <T as FromStr>::Err: Debug,
    {
        if let Some(v_str) = self.get_option_string(keys) {
            return parse_option_vec(v_str, split);
        }
        None
    }
}

pub trait ArgsOptionParse {
    fn get_option_vec<T>(&self, keys: Vec<&str>) -> Option<Vec<T>>
    where
        T: FromStr,
        <T as FromStr>::Err: Debug;
}

impl ArgsOptionParse for Args {
    fn get_option_vec<T>(&self, keys: Vec<&str>) -> Option<Vec<T>>
    where
        T: FromStr,
        <T as FromStr>::Err: Debug,
    {
        if let Some(v_str) = self.get_option_string(keys) {
            return parse_option_vec(v_str, ",");
        }
        None
    }
}

/// 获取二进制项目(当前应用)所在目录
/// ### example
/// ```
///  // ~/uymas.exe
///  use uymas_cli::args::project_path;
///  println!("{}", project_path("logs/2023/05/27.log"));
///  // 输入: `~/logs/2023/05/27.log`
/// ```
pub fn project_path<P: AsRef<Path>>(joins: P) -> String {
    let path = base_bin_path(joins);
    path.replace('\\', "/")
}

/// 获取当前正在执行二进制所在路径
pub fn get_exec_path() -> String {
    let mut exec_path = String::new();
    if let Some(arg) = env::args().next() {
        exec_path = arg;
    }

    exec_path.replace('\\', "/")
}

// 通过解析函数获取当前目录地址
fn get_exec_dir_by_args() -> String {
    let exec_path = get_exec_path();
    let pth = Path::new(&exec_path);

    if let Some(parent) = pth.parent() {
        parent.to_str().unwrap().to_string()
    } else {
        String::new()
    }
}

/// 获取当前正在执行二进制所在目录
pub fn get_exec_dir() -> String {
    let mut v_dir = get_exec_dir_by_args();
    if v_dir.is_empty() {
        v_dir = if let Ok(cur_dir) = env::current_dir() {
            if let Some(v) = cur_dir.to_str() {
                String::from(v)
            } else {
                String::new()
            }
        } else {
            String::new()
        }
    }

    v_dir
}

/// 获取当前正在执行二进制名称
pub fn get_exec_name() -> String {
    let exec_path = get_exec_path();
    let pth = Path::new(&exec_path);

    if let Some(vfl) = pth.file_name() {
        vfl.to_str().unwrap().to_string()
    } else {
        String::new()
    }
}

/// 过去可执行名称无后缀
pub fn get_exec_no_ext() -> String {
    let exec_path = get_exec_path();
    let pth = Path::new(&exec_path);

    if let Some(vfl) = pth.file_name() {
        let mut file_name = vfl.to_str().unwrap().to_string();
        if let Some(ext) = pth.extension() {
            file_name = file_name.replace(&ext.to_str().unwrap().to_string(), "");
        }
        file_name
    } else {
        String::new()
    }
}

/// 根据当前正在执行二进制所在吗,目录进行 split
pub fn root_path_split() -> (String, String) {
    let exec_path = get_exec_path();
    let pth = Path::new(&exec_path);

    let file_name = if let Some(vfl) = pth.file_name() {
        vfl.to_str().unwrap().to_string()
    } else {
        String::new()
    };

    if let Some(parent) = pth.parent() {
        (parent.to_str().unwrap().to_string(), file_name)
    } else {
        (String::new(), file_name)
    }
}

// 依赖二进制所在目录
fn base_bin_path<P: AsRef<Path>>(joins: P) -> String {
    let basedir = root_path_split();
    let pth = Path::new(basedir.0.as_str());
    let pth = pth.join(joins);
    pth.to_str().unwrap().to_string()
}

/// 参数解析
fn parse_option_vec<T>(value: String, split: &str) -> Option<Vec<T>>
where
    T: FromStr,
    <T as FromStr>::Err: Debug,
{
    let vec_list: Vec<&str> = value.split(split).collect();
    let mut parse = Vec::new();
    for vs in vec_list {
        let rslt_or_err = String::from(vs).parse::<T>();
        if let Ok(relt) = rslt_or_err {
            parse.push(relt);
        }
    }

    if parse.is_empty() {
        return None;
    }

    Some(parse)
}

/// 获取当前系统运行的目录
pub fn get_current_dir<P: AsRef<Path>>(v_path: P) -> Result<PathBuf, std::io::Error> {
    let cur_dir = env::current_dir()?;
    Ok(cur_dir.join(v_path))
}