mirror of
https://github.com/clap-rs/clap
synced 2025-01-18 23:53:54 +00:00
api: adds new App method calls for printing and writing the long version of help messages
One can now use `App::print_long_help` or `App::write_long_help` Note that `App::write_long_help` is **NOT** affected by kbknapp/clap-rs#808 in the manner that `App::write_help` help is and thus should be preferred if at all possible.
This commit is contained in:
parent
6b371891a1
commit
d82b4be5d7
1 changed files with 76 additions and 3 deletions
|
@ -1095,7 +1095,11 @@ impl<'a, 'b> App<'a, 'b> {
|
|||
self
|
||||
}
|
||||
|
||||
/// Prints the full help message to [`io::stdout()`] using a [`BufWriter`]
|
||||
/// Prints the full help message to [`io::stdout()`] using a [`BufWriter`] using the same
|
||||
/// method as if someone ran `-h` to request the help message
|
||||
///
|
||||
/// **NOTE:** clap has the ability to distinguish between "short" and "long" help messages
|
||||
/// depending on if the user ran [`-h` (short)] or [`--help` (long)]
|
||||
///
|
||||
/// # Examples
|
||||
///
|
||||
|
@ -1106,6 +1110,8 @@ impl<'a, 'b> App<'a, 'b> {
|
|||
/// ```
|
||||
/// [`io::stdout()`]: https://doc.rust-lang.org/std/io/fn.stdout.html
|
||||
/// [`BufWriter`]: https://doc.rust-lang.org/std/io/struct.BufWriter.html
|
||||
/// [`-h` (short)]: ./struct.Arg.html#method.help
|
||||
/// [`--help` (long)]: ./struct.Arg.html#method.long_help
|
||||
pub fn print_help(&mut self) -> ClapResult<()> {
|
||||
// If there are global arguments, or settings we need to propgate them down to subcommands
|
||||
// before parsing incase we run into a subcommand
|
||||
|
@ -1119,7 +1125,45 @@ impl<'a, 'b> App<'a, 'b> {
|
|||
self.write_help(&mut buf_w)
|
||||
}
|
||||
|
||||
/// Writes the full help message to the user to a [`io::Write`] object
|
||||
/// Prints the full help message to [`io::stdout()`] using a [`BufWriter`] using the same
|
||||
/// method as if someone ran `-h` to request the help message
|
||||
///
|
||||
/// **NOTE:** clap has the ability to distinguish between "short" and "long" help messages
|
||||
/// depending on if the user ran [`-h` (short)] or [`--help` (long)]
|
||||
///
|
||||
/// # Examples
|
||||
///
|
||||
/// ```rust
|
||||
/// # use clap::App;
|
||||
/// let mut app = App::new("myprog");
|
||||
/// app.print_long_help();
|
||||
/// ```
|
||||
/// [`io::stdout()`]: https://doc.rust-lang.org/std/io/fn.stdout.html
|
||||
/// [`BufWriter`]: https://doc.rust-lang.org/std/io/struct.BufWriter.html
|
||||
/// [`-h` (short)]: ./struct.Arg.html#method.help
|
||||
/// [`--help` (long)]: ./struct.Arg.html#method.long_help
|
||||
pub fn print_long_help(&mut self) -> ClapResult<()> {
|
||||
// If there are global arguments, or settings we need to propgate them down to subcommands
|
||||
// before parsing incase we run into a subcommand
|
||||
self.p.propogate_globals();
|
||||
self.p.propogate_settings();
|
||||
self.p.derive_display_order();
|
||||
|
||||
self.p.create_help_and_version();
|
||||
let out = io::stdout();
|
||||
let mut buf_w = BufWriter::new(out.lock());
|
||||
self.write_long_help(&mut buf_w)
|
||||
}
|
||||
|
||||
/// Writes the full help message to the user to a [`io::Write`] object in the same method as if
|
||||
/// the user ran `-h`
|
||||
///
|
||||
/// **NOTE:** clap has the ability to distinguish between "short" and "long" help messages
|
||||
/// depending on if the user ran [`-h` (short)] or [`--help` (long)]
|
||||
///
|
||||
/// **NOTE:** There is a known bug where this method does not write propogated global arguments
|
||||
/// or autogenerated arguments (i.e. the default help/version args). Prefer
|
||||
/// [`App::write_long_help`] instead if possibe!
|
||||
///
|
||||
/// # Examples
|
||||
///
|
||||
|
@ -1131,6 +1175,8 @@ impl<'a, 'b> App<'a, 'b> {
|
|||
/// app.write_help(&mut out).expect("failed to write to stdout");
|
||||
/// ```
|
||||
/// [`io::Write`]: https://doc.rust-lang.org/std/io/trait.Write.html
|
||||
/// [`-h` (short)]: ./struct.Arg.html#method.help
|
||||
/// [`--help` (long)]: ./struct.Arg.html#method.long_help
|
||||
pub fn write_help<W: Write>(&self, w: &mut W) -> ClapResult<()> {
|
||||
// PENDING ISSUE: 808
|
||||
// https://github.com/kbknapp/clap-rs/issues/808
|
||||
|
@ -1144,6 +1190,33 @@ impl<'a, 'b> App<'a, 'b> {
|
|||
Help::write_app_help(w, self, false)
|
||||
}
|
||||
|
||||
/// Writes the full help message to the user to a [`io::Write`] object in the same method as if
|
||||
/// the user ran `--help`
|
||||
///
|
||||
/// **NOTE:** clap has the ability to distinguish between "short" and "long" help messages
|
||||
/// depending on if the user ran [`-h` (short)] or [`--help` (long)]
|
||||
///
|
||||
/// # Examples
|
||||
///
|
||||
/// ```rust
|
||||
/// # use clap::App;
|
||||
/// use std::io;
|
||||
/// let mut app = App::new("myprog");
|
||||
/// let mut out = io::stdout();
|
||||
/// app.write_long_help(&mut out).expect("failed to write to stdout");
|
||||
/// ```
|
||||
/// [`io::Write`]: https://doc.rust-lang.org/std/io/trait.Write.html
|
||||
/// [`-h` (short)]: ./struct.Arg.html#method.help
|
||||
/// [`--help` (long)]: ./struct.Arg.html#method.long_help
|
||||
pub fn write_long_help<W: Write>(&mut self, w: &mut W) -> ClapResult<()> {
|
||||
self.p.propogate_globals();
|
||||
self.p.propogate_settings();
|
||||
self.p.derive_display_order();
|
||||
self.p.create_help_and_version();
|
||||
|
||||
Help::write_app_help(w, self, true)
|
||||
}
|
||||
|
||||
/// Writes the version message to the user to a [`io::Write`] object
|
||||
///
|
||||
/// # Examples
|
||||
|
@ -1216,7 +1289,7 @@ impl<'a, 'b> App<'a, 'b> {
|
|||
/// build = "build.rs"
|
||||
///
|
||||
/// [build-dependencies]
|
||||
/// clap = "2.9"
|
||||
/// clap = "2.23"
|
||||
/// ```
|
||||
///
|
||||
/// Next, we place a `build.rs` in our project root.
|
||||
|
|
Loading…
Reference in a new issue