feat: allows waiting for user input on error

In order to pause for user input on error use `.wait_on_error(true)` but
it's important to note that this is *not* recursive through subcommands.

This is useful on Windows when a user mistakenly opens an application by
double clicking it, instead of using the command line or if using a GUI
shortcut to run a program.

Closes #140
This commit is contained in:
Kevin K 2015-06-29 21:55:44 -04:00
parent 26d5ae3e33
commit d0da3bdd9d

View file

@ -4,6 +4,7 @@ use std::collections::HashSet;
use std::collections::HashMap; use std::collections::HashMap;
use std::collections::VecDeque; use std::collections::VecDeque;
use std::env; use std::env;
use std::io::{self, BufRead};
use std::path::Path; use std::path::Path;
use std::vec::IntoIter; use std::vec::IntoIter;
use std::process; use std::process;
@ -120,6 +121,7 @@ pub struct App<'a, 'v, 'ab, 'u, 'h, 'ar> {
groups: HashMap<&'ar str, ArgGroup<'ar, 'ar>>, groups: HashMap<&'ar str, ArgGroup<'ar, 'ar>>,
global_args: Vec<Arg<'ar, 'ar, 'ar, 'ar, 'ar, 'ar>>, global_args: Vec<Arg<'ar, 'ar, 'ar, 'ar, 'ar, 'ar>>,
no_sc_error: bool, no_sc_error: bool,
wait_on_error: bool,
help_on_no_args: bool, help_on_no_args: bool,
help_str: Option<&'u str>, help_str: Option<&'u str>,
help_on_no_sc: bool help_on_no_sc: bool
@ -168,6 +170,7 @@ impl<'a, 'v, 'ab, 'u, 'h, 'ar> App<'a, 'v, 'ab, 'u, 'h, 'ar>{
global_args: vec![], global_args: vec![],
no_sc_error: false, no_sc_error: false,
help_str: None, help_str: None,
wait_on_error: false,
help_on_no_args: false, help_on_no_args: false,
help_on_no_sc: false help_on_no_sc: false
} }
@ -443,6 +446,31 @@ impl<'a, 'v, 'ab, 'u, 'h, 'ar> App<'a, 'v, 'ab, 'u, 'h, 'ar>{
self self
} }
/// Will display a message "Press [ENTER]/[RETURN] to continue..." and wait user before
/// exiting
///
/// This is most useful when writing an application which is run from a GUI shortcut, or on
/// Windows where a user tries to open the binary by double-clicking instead of using the
/// command line (i.e. set `.arg_required_else_help(true)` and `.wait_on_error(true)` to
/// display the help in such a case).
///
/// **NOTE:** This setting is **not** recursive with subcommands, meaning if you wish this
/// behavior for all subcommands, you must set this on each command (needing this is extremely
/// rare)
///
/// # Example
///
/// ```no_run
/// # use clap::{App, Arg};
/// App::new("myprog")
/// .arg_required_else_help(true)
/// # ;
/// ```
pub fn wait_on_error(mut self, w: bool) -> App<'a, 'v, 'ab, 'u, 'h, 'ar> {
self.wait_on_error = w;
self
}
/// Specifies that the help text sould be displayed (and then exit gracefully), if no /// Specifies that the help text sould be displayed (and then exit gracefully), if no
/// subcommands are present at runtime (i.e. an empty run such as, `$ myprog`. /// subcommands are present at runtime (i.e. an empty run such as, `$ myprog`.
/// ///
@ -1402,6 +1430,12 @@ impl<'a, 'v, 'ab, 'u, 'h, 'ar> App<'a, 'v, 'ab, 'u, 'h, 'ar>{
// Exits with a status code passed to the OS // Exits with a status code passed to the OS
// This is legacy from before std::process::exit() and may be removed evenutally // This is legacy from before std::process::exit() and may be removed evenutally
fn exit(&self, status: i32) { fn exit(&self, status: i32) {
if self.wait_on_error {
println!("\nPress [ENTER] / [RETURN] to continue...");
let mut s = String::new();
let i = io::stdin();
i.lock().read_line(&mut s).unwrap();
}
process::exit(status); process::exit(status);
} }