Skip to content

Commit

Permalink
api: adds new App method calls for printing and writing the long vers…
Browse files Browse the repository at this point in the history
…ion 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 #808 in the manner
that `App::write_help` help is and thus should be preferred if at all possible.
  • Loading branch information
kbknapp committed Apr 5, 2017
1 parent 6b37189 commit d82b4be
Showing 1 changed file with 76 additions and 3 deletions.
79 changes: 76 additions & 3 deletions src/app/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
///
Expand All @@ -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
Expand All @@ -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
///
Expand All @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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.
Expand Down

0 comments on commit d82b4be

Please sign in to comment.