Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Release 2.2.0 #77

Merged
merged 1 commit into from
Dec 2, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 = "2.1.0"
version = "2.2.0"
authors = ["ksk001100 <hm.pudding0715@gmail.com>"]
edition = "2018"
keywords = [
Expand Down
61 changes: 55 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ To use seahorse, add this to your Cargo.toml:

```toml
[dependencies]
seahorse = "2.1"
seahorse = "2.2"
```

## Example
Expand Down Expand Up @@ -191,11 +191,11 @@ fn calc_action(c: &Context) {
println!("{}", sum);
}
Err(e) => match e {
FlagError::Undefined => panic!("undefined operator..."),
FlagError::ArgumentError => panic!("argument error..."),
FlagError::NotFound => panic!("not found flag..."),
FlagError::ValueTypeError => panic!("value type mismatch..."),
FlagError::TypeError => panic!("flag type mismatch..."),
FlagError::Undefined => panic!("undefined operator..."),
FlagError::ArgumentError => panic!("argument error..."),
FlagError::NotFound => panic!("not found flag..."),
FlagError::ValueTypeError => panic!("value type mismatch..."),
FlagError::TypeError => panic!("flag type mismatch..."),
},
}
}
Expand Down Expand Up @@ -236,6 +236,55 @@ $ cli calc -op sub 10 6 3 2
-21
```

### Top level error handling

```rust
use seahorse::{ActionError, App, Context, Flag, FlagType};
use std::env;

fn main() {
let args: Vec<String> = env::args().collect();
let app = App::new(env!("CARGO_PKG_NAME"))
.author(env!("CARGO_PKG_AUTHORS"))
.description(env!("CARGO_PKG_DESCRIPTION"))
.usage("multiple_app [command] [arg]")
.version(env!("CARGO_PKG_VERSION"))
.action_with_result(|c: &Context| {
if c.bool_flag("error") {
Err(ActionError {
message: "ERROR...".to_string(),
})
} else {
Ok(())
}
})
.flag(
Flag::new("error", FlagType::Bool)
.description("error flag")
.alias("e"),
);

match app.run_with_result(args) {
Ok(_) => println!("OK"),
Err(e) => match e {
ActionError { message } => println!("{}", message),
},
};
}
```

```bash
$ cli
OK

$ cli --error
ERROR...

$ cli -e
ERROR...
```


## Contributing
Please read [CONTRIBUTING.md](.github/CONTRIBUTING.md) for details on our code of conduct, and the process for submitting pull requests to us.

Expand Down
32 changes: 32 additions & 0 deletions examples/top_level_error_handling.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
use seahorse::{ActionError, App, Context, Flag, FlagType};
use std::env;

fn main() {
let args: Vec<String> = env::args().collect();
let app = App::new("cli")
.author(env!("CARGO_PKG_AUTHORS"))
.description(env!("CARGO_PKG_DESCRIPTION"))
.usage("multiple_app [command] [arg]")
.version(env!("CARGO_PKG_VERSION"))
.action_with_result(|c: &Context| {
if c.bool_flag("error") {
Err(ActionError {
message: "ERROR...".to_string(),
})
} else {
Ok(())
}
})
.flag(
Flag::new("error", FlagType::Bool)
.description("error flag")
.alias("e"),
);

match app.run_with_result(args) {
Ok(_) => println!("OK"),
Err(e) => match e {
ActionError { message } => println!("{}", message),
},
};
}