diff --git a/.gitignore b/.gitignore index 6936990..7264fab 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,4 @@ -/target +**/target **/*.rs.bk Cargo.lock + diff --git a/Cargo.toml b/Cargo.toml index 8424c4b..4664857 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "seahorse" -version = "0.1.1" +version = "0.2.0" authors = ["KeisukeToyota "] edition = "2018" keywords = ["cli"] diff --git a/README.md b/README.md index 00fbb73..075989a 100644 --- a/README.md +++ b/README.md @@ -6,7 +6,7 @@ A minimal CLI framework written in Rust ```toml [dependencies] -seahorse = "0.1.1" +seahorse = "0.2.0" ``` ## Example @@ -17,27 +17,26 @@ use seahorse::{App, Command, color}; fn main() { let args: Vec = env::args().collect(); - - let command = Command { - name: "hello".to_string(), - usage: "cli_tool hello user".to_string(), - action: |v: Vec| println!("Hello, {:?}", v) - }; - - let mut app = App::new(); - - app.name = "cli_tool".to_string(); - app.display_name = color::magenta(" + let display_name = color::magenta(" ██████╗██╗ ██╗ ██╔════╝██║ ██║ ██║ ██║ ██║ ██║ ██║ ██║ ╚██████╗███████╗██║ ╚═════╝╚══════╝╚═╝"); - app.usage = "cli_tool [command] [arg]".to_string(); - app.version = env!("CARGO_PKG_VERSION").to_string(); - app.commands = vec![command]; + let command = Command { + name: "hello", + usage: "cli_tool hello user", + action: |v: Vec| println!("Hello, {:?}", v) + }; + + let mut app = App::new() + .name("cli_tool") + .display_name("display_name") + .usage("cli_tool [command] [arg]") + .version(env!("CARGO_PKG_VERSION")) + .commands(vec![command]); - app.run(args.clone()); + app.run(args); } ``` \ No newline at end of file diff --git a/example/Cargo.toml b/example/Cargo.toml new file mode 100644 index 0000000..f2dc6de --- /dev/null +++ b/example/Cargo.toml @@ -0,0 +1,10 @@ +[package] +name = "example" +version = "0.1.0" +authors = ["KeisukeToyota "] +edition = "2018" + +# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html + +[dependencies] +seahorse = { path = "../" } diff --git a/example/src/main.rs b/example/src/main.rs new file mode 100644 index 0000000..6894c10 --- /dev/null +++ b/example/src/main.rs @@ -0,0 +1,19 @@ +use seahorse::{color, App, Command}; +use std::env; + +fn main() { + let args: Vec = env::args().collect(); + let c = Command::new("hello", "app hello [args]", test_action); + let app = App::new() + .name("app") + .display_name(color::red("app")) + .usage("app [command] [args]") + .version("0.0.1") + .commands(vec![c]); + + app.run(args); +} + +fn test_action(v: Vec) { + println!("Hello, {:?}", v); +} diff --git a/src/lib.rs b/src/lib.rs index 523df10..ab99ec6 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -8,6 +8,17 @@ pub struct Command { pub action: Action, } +impl Command { + pub fn new>(name: T, usage: T, action: Action) -> Self { + Self { + name: name.into(), + usage: usage.into(), + action, + } + } +} + +#[derive(Default)] pub struct App { pub name: String, pub display_name: String, @@ -18,13 +29,32 @@ pub struct App { impl App { pub fn new() -> Self { - Self { - name: "".to_string(), - display_name: "".to_string(), - usage: "".to_string(), - version: "".to_string(), - commands: Vec::::new(), - } + Self::default() + } + + pub fn name>(mut self, name: T) -> Self { + self.name = name.into(); + self + } + + pub fn display_name>(mut self, display_name: T) -> Self { + self.display_name = display_name.into(); + self + } + + pub fn usage>(mut self, usage: T) -> Self { + self.usage = usage.into(); + self + } + + pub fn version>(mut self, version: T) -> Self { + self.version = version.into(); + self + } + + pub fn commands(mut self, commands: Vec) -> Self { + self.commands = commands; + self } pub fn run(&self, args: Vec) { @@ -83,20 +113,17 @@ impl App { #[cfg(test)] mod tests { - use super::{App, Command}; + use super::{Action, App, Command}; #[test] fn app_test() { - let c = Command { - name: "hello".to_string(), - usage: "test hello user".to_string(), - action: |v: Vec| println!("Hello, {:?}", v), - }; - let mut app = App::new(); - app.name = "test".to_string(); - app.usage = "test [command] [arg]".to_string(); - app.version = "0.0.1".to_string(); - app.commands = vec![c]; + let a: Action = |v: Vec| println!("Hello, {:?}", v); + let c = Command::new("hello", "test hello user", a); + let app = App::new() + .name("test") + .usage("test [command] [arg]") + .version("0.0.1") + .commands(vec![c]); app.run(vec![ "test".to_string(),