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
use crate::action::Action;
use crate::args::{Args, ArgsNew};
use crate::cmd::subc::ExternSubc;
use std::collections::HashMap;
use std::env;

/// sub command 子命令,支持加载同目录下其他二进制文件
pub mod subc;

// 类型别名
// type ActionFn = fn(&Args);

pub struct ActionApp {
    pub command: String, // 命令
    pub alias: Vec<String>,
    pub action: Box<dyn Action>,
}

pub type CmdCallOption = Option<Box<dyn FnMut(&Args)>>;
pub type CmdCallMap = HashMap<String, Box<dyn FnMut(&Args)>>;

/// 命令行工具,使用实现命令行注册
///
///
/// # Examples
/// 默认使用 `start` 指定命令行程序,
/// ```
/// use uymas_cli::args::Args;
/// use uymas_cli::cmd::Cmd;
/// let mut cmd = Cmd::new();
///
/// // 默认调用
/// cmd.empty(|_: &Args|{
///     println!("Hello World, Cli.");
/// });
///
/// // 注册方法
/// cmd.register("version", |_: &Args|{
///     println!("v1.0.0");
/// });
///
/// // 方法指定
/// cmd.start();
/// ```
///
/// 使用 run 执行命令行,支持输入参数
/// ```
/// use uymas_cli::cmd::{Cmd, CmdRunOs};
/// let mut cmd = Cmd::new();
/// cmd.run();
/// ```
/// 支持 `Vec<String>` 参数
/// ```
/// use uymas_cli::cmd::{Cmd, CmdRunString};
/// let mut cmd = Cmd::new();
/// cmd.run(vec!["git".to_string(), "status".to_string()]);
/// ```
/// 支持 &str 参数
/// ```
/// use uymas_cli::cmd::{Cmd, CmdRunStr};
/// let mut cmd = Cmd::new();
/// cmd.run("git status");
/// ```
/// 支持 Vec<&str> 参数
/// ```
/// use uymas_cli::cmd::{Cmd, CmdRunArgs};
/// let mut cmd = Cmd::new();
/// cmd.run(vec!["git", "status"]);
/// ```
pub struct Cmd {
    raw_args: Vec<String>,            //原始输入参数
    calls: CmdCallMap,                // (注册的)函数集合
    actions: Vec<ActionApp>,          // 方法集合
    action_default: CmdCallOption,    // 默认执行方法
    action_no_handler: CmdCallOption, // 不存在的处理方法时
    args: Option<Args>,
    cmd_alias: Option<HashMap<String, Vec<String>>>, // 命令别名类别,函数定义时
}

/// 将 `env::args` 转化为 `Vec<String>`
pub fn get_os_args() -> Vec<String> {
    let mut args: Vec<String> = Vec::new();
    let mut idx = 0;
    for arg in env::args() {
        if idx < 1 {
            idx += 1;
            continue;
        }
        args.push(arg);
    }
    args
}

/// 来自Os::Args实例化的命令行
pub trait CmdRunOs {
    fn run(&mut self);
}

/// 来源自定义Args实例化的命令行
pub trait CmdRunArgs {
    /// 运行命令行应用
    fn run(&mut self, param: Vec<&str>);

    /// 多个命令注册命令
    fn register_multi<F>(&mut self, names: Vec<&str>, action: F) -> &mut Self
    where
        F: FnMut(&Args) + 'static;
}

/// 来源自定义 `str` 转化为 Args实例化的命令行
pub trait CmdRunStr {
    fn run(&mut self, param: &str);
}

/// 来源自定义 `String` 转化为 Args实例化的命令行
pub trait CmdRunString {
    /// 运行命令行应用
    fn run(&mut self, param: Vec<String>);

    /// 多个命令注册命令
    fn register_multi<F>(&mut self, names: Vec<String>, action: F) -> &mut Self
    where
        F: FnMut(&Args) + 'static;
}

// 为结构体添加方法
// @todo 考虑是否为其添加事件,调用前、调用后等事件。如 before, end 之类
impl Cmd {
    /// 命令行方法调用
    pub fn new() -> Cmd {
        Cmd {
            ..Default::default()
        }
    }

    // 获取操作系统命令
    fn get_os_args(&mut self) {
        self.raw_args = get_os_args();
    }

    // 解析参数
    fn parse_args(&mut self) {
        if self.raw_args.is_empty() {
            self.get_os_args()
        }
        if self.args.is_none() {
            let args = Args::new(&self.raw_args);
            self.args = Some(args);
        }
    }

    // 设置参数
    fn set_args(&mut self, args: Args) -> &mut Cmd {
        self.args = Some(args);
        self
    }

    /// 命令方法注册
    pub fn register<F>(&mut self, name: &str, action: F) -> &mut Cmd
    where
        F: FnMut(&Args) + 'static,
    {
        self.calls.insert(String::from(name), Box::new(action));
        self
    }

    /// 命令方法注册
    pub fn registers<F>(&mut self, names: Vec<&str>, action: F) -> &mut Self
    where
        F: FnMut(&Args) + 'static,
    {
        self.try_register_multi(names, action)
    }

    /// app 类型应用注册
    pub fn register_action(&mut self, app: ActionApp) -> &mut Cmd {
        self.actions.push(app);
        self
    }

    /// 命令行默认执行方法
    pub fn empty<F>(&mut self, action: F) -> &mut Cmd
    where
        F: FnMut(&Args) + 'static,
    {
        self.action_default = Some(Box::new(action));
        self
    }

    /// 处理应用不匹配时的调用
    pub fn un_found<F>(&mut self, action: F) -> &mut Cmd
    where
        F: FnMut(&Args) + 'static,
    {
        self.action_no_handler = Some(Box::new(action));
        self
    }

