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

Question/Idea: Parse config file via clap-rs? #1519

Closed
michael-smythe opened this issue Jul 16, 2019 · 4 comments
Closed

Question/Idea: Parse config file via clap-rs? #1519

michael-smythe opened this issue Jul 16, 2019 · 4 comments

Comments

@michael-smythe
Copy link

michael-smythe commented Jul 16, 2019

I have a program which looks like this:

let matches = App::new("Great Program")
                 .version("0.0.1")
                 .arg(Arg::with_name("great_option_a")
                    .short("a")
                    .long("great_option_a")
                    .value_name("A_NAME")
                    .help("Specify great_option_a's name")
                    .required(true)
                 )
                 .arg(Arg::with_name("great_option_b")
                    .short("b")
                    .long("great_option_b")
                    .value_name("B_NAME")
                    .help("Specify great_option_b's name")
                    .required(true)
                 )
                 ==== TRUNCATED ====
                 .arg(Arg::with_name("great_option_z")
                    .short("z")
                    .long("great_option_z")
                    .value_name("Z_NAME")
                    .help("Specify great_option_z's name")
                    .required(true)
                 )
                 .get_matches();

The program works great, because its a great program, but something that I would like to be able to do is use a config file instead of specify all of these options via the cli at runtime. Is there a way to leverage the backend logic that gets built by clap where it parses and validates all or some of the options that I would normally have to type as arguments? For instance if I was to make a file that contained the following:

cat ./greatest_config
great_option_a="A"
great_option_b="B"
==== TRUNCATED ====
great_option_z="Z"

Would it be possible using clap to have something which makes the following commands equivelant:

./great_program -a A -b B ==== TRUNCATERD ==== -z Z
./great_program --config_file=./greatest_config

Or is the correct answer that I would have to build a seperate parsing and validating system for when the option of a config file is preseted and ensure that the logic matches the logic setup by the clap parsing engine? If this a completely idiotic question please just point me in the right direction, if this is not a completely dumb question and it does not currently exist inside of clap is it a feature the clap-rs project would be interested in integrating? Also if there is a better venue for this conversation please let me know. Thanks in advance for any help/insight you may be able to provide!

@comfortablynick
Copy link

I had this same idea recently. I am using using clap for my cli and reading from a toml config file, and they share some options. Serde converts the toml file into a separate struct, so for now I'll have to validate them separately. But it would be great to have some type of integration.

@ebkalderon
Copy link

@comfortablynick Would you be able to use structopt with its clap backend and then derive Serialize and Deserialize on the struct to achieve the same effect? This won't fully solve this issue, but it might make your current experience a bit more streamlined.

@davidMcneil
Copy link
Contributor

Here is proof-of-concept work on parsing a config file.

@pksunkara
Copy link
Member

#1693 should be fixing this. I would like to close this issue in favour of that. Please reopen with arguments if I missed something.

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

No branches or pull requests

6 participants