-
-
Notifications
You must be signed in to change notification settings - Fork 1.1k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Expand template macros found in Rust comments [feature request] #2033
Comments
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 |
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 |
You should be able to use |
@CreepySkeleton pointed out above…
|
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 |
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. |
Agreed that we'd want to implement this feature in the Builder for the Derive API to use. #2914's idea for code-genning help messages would open the door for us doing this without incurring runtime costs. |
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: