Skip to content

Commit

Permalink
v0.2.0
Browse files Browse the repository at this point in the history
  • Loading branch information
ksk001100 committed Dec 10, 2019
1 parent 050bd3a commit 369cf5d
Show file tree
Hide file tree
Showing 6 changed files with 92 additions and 36 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
/target
**/target
**/*.rs.bk
Cargo.lock

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "seahorse"
version = "0.1.1"
version = "0.2.0"
authors = ["KeisukeToyota <[email protected]>"]
edition = "2018"
keywords = ["cli"]
Expand Down
31 changes: 15 additions & 16 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ A minimal CLI framework written in Rust

```toml
[dependencies]
seahorse = "0.1.1"
seahorse = "0.2.0"
```

## Example
Expand All @@ -17,27 +17,26 @@ use seahorse::{App, Command, color};

fn main() {
let args: Vec<String> = env::args().collect();

let command = Command {
name: "hello".to_string(),
usage: "cli_tool hello user".to_string(),
action: |v: Vec<String>| 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<String>| 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);
}
```
10 changes: 10 additions & 0 deletions example/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
[package]
name = "example"
version = "0.1.0"
authors = ["KeisukeToyota <[email protected]>"]
edition = "2018"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
seahorse = { path = "../" }
19 changes: 19 additions & 0 deletions example/src/main.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
use seahorse::{color, App, Command};
use std::env;

fn main() {
let args: Vec<String> = 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<String>) {
println!("Hello, {:?}", v);
}
63 changes: 45 additions & 18 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,17 @@ pub struct Command {
pub action: Action,
}

impl Command {
pub fn new<T: Into<String>>(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,
Expand All @@ -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::<Command>::new(),
}
Self::default()
}

pub fn name<T: Into<String>>(mut self, name: T) -> Self {
self.name = name.into();
self
}

pub fn display_name<T: Into<String>>(mut self, display_name: T) -> Self {
self.display_name = display_name.into();
self
}

pub fn usage<T: Into<String>>(mut self, usage: T) -> Self {
self.usage = usage.into();
self
}

pub fn version<T: Into<String>>(mut self, version: T) -> Self {
self.version = version.into();
self
}

pub fn commands(mut self, commands: Vec<Command>) -> Self {
self.commands = commands;
self
}

pub fn run(&self, args: Vec<String>) {
Expand Down Expand Up @@ -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<String>| 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<String>| 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(),
Expand Down

0 comments on commit 369cf5d

Please sign in to comment.