-
Notifications
You must be signed in to change notification settings - Fork 0
Expand template macros found in Rust comments [feature request] #159
Comments
Comment by CreepySkeleton I think this is one of those feature requests which are, while unquestionably useful, very hard to design and implement properly. Why derive-only won't doFor starters, this cannot be derive macro only feature because
Alternative proposalBuild it in clap itself: Arg::new("json")
.long("json")
.help("Path to the JSON file produced by '{bin} clean --out=JSON INPUT.TXT'") Then clap scans the help messages at runtime seeking for List of placeholdersAnything other than Runtime overheadIf this is implemented, clap will have to scan through all help messages at runtime. Good news is, the scanning will run only when generating help messages, and that ain't the part you expect to be amazingly fast. We may run the scan step only if |
Comment by sjackman
Yes, I agree that the expansion would happen at run time. I specifically want this feature when using Clap with its derive macros. When using the builder pattern, Arg::new("json")
.long("json")
.help(format!("Path to the JSON file produced by '{} clean --out=JSON INPUT.TXT'"), bin_name) When using the derive macro /// Path to the JSON file produced by '{bin} clean --out=JSON INPUT.TXT'
#[clap(long = "json")]
pub json: CliPath, the expansion of pub fn get_help<T: IntoApp>() -> String {
let mut output = Vec::new();
<T as IntoApp>::into_app().write_help(&mut output).unwrap();
- let output = String::from_utf8(output).unwrap();
+ let output = String::from_utf8(output).unwrap().replace("{bin}", self.get_bin_name()?);
eprintln!("\n%%% HELP %%%:=====\n{}\n=====\n", output);
eprintln!("\n%%% HELP (DEBUG) %%%:=====\n{:?}\n=====\n", output);
output
}
https://docs.rs/clap/2.33.1/clap/struct.App.html#method.template I looked through the other template macros, and |
Comment by pksunkara You should be able to use |
Comment by sjackman @CreepySkeleton pointed out above…
|
Comment by TeXitoi That's not totally true: the code is generated at expantiin time, but the given string can be generated at run time. See the lazy_static hack |
Comment by pickfire Templating doc comments is a bit hard even when using a macro but there is a tool on that https://github.com/dtolnay/paste. Not sure if this is useful. |
Issue by sjackman
Friday Jul 24, 2020 at 17:53 GMT
Originally opened as clap-rs/clap#2033
Make sure you completed the following tasks
Describe your use case
I'm using the
clap
with its derive macros. The same library code is used with two separate executables. I'd like to include the name of the executable in the about text of a command line argument. For example.Describe the solution you'd like
where
{bin}
is the name of one of the two executables that uses this library code.Alternatives, if applicable
We could convert the program to use builder notation rather than the derive macros, which would make it easier to programmatically format the text of an about option. All of our projects use the derive macros though, so this would be a deviation from our other projects. Is either the YAML or macro mode appropriate?
Additional context
Thank you very much for Clap! It's fantastic!
The text was updated successfully, but these errors were encountered: