Skip to content

Commit ed73d00

Browse files
author
Trung Nguyen
committed
Make Commander::run{_with_args} exit on errors
1 parent 6b613a6 commit ed73d00

File tree

4 files changed

+19
-13
lines changed

4 files changed

+19
-13
lines changed

examples/clap_nested/main.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,5 @@ fn main() {
2828
println!("No subcommand matched");
2929
Ok(())
3030
})
31-
.run()
32-
.unwrap();
31+
.run();
3332
}

src/lib.rs

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -80,8 +80,7 @@
8080
//! println!("No subcommand matched");
8181
//! Ok(())
8282
//! })
83-
//! .run()
84-
//! .unwrap();
83+
//! .run();
8584
//! }
8685
//! ```
8786
//!
@@ -358,11 +357,19 @@ impl<'a, S: ?Sized, T: ?Sized> Commander<'a, S, T> {
358357
}
359358

360359
impl<'a, T: ?Sized> Commander<'a, (), T> {
361-
pub fn run(&self) -> Result {
362-
self.run_with_args(std::env::args_os())
360+
pub fn run(&self) {
361+
self.run_result().unwrap_or_else(|error| error.exit())
363362
}
364363

365-
pub fn run_with_args(
364+
pub fn run_with_args(&self, args: impl IntoIterator<Item = impl Into<OsString> + Clone>) {
365+
self.run_with_args_result(args).unwrap_or_else(|error| error.exit())
366+
}
367+
368+
pub fn run_result(&self) -> Result {
369+
self.run_with_args_result(std::env::args_os())
370+
}
371+
372+
pub fn run_with_args_result(
366373
&self,
367374
args: impl IntoIterator<Item = impl Into<OsString> + Clone>,
368375
) -> Result {

tests/common/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ pub fn assert_output<T>(
1414
out: &str,
1515
use_stderr: bool,
1616
) {
17-
assert_result(commander.run_with_args(args), out, use_stderr);
17+
assert_result(commander.run_with_args_result(args), out, use_stderr);
1818
}
1919

2020
pub fn assert_result(res: Result<(), clap::Error>, out: &str, use_stderr: bool) {

tests/lib.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -66,13 +66,13 @@ fn two_level_commander() {
6666
.add_cmd(show)
6767
.add_cmd(what);
6868

69-
assert!(commander.run_with_args(&["program", "show"]).is_ok());
70-
assert!(commander.run_with_args(&["program", "show", "foo"]).is_ok());
71-
assert!(commander.run_with_args(&["program", "show", "bar"]).is_ok());
72-
assert!(commander.run_with_args(&["program", "what"]).is_ok());
69+
assert!(commander.run_with_args_result(&["program", "show"]).is_ok());
70+
assert!(commander.run_with_args_result(&["program", "show", "foo"]).is_ok());
71+
assert!(commander.run_with_args_result(&["program", "show", "bar"]).is_ok());
72+
assert!(commander.run_with_args_result(&["program", "what"]).is_ok());
7373

7474
assert_result(
75-
commander.run(),
75+
commander.run_result(),
7676
"error: program __VERSION__
7777
__AUTHOR__
7878
__DESC__

0 commit comments

Comments
 (0)