Skip to content

Commit

Permalink
Merge pull request #77 from ksk001100/release2.2.0
Browse files Browse the repository at this point in the history
Release 2.2.0
  • Loading branch information
ksk001100 authored Dec 2, 2023
2 parents 2a0dcc8 + ec2c7c9 commit 36c5f28
Show file tree
Hide file tree
Showing 3 changed files with 88 additions and 7 deletions.
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 <[email protected]>"]
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),
},
};
}

0 comments on commit 36c5f28

Please sign in to comment.