clap-rs / clap

A full featured, fast Command Line Argument Parser for Rust
docs.rs/clap
Apache License 2.0
14.13k stars 1.03k forks source link

Environment variable name aliases #5447

Open Aeron opened 5 months ago

Aeron commented 5 months ago

Please complete the following tasks

Clap Version

4.4.8

Describe your use case

Clap already supports aliases for short and long argument names, which is superb, especially for keeping backward compatibility. Yet environment variables lack this feature that can be useful in all the same cases.

For example, if my CLI changes one of its arguments’ name, I can keep compatibility with short_alias and alias functions and derive parameters. But if such an argument has an environment variable assigned, I either need to keep the name that became inconsistent with the argument name or introduce a breaking CLI change.

For another case, the same will be true if I no longer need separate arguments for something that now can be treated as one value, so I need to merge two argument names. The short_aliases and aliases will cover it great. But environment variables will hold me back from doing so.

Describe the solution you'd like

It’d be nice to have the same alias options for environment variables, like env_aliases and env_alias—a super-similar implementation to what argument names have.

Alternatives, if applicable

No response

Additional Context

I can prepare a PR if there are no objections, considerations, or pitfalls I’m missing. Maybe I overlooked something, or there are some limitations.

epage commented 5 months ago

I can prepare a PR if there are no objections, considerations, or pitfalls I’m missing. Maybe I overlooked something, or there are some limitations.

I appreciate the willingness. My main hesitation is just around figuring out where the line is for what feature we provide. Sometimes providing features that overlap just enough with out use case lead people wanting a lot more like #2763. Each feature also has an impact, on discoverability, on binary size (#2038), and and build-times (#1365). This is behind an env feature which helps but even that has its downsides.

So I think I want to sit on this a bit, let my thoughts settle on this a bit and to see what other input comes along.

survived commented 2 months ago

I was surprised to find out that env aliases are not supported, which was unexpected because there are aliases for short/long arg names. I think env aliases do improve usability and user experience.

dinhani commented 2 months ago

I also want this feature.

For our deployed services, environment variables are the main way to configure applications because they are integrated with configurations/secrets from cloud providers.

Aliases would help preventing breaking changes.

Today we have to do something like this before parsing configurations:

if let Ok(value) = env::var("SOME_ALIAS_NAME") {
    env::set_var("CANONICAL_NAME", value);
}