mirror of
https://github.com/clap-rs/clap
synced 2024-12-05 02:29:12 +00:00
05cd057978
In some sophisticated situations, these may be expensive to calculate. One example might be a '--branch' option accepting any single Git branch that exists on the remote -- in such a case, the remote would need to be queried for all possible_values. The cost is ultimately unavoidable at runtime since this validation has to happen eventually, but there's no need to pay it when generating help text if `is_hide_possible_values_set`. To keep '-h' fast, avoid collecting `possible_values` during '-h' unless we're actually going to use the values in display. This optimization is repeated for the manpage renderer. This is trivially based on the short-circuiting logic at [1], which at least supports the idea that actually consuming the iterator is not generally-guaranteed behavior when `hide_possible_values` is set. Note on the 'expensive' mod: This keeps all the possible_values tests in one file but allows the entire set of tests to be controlled by the 'strings' feature (which is required to be able to use String rather than str for each possible value). [1]: clap_builder/src/builder/command.rs:long_help_exists_ |
||
---|---|---|
.. | ||
examples | ||
src | ||
tests | ||
Cargo.toml | ||
CHANGELOG.md | ||
CONTRIBUTING.md | ||
LICENSE-APACHE | ||
LICENSE-MIT | ||
README.md |
clap_mangen
Manpage generation for
clap
Dual-licensed under Apache 2.0 or MIT.
About
Generate ROFF from a clap::Command
.
Example
We're going to assume you want to generate your man page as part of your development rather than your shipped program having a flag to generate it.
Run
$ cargo add --build clap_mangen
In your build.rs
:
fn main() -> std::io::Result<()> {
let out_dir = std::path::PathBuf::from(std::env::var_os("OUT_DIR").ok_or(std::io::ErrorKind::NotFound)?);
let cmd = clap::Command::new("mybin")
.arg(clap::arg!(-n --name <NAME>))
.arg(clap::arg!(-c --count <NUM>));
let man = clap_mangen::Man::new(cmd);
let mut buffer: Vec<u8> = Default::default();
man.render(&mut buffer)?;
std::fs::write(out_dir.join("mybin.1"), buffer)?;
Ok(())
}
Tip: Consider a cargo xtask instead of a build.rs
to reduce build costs.