Skip to content
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

No way to create a global template #1184

Open
anna-is-cute opened this issue Feb 14, 2018 · 5 comments
Open

No way to create a global template #1184

anna-is-cute opened this issue Feb 14, 2018 · 5 comments
Labels
A-builder Area: Builder API A-help Area: documentation, including docs.rs, readme, examples, etc... C-enhancement Category: Raise on the bar on expectations S-waiting-on-design Status: Waiting on user-facing design to be resolved before implementing

Comments

@anna-is-cute
Copy link

anna-is-cute commented Feb 14, 2018

Affected Version of clap

2.29.4

Expected Behavior Summary

Being able to call global_template and have the template propagate down to subcommands.

Actual Behavior Summary

For every single subcommand, I have to add a template call to get them all to match my custom template.

@kbknapp
Copy link
Member

kbknapp commented Feb 14, 2018

Totally agree this is a lacking feature, thanks for the suggestion!

In order for me to actually get v3 released I have to put blinders on and only implement bug fixes or super trivial features which are simple to forward port to the v3 branch.

I don't know that this is one such issue. I'll leave it open for v3 as it is something I do want to have.

Having said all this, if someone implements this for v2 I'm not against the PR, so long as the understanding is that for v3 I may have to rip out the commit and re-do it depending how deep into the internals it gets to implement this. (for v3 the internals are quite different, so some changes aren't trivial to forward port).

@kbknapp kbknapp added A-help Area: documentation, including docs.rs, readme, examples, etc... C: subcommands labels Feb 14, 2018
@kbknapp kbknapp mentioned this issue Feb 15, 2018
87 tasks
@kbknapp kbknapp added this to the v3-alpha.1 milestone Jul 22, 2018
@kbknapp kbknapp added E-medium Call for participation: Experience needed to fix: Medium / intermediate E-easy Call for participation: Experience needed to fix: Easy / not much labels Jul 22, 2018
@pksunkara pksunkara modified the milestones: v3-alpha.2, v3.0 Feb 1, 2020
@pksunkara pksunkara modified the milestones: 3.0, 3.1 Oct 9, 2020
@pksunkara pksunkara added 💸 $5 D: easy and removed W: 3.x E-medium Call for participation: Experience needed to fix: Medium / intermediate labels Oct 9, 2020
@pksunkara
Copy link
Member

To solve this, we would either need to add a global_help_template function that would forward the template to children (example) or we could add a setting called PropagateTemplate which does the same.

@epage epage added A-builder Area: Builder API and removed C: subcommands labels Dec 8, 2021
@epage epage removed this from the 3.1 milestone Dec 8, 2021
@epage epage removed D: easy E-easy Call for participation: Experience needed to fix: Easy / not much labels Dec 9, 2021
@epage epage added the S-waiting-on-design Status: Waiting on user-facing design to be resolved before implementing label Dec 9, 2021
@epage
Copy link
Member

epage commented Dec 9, 2021

One idea I have for this (and other issues) is if we implemented a layered config. Some fields would be designated as layered, like this, and others would not, like a subcommands display order. For layered fields, we propagate them to subcommands if they are set in the parent but not the sucommand, and then continue this recursively.

So build would have code like:

if let Some(help_template) = self.help_template.as_deref() {
    sub.help_template.get_or_insert(help_template)
}

@pksunkara
Copy link
Member

I think you might need to expand on this. I am quite confused reading your comment.

@epage
Copy link
Member

epage commented Dec 10, 2021

Yeah, I can see needing to re-phrase since i'm using a term in an adjacent domain for which we have feature requests ;).

What if we modeled clap's builder API after layered config? What I mean is where you take configuration from different sources and merge them together.

The approach I've taken in several applications that I've been finding works well is that the config data tracks what is explicitly set, using Option. You then build the final config by merging all the sources by reading each layer and reading the explicitly set values, giving precedence to the higher layer. All the config fields are private and we expose getters that contain the default value so the callers don't care about these details. Granted, when a user does a --dump-config, I want to merge in the defaults so they can see them

So what this would look like in clap is an App would have all "global" fields be Option<T>. When we build, we merge the parent Apps global fields into its subcommand with code blocks similar to

if let Some(help_template) = self.help_template.as_deref() {
    subcommand.help_template.get_or_insert(help_template)
}

This will preserve the subcommands original help template. If it was never explicitly set by the user, the parent command's help template would be used.

Depending on performance, we might also apply the default layer so that the accessors don't have to re-compute it on each call.

Arg::display_order is part way there at this point

@epage epage added the C-enhancement Category: Raise on the bar on expectations label Dec 13, 2021
nmlgc added a commit to nmlgc/mly that referenced this issue Feb 29, 2024
clap unfortunately doesn't support sharing help templates between
subcommands; see clap-rs/clap#1184. But as it
will turn out, we don't *just* want to share a single template, but
conditionally add certain snippets to the `Usage:` section of certain
subcommands, depending on their arguments.

Also, given our plans, the const_format crate isn't really worth it
just for those {INDENT}s…

Part of P0266, funded by Ember2528.
@pksunkara pksunkara removed the 💸 $5 label Jun 7, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-builder Area: Builder API A-help Area: documentation, including docs.rs, readme, examples, etc... C-enhancement Category: Raise on the bar on expectations S-waiting-on-design Status: Waiting on user-facing design to be resolved before implementing
Projects
None yet
Development

No branches or pull requests

4 participants