    /// 命令行默认调整
    pub fn start(&mut self) {
        self.parse_args();
        self.try_router();
    }

    // 尝试执行路由
    fn try_router(&mut self) {
        if self.args.is_none() {
            println!("因无Args参数,Cmd 运行失败");
            return;
        }
        let args = self.args.as_ref().unwrap();
        // 函数式定义参数
        for (v_key, v_fn) in &mut self.calls {
            if args.command == *v_key {
                v_fn(args);
                return;
            }
        }
        // 函数式定义参数(别名)
        if let Some(cmd_alias) = &self.cmd_alias {
            for (ca_key, ca_val) in cmd_alias {
                let mut is_match = args.command == *ca_key.as_str();
                if !is_match {
                    for cv in ca_val {
                        if args.command == *cv.as_str() {
                            is_match = true;
                            break;
                        }
                    }
                }
                if is_match {
                    if let Some(v_fn) = self.calls.get_mut(ca_key.as_str()) {
                        v_fn(args);
                        return;
                    }
                }
            }
        }

        // 类名定义尝试
        for action in &self.actions {
            if action.command == args.command {
                action.action.as_ref().run(args);
                return;
            } else {
                for alias in &action.alias {
                    if *alias == args.command {
                        action.action.as_ref().run(args);
                        return;
                    }
                }
            }
        }

        // 尝试读取子目录
        if !args.command.is_empty() {
            let es = ExternSubc::new(args.command.clone());
            if es.is_valid() {
                let raw = args.raw.clone();
                let count = raw.len();
                let raw = &raw[1..count].to_vec();
                let (is_ok, content) = es.run(raw);
                if is_ok {
                    println!("{}", content);
                    let mut args_chg = args.clone();
                    args_chg.is_extern_subc = true;
                    self.args = Some(args_chg);
                    return;
                }
            }
        }

        // 命令不存在时
        if let Some(action_no_hdl) = &mut self.action_no_handler {
            if !args.command.is_empty() {
                action_no_hdl(args);
                return;
            }
        }

        // 默认参数
        if let Some(action_default) = &mut self.action_default {
            action_default(args);
        } else {
            println!("请您至少为 Cmd 应用注册默认方法");
        }
    }

    // 多命令注册
    fn try_register_multi<F>(&mut self, names: Vec<&str>, action: F) -> &mut Self
    where
        F: FnMut(&Args) + 'static,
    {
        let v_len = names.len();
        if v_len < 1 {
            return self;
        }
        let key = names[0];
        self.register(key, action);
        let mut cmd_alias: HashMap<String, Vec<String>> = HashMap::new();
        if let Some(v_cmd_alias) = &self.cmd_alias {
            cmd_alias = v_cmd_alias.clone();
        }

        // Vec<&str> -> Vec<String>
        let mut values: Vec<String> = Vec::from_iter(names.iter().map(|x| String::from(*x)));
        if let Some(old_val) = cmd_alias.get(key) {
            for old in old_val {
                values.push(old.to_string())
            }
        }

        cmd_alias.insert(String::from(key), values);
        self.cmd_alias = Some(cmd_alias);
        self
    }
}

impl CmdRunOs for Cmd {
    /// 来源与系统的参数
    /// ```
    /// use uymas_cli::cmd::{Cmd, CmdRunArgs};
    /// let mut cmd = Cmd::new();
    /// cmd.run();
    /// ```
    fn run(&mut self) {
        let args = Args::from_os();
        self.set_args(args).start();
    }
}

impl CmdRunArgs for Cmd {
    /// 来源与`Vec<String>`的参数
    /// ```
    /// use uymas_cli::cmd::{Cmd, CmdRunArgs};
    /// let mut cmd = Cmd::new();
    /// cmd.run(vec!["git", "log", "--stat"]);
    /// ```
    fn run(&mut self, param: Vec<&str>) {
        let args = Args::new(param);
        self.set_args(args).start();
    }

    /// 多命令注册
    /// ```
    /// use uymas_cli::args::Args;
    /// use uymas_cli::cmd::{Cmd, CmdRunArgs};
    ///
    /// let mut cmd = Cmd::new();
    /// cmd.register_multi(vec!["version", "v"], |_: &Args| println!("v1.0.0"));
    /// cmd.run(vec!["git", "log", "--stat"]);
    /// ```
    fn register_multi<F>(&mut self, names: Vec<&str>, action: F) -> &mut Self
    where
        F: FnMut(&Args) + 'static,
    {
        self.try_register_multi(names, action)
    }
}

impl CmdRunStr for Cmd {
    /// 来源于字符串的参数
    /// ```
    /// use uymas_cli::cmd::{Cmd, CmdRunArgs};
    /// let mut cmd = Cmd::new();
    /// cmd.run(vec!["git", "log", "--stat"]);
    /// ```
    fn run(&mut self, param: &str) {
        let args = Args::from_str(param);
        self.set_args(args).start();
    }
}

impl CmdRunString for Cmd {
    fn run(&mut self, param: Vec<String>) {
        let args = Args::new(param);
        self.set_args(args).start();
    }

    fn register_multi<F>(&mut self, names: Vec<String>, action: F) -> &mut Self
    where
        F: FnMut(&Args) + 'static,
    {
        // Vec<&str> -> Vec<String>
        let values: Vec<&str> = Vec::from_iter(names.iter().map(|x| x.as_str()));
        self.try_register_multi(values, action)
    }
}

/// 命令行默认执行方法
impl Default for Cmd {
    fn default() -> Self {
        Cmd {
            raw_args: vec![],
            calls: Default::default(),
            actions: vec![],
            action_default: None,
            action_no_handler: None,
            args: None,
            cmd_alias: None,
        }
    }
}