-
Make sure you completed the following tasks
Describe your use caseI would like to use sensitive information from environment variables and have them being hidden from the CLI help. In my case I would like to hide my GitHub token. I found the feature I wanted in clap 2 with ArgSettings::HideEnvValues, but it looks like it has not been ported to Clap 3. I am also trying to make arguments backed by an environment variable optional (it is currently shown as a mandatory option):
Describe the solution you'd likeCreate a project: cd /tmp
cargo new sensitive-env-var
cd sensitive-env-var/
cargo add clap --allow-prerelease The code: use clap::Clap;
/// A utility to collect GitHub information.
#[derive(Clap, Debug)]
pub struct Opts {
// Set GitHub token.
#[clap(long, env = "GITHUB_TOKEN")]
token: String,
}
fn main() {
let opts: Opts = Opts::parse();
dbg!(opts);
println!("A GitHub token was provided!");
} And unset the GitHub token with First case, no token is provided at all: $ cargo run -q
error: The following required arguments were not provided:
--token <token>
USAGE:
sensitive-env-var --token <token>
For more information try --help Clap shows an error, everything as expected, except for the fact that Second case, we pass a token via the cli: $ cargo run -q -- --token 1234abcd
[src/main.rs:13] opts = Opts {
token: "1234abcd",
}
A GitHub token was provided! Here, we are happy, no problem. Third case, we pass a token via the environment variable: $ export GITHUB_TOKEN=0987poiu
$ cargo run -q
[src/main.rs:13] opts = Opts {
token: "0987poiu",
}
A GitHub token was provided! We are also happy with the application behavior, however, the token is displayed on the help screen: $ cargo run -q -- --help
sensitive-env-var
A utility to collect GitHub information
USAGE:
sensitive-env-var --token <token>
FLAGS:
-h, --help Prints help information
-V, --version Prints version information
OPTIONS:
--token <token> [env: GITHUB_TOKEN=0987poiu] The sensitive value should be hidden here. My ultimate use case: I modified my token: Option<String>, And now what I would like to achieve: $ cargo run -q -- --help
sensitive-env-var
A utility to collect GitHub information
USAGE:
sensitive-env-var [OPTIONS]
FLAGS:
-h, --help Prints help information
-V, --version Prints version information
OPTIONS:
--token <token> [env: GITHUB_TOKEN]
|
Beta Was this translation helpful? Give feedback.
Replies: 6 comments 2 replies
-
Wait a minute, I see it! It's right there! Unless I'm having hallucinations - but this is unlikely because I'm out of crack - you should be able to just use it, can't you? Or are you reporting it fails to work? |
Beta Was this translation helpful? Give feedback.
-
Then maybe an example with derive? I tried a few combinations, like this: #[clap(long, env = "GITHUB_TOKEN", hideenvvalues)] but I was not able to get it to work. |
Beta Was this translation helpful? Give feedback.
-
This should work: #[clap(long, env = "GITHUB_TOKEN", setting = ArgSettings::HideEnvValues)] |
Beta Was this translation helpful? Give feedback.
-
That worked! Thank you very much! I solved my problem number 2 ("I don't want the use anyhow::{ensure, Result};
use clap::{ArgSettings, Clap};
/// A utility to collect GitHub information.
#[derive(Clap, Debug)]
pub struct Opts {
// Set GitHub token.
#[clap(long, env = "GITHUB_TOKEN", setting = ArgSettings::HideEnvValues)]
token: Option<String>,
}
fn main() -> Result<()> {
let opts: Opts = Opts::parse();
ensure!(opts.token.is_some(), "A GitHub token must be provided.");
println!("A GitHub token was provided!");
Ok(())
} Is there a more clap-like approach to achieve this? |
Beta Was this translation helpful? Give feedback.
-
validators are made for this scenario. I am moving this to discussion. |
Beta Was this translation helpful? Give feedback.
-
I ran into this again in v4.3.23, and the syntax for the fix has since changed. I now use
|
Beta Was this translation helpful? Give feedback.
This should work:
#[clap(long, env = "GITHUB_TOKEN", setting = ArgSettings::HideEnvValues)]