Skip to content

Command-line configuration #64

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

Open
liamdawson opened this issue Feb 27, 2018 · 9 comments
Open

Command-line configuration #64

liamdawson opened this issue Feb 27, 2018 · 9 comments

Comments

@liamdawson
Copy link

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.

@mehcode
Copy link
Collaborator

mehcode commented Mar 12, 2018

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

@mehcode mehcode added this to the 0.10.0 milestone Oct 5, 2018
@tanujitghosh
Copy link

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

@liamdawson
Copy link
Author

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".

@tanujitghosh
Copy link

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

@omarabid
Copy link

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.

@mehcode mehcode removed this from the 0.10.0 milestone May 7, 2019
@blankhart
Copy link

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.

@theguy147
Copy link

Are there any updates on this? Are there recommendations on how to use config-rs together with clap?

@polarathene
Copy link
Collaborator

Are there any updates on this?

This project presently has no active maintainer AFAIK. I would be reluctant to adopt a crate for a new project when maintenance / development is halted.

I may eventually have time to assist with review and merging of contributions, but I'm not able to commit to a proper maintainer role here, nor do I have the ability to publish a new release I think even if I assist with PRs in future.

Despite the crates popularity, it's unclear when/if a new maintainer will arrive. You may want to look into other alternatives?

@epage epage added the A-source label Oct 23, 2024
@blueforesticarus
Copy link

blueforesticarus commented Nov 19, 2024

I rigged something up that supports jq syntax via the jaq crate

Rigged something up quick for my application.

I implement it two different ways. One uses the set_override builtin to this crate.
It can be be used like ./run -Xspotify.secret=blah (which auto inserts the quotes) or ./run -X'matrix.auth={password:"password"}'.

The second way pulls in jaq. It supports any transform jq does, which has slightly different syntax ./run -J'.spotify.secret="<blah>"'
There are ways to recursively merge records. ./run -J'.*{ <this whole object would be merged in> }'

I am able to do things like -J'del(.[].enabled?)' to disable all my modules.
I can do -J'.playlist|=new(.id="<id>")' to add things to a (possibly null) list, via a function (new) I added to the library.

This satisfies my desire to completely expose the config to the cli.

Now I'm wondering how this can be cleaned up, and whether there is a nice abstraction to be had with clap sub-commands.

  • fields can be left out of the config file, ie. forcing them to be provided at the cli.
    There might even be a good way to use the errors to prompt the user interactively if they don't
  • config parse Errors no longer point to the actual source, I will have to dig into the guts to fix it.

EDIT: link to cleaned up version https://github.com/blueforesticarus/goontunes2/blob/b73a4a9d90322aee223046893fddbc4bac73c719/src/config.rs

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

9 participants