config-rs icon indicating copy to clipboard operation
config-rs copied to clipboard

Command-line configuration

Open liamdawson opened this issue 7 years ago • 9 comments

Would you consider adding CLI support? A scenario I'm thinking of would work very nicely if you could override most/all of the config from CLI input, and it seems redundant to use clap to find the config file, get the contents of the config file, and then clobber those values with CLI inputs where they exist.

In the C# world, I'd add the https://www.nuget.org/packages/Microsoft.Extensions.Configuration.CommandLine/ package, which would then allow inputs in a number of forms, which would then override the config file settings depending on precedence.

liamdawson avatar Feb 27 '18 04:02 liamdawson

A close integration with clap would definitely be a neat thing to have.

mehcode avatar Mar 12 '18 18:03 mehcode

what are your thoughts on this integration, should it be like multiple --conf key=value parameters passed through the command line which will override the values in the config

tanujitghosh avatar Jan 31 '19 17:01 tanujitghosh

If possible, it'd be great to reuse the names of fields in the struct, or allow them to be set via macros, so they can be used directly as flags (e.g. --key=value). Otherwise, yeah, --conf "key=value".

liamdawson avatar Feb 01 '19 07:02 liamdawson

sure will figure out how to do it through macros, still new to rust :) did initial version with multiple --conf parameter, let me know ur thoughts

https://github.com/tanujitghosh/config-rs/blob/issue-64/src/config.rs . - merge_args() https://github.com/tanujitghosh/config-rs/blob/issue-64/tests/file_toml.rs . - test case here

tanujitghosh avatar Feb 01 '19 12:02 tanujitghosh

An integration with Clap will be the way to go in my opinion too. Did you start working on that? Otherwise, I might give it a shot.

omarabid avatar Mar 11 '19 17:03 omarabid

It seems to me clap integration could be implemented by providing a derive macro allowing a user to convert a struct into a Config value, which could then be merged with other sources in the usual way.

This would also allow the programmer to declare a single static struct with all of the default values, and convert that into the base Config for merger with other sources.

It should be possible to do a poor man's version of this already by implementing Serialize for the struct produced by clap and then deserializing the result into a Config.

blankhart avatar Jan 23 '21 18:01 blankhart