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

Allow accessing the env name and default value when iterating over the arguments #2367

Closed
2 tasks done
shepmaster opened this issue Feb 26, 2021 · 2 comments · Fixed by #2368
Closed
2 tasks done

Allow accessing the env name and default value when iterating over the arguments #2367

shepmaster opened this issue Feb 26, 2021 · 2 comments · Fixed by #2368
Labels
E-easy Call for participation: Experience needed to fix: Easy / not much 💸 $5
Milestone

Comments

@shepmaster
Copy link
Contributor

shepmaster commented Feb 26, 2021

Please complete the following tasks

  • I have searched the discussions
  • I have searched the existing issues

Describe your use case

I want to offer the ability to quickly generate a placeholder .env file for developers working on my projects. This information is contained with the App struct via the name / help text / environment variable name. However, I cannot access the environment variable or default value.

dotenv is a crate that allows specifying an .env file containing environment variables in a file. This allows developers to have a semi-persistent way to store and document environment variables needed to run an application.

Clap allows specifying environment variables as fallback sources for command line options. This includes the environment variable names, documentation about them, default values, possible values, etc.

Describe the solution you'd like

I want to reuse the definitions we've already given to Clap to generate a skeleton .env file:

use clap::{IntoApp, Clap};

#[derive(Clap)]
struct Config {
    #[clap(long, default_value = "world", env = "NAME")]
    /// The name to use
    name: String,
}

fn main() {
    let app: clap::App = Config::into_app();

    for arg in app.get_arguments() {
        println!("# {}", arg.get_about().unwrap_or(""));
        println!("# {}", arg.get_possible_values().unwrap_or(&[]).join(", "));
        println!("# {} = {}", arg.get_env(), arg.get_default_value());
        // Does not exist     ^^^^^^^^^^^^^  ^^^^^^^^^^^^^^^^^^^^^^^
    }
}

This will generate the output

# The name to use
# 
# NAME = world

A developer starting on the project can direct that output to a .env file and edit it as needed.

Alternatives, if applicable

No response

Additional Context

Ideally in a stable release of Clap, but in a beta release would be cool too!

@pksunkara
Copy link
Member

Need more description and examples. Not really sure what you are asking here.

@shepmaster
Copy link
Contributor Author

Updated.

@pksunkara pksunkara added this to the 3.1 milestone Feb 26, 2021
@pksunkara pksunkara added 💸 $5 C: args E-easy Call for participation: Experience needed to fix: Easy / not much labels Feb 26, 2021
shepmaster added a commit to integer32llc/clap that referenced this issue Feb 26, 2021
shepmaster added a commit to integer32llc/clap that referenced this issue Feb 26, 2021
shepmaster added a commit to integer32llc/clap that referenced this issue Mar 5, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
E-easy Call for participation: Experience needed to fix: Easy / not much 💸 $5
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants