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

Add App::name set function to use crate_name!() macro #908

Closed
SuperFluffy opened this issue Mar 20, 2017 · 3 comments
Closed

Add App::name set function to use crate_name!() macro #908

SuperFluffy opened this issue Mar 20, 2017 · 3 comments
Labels
A-builder Area: Builder API C-enhancement Category: Raise on the bar on expectations E-medium Call for participation: Experience needed to fix: Medium / intermediate

Comments

@SuperFluffy
Copy link
Contributor

Currently, you can further specify the about, authors, and version field of an App through setter methods of the same, and pull in the appropriate value through crate_about!() and similar macros. Only crate_name!() is special, in that we can only use it when constructing a new App through App::new.

If we construct an app through App::from_yaml, we have no way of setting the name to the one in Cargo.toml through the macro. It would be great to be able to set the name after App was constructed.

Is it possible to add this or is there a reason why this was omitted?

@kbknapp
Copy link
Member

kbknapp commented Mar 21, 2017

It's not omiited on purpose,probably just overlooked. Until I get a chance to add this (I recently broke my finger so am typing with one hand and it's not feasible to get real work done 😞) there is a workaround (that is technically public, but its not meant to be public and is undocumented. As such it may change in the future without warning):

let mut app = // load YAML like normal
app.p.meta.name = "Some Name".into();
let m = app.get_matches();

If someone would like to submit a PR with this, it'd be an easy one just adding the fn to src/app/mod.rs!

@kbknapp kbknapp added A-builder Area: Builder API D: easy E-medium Call for participation: Experience needed to fix: Medium / intermediate C-enhancement Category: Raise on the bar on expectations labels Mar 21, 2017
@SuperFluffy
Copy link
Contributor Author

Sorry about your hand. :( To make it heal faster I submitted #910.

homu added a commit that referenced this issue Mar 22, 2017
Implement name setter on App; fixes #908

Probably should have done that immediately before opening an issue.

<!-- Reviewable:start -->
---
This change is [<img src="https://reviewable.io/review_button.svg" height="34" align="absmiddle" alt="Reviewable"/>](https://reviewable.io/reviews/kbknapp/clap-rs/910)
<!-- Reviewable:end -->
@homu homu closed this as completed in feb3616 Mar 22, 2017
kbknapp pushed a commit that referenced this issue Mar 23, 2017
@larsrh
Copy link

larsrh commented Feb 13, 2018

What I find a bit strange here is that I need to use a dummy name in the YAML file, because otherwise clap does some strange stuff:

        let mut a = if let Some(name) = yaml["name"].as_str() {
            App::new(name)
        } else {
            let yaml_hash = yaml.as_hash().unwrap();
            let sc_key = yaml_hash.keys().nth(0).unwrap();
            is_sc = Some(yaml_hash.get(sc_key).unwrap());
            App::new(sc_key.as_str().unwrap())
        };

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-builder Area: Builder API C-enhancement Category: Raise on the bar on expectations E-medium Call for participation: Experience needed to fix: Medium / intermediate
Projects
None yet
Development

No branches or pull requests

3 participants