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 method to update env variables from custom source #4605

Closed
wants to merge 4 commits into from

Conversation

robertkiel
Copy link

@robertkiel robertkiel commented Jan 4, 2023

See #4607

tldr

Adds a method that allows parsing environment variables from a custom source.

Detailed description

When building Rust application that need to run without full access to std::env, e.g. when building an application that is supposed to run as a WebAssembly module as well as a standalone application, it becomes necessary to parse arguments from a custom source, such as cli_args iterator.

Currently, it is possible to pass in cli-args from a custom source but not environment variables, which means the WebAssembly module requires additional logic to parse arguments from environment variables. By having an update_env_from function, the standalone client and the WebAssembly module can share the same CLI code.

Example:

use std::env;
use std::collections::HashMap;
use clap::{Command, Arg};

let mut cmd = Command::new("myprog")
    .arg(Arg::new("data_dir")
        .long("data_dir")
        .env("MYPROG_DATA_DIR"));

env::set_var("MYPROG_DATA_DIR", "/home/user/Work/clap");

let env_vars = HashMap::from_iter(env::vars_os());
cmd.get_env_from(env_vars);

let m = cmd.get_matches_from(vec!["foo"]);
assert_eq!("/home/user/Work/clap", m.get_one::<String>("data_dir").unwrap());

@robertkiel robertkiel changed the title add 'update_env_from' add method to update env variables from custom source Jan 4, 2023
@epage
Copy link
Member

epage commented Jan 4, 2023

Could you open an issue for us to discuss the problem and possible solutions, per CONTRIBUTING.md?

@robertkiel
Copy link
Author

Could you open an issue for us to discuss the problem and possible solutions, per CONTRIBUTING.md?

Thanks for you reply, created issue #4607

@epage
Copy link
Member

epage commented Mar 27, 2023

Cl;osing as this doesn't align with our plans in #4793

@epage epage closed this Mar 27, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants