forked from cry/cerulean
cli init
This commit is contained in:
parent
0651bd0118
commit
e3a2a17e6d
8 changed files with 92 additions and 0 deletions
8
cli/src/main.rs
Normal file
8
cli/src/main.rs
Normal file
|
|
@ -0,0 +1,8 @@
|
|||
use std::env;
|
||||
|
||||
mod rocli;
|
||||
|
||||
fn main() {
|
||||
let args: Vec<String> = env::args().collect();
|
||||
println!("Hello, world!");
|
||||
}
|
||||
9
cli/src/rocli/command.rs
Normal file
9
cli/src/rocli/command.rs
Normal file
|
|
@ -0,0 +1,9 @@
|
|||
pub struct Command {
|
||||
pub name: String,
|
||||
pub version: Option<String>,
|
||||
|
||||
pub action: Action,
|
||||
|
||||
pub subcommands: Vec<Commands>,
|
||||
pub flags: Vec<Flag>,
|
||||
}
|
||||
4
cli/src/rocli/mod.rs
Normal file
4
cli/src/rocli/mod.rs
Normal file
|
|
@ -0,0 +1,4 @@
|
|||
mod command;
|
||||
mod parse;
|
||||
|
||||
pub use parse::normalize_args;
|
||||
33
cli/src/rocli/parse.rs
Normal file
33
cli/src/rocli/parse.rs
Normal file
|
|
@ -0,0 +1,33 @@
|
|||
/// Parse the GNU convention CLI argument syntax.
|
||||
///
|
||||
/// # Examples
|
||||
/// ```rs
|
||||
/// assert!(parse(vec!["--flag=value"]) == vec!["--flag", "value"]);
|
||||
/// assert!(parse(vec!["--flag value"]) == vec!["--flag", "value"]);
|
||||
/// assert!(parse(vec!["-abe"]) == vec!["-a", "-b", "-e"]);
|
||||
/// assert!(parse(vec!["-abef=32"]) == vec!["-a", "-b", "-e", "-f", "32"]);
|
||||
/// ```
|
||||
///
|
||||
/// # Credit
|
||||
/// Based on [github:ksk001100/seahorse `src/utils.rs`](https://github.com/ksk001100/seahorse/blob/master/src/utils.rs)
|
||||
pub fn normalize_args(args: Vec<String>) -> Vec<String> {
|
||||
args.iter().fold(Vec::<String>::new(), |mut acc, el| {
|
||||
if !el.starts_with('-') {
|
||||
acc.push(el.to_owned());
|
||||
return acc;
|
||||
}
|
||||
|
||||
let mut split = el.splitn(2, '=').map(|s| s.to_owned()).collect();
|
||||
if el.starts_with("--") {
|
||||
acc.append(&mut split);
|
||||
} else {
|
||||
let flags = split[0].chars().skip(1).map(|c| format!("-{c}"));
|
||||
|
||||
acc.append(&mut flags.collect());
|
||||
if let Some(value) = split.get(1) {
|
||||
acc.push(value.to_owned());
|
||||
}
|
||||
}
|
||||
acc
|
||||
})
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue