-
-
Notifications
You must be signed in to change notification settings - Fork 1.1k
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
Allow configuration from config files as well #251
Comments
Interesting problem... At least for now, I'm going to suggest |
What two lists? I don't understand what you're suggesting. I would like to be able to allow config files with a limited set of valid configuration options. But... how do I provide those options to |
It would appear that you responded to an older message of mine, that I had deleted. Thoughts for my updated message? |
I don't entirely understand your new suggestion. Let's say I have thirty configuration options, with various let app = App::new("myapp")
.arg(Arg::with_name("foo")
.short("f")
.long("foo")
.help("Sets a custom foo")
.takes_value(true))
.arg(Arg::with_name("bar")
.help("Sets baz")
.required(true)
.index(1))
.arg(Arg::with_name("baz")
.short("b")
.multiple(true)
.help("Sets the bazness"))
[...] So I can create the app that way. Now, how do I get a list of options to pass to my TOML validator? I could have made it ahead of time, but that's cumbersome if I want to use features like |
I see two possible actions for your question, so bare with me if one of them is way off track! It may also help if you show us an example of what a Pre-Specifying Argument ValuesMeaning, your application accepts To use valid arguments you shouldn't need to pass any First, you could pre-check for your The next option is just to use default values using Finally, you could also re-parse a simulated arguments list if there are errors on parsing. But doing this would require all the potentially default arg values to be required (i.e. in order to cause an error when omitted). If you're looking for a way to programmatically change Getting A List of Valid Arguments from the App structIf you're trying to create valid arguments from these TOML files, your options are a little more limited. In those cases I would almost exclusively recommend parsing those files FIRST to create an If there is overlap, you almost certainly want it to error or panic, unless you truly want users to be able to change arguments at runtime...which seems dangerous! Adding arguments seems safe, but changing existing is ones is something I'd think hard about 😜 Having said that, I have it on the back burner to add a method to get a The other option is to add some way to get a Because Hopefully this helped a little. If not, please let us know! Thanks for taking the time to file this issue! 😄 |
|
Good point! I went through a few iterations of that comment and didn't On Fri, Sep 11, 2015, 11:21 PM James McGlashan [email protected]
|
Thanks for the thoughtful and thorough response! I was thinking of option 1: the final user of the application creates a Your suggestions on how to merge TOML config and For example, let's imagine we have this app: let app = App::new("myapp")
.arg(Arg::with_name("foo")
.short("f")
.long("foo")
.help("Sets a custom foo")
.takes_value(true))
.arg(Arg::with_name("bar")
.help("Sets baz")
.required(true)
.index(1))
.arg(Arg::with_name("baz")
.short("b")
.multiple(true)
.help("Sets the bazness")); Then the user might have a config foo="some string"
bar=3 Your plan would work fine. However, if they had this file: fooo="some string"
bar=3 I can't see a good way to inform the user that option I could build it up at the same time as the let valid_names = vec!();
let mut app = clap::App::new("foobar");
let new_opt = |s: &str| {
valid_names.append(s);
Arg::with_name("foo")
}
app = app
.arg(new_opt("foo")
.short("f")
.long("foo")
.help("Sets a custom foo")
.takes_value(true))
.arg(new_opt("bar")
.help("Sets baz")
.required(true)
.index(1))
.arg(new_opt("baz")
.short("b")
.multiple(true)
.help("Sets the bazness")); That's... actually not too bad; its still a bit awkward, though, and while it works for a single instance, it seems to me like it would be hard to extend to a library for use in many apps that handles configuration files as carefully and thoroughly as If I wanted to write such a library, I can't really see how to do it effectively. I can't extend Do you see what I mean? Right now, What are your thoughts? |
Your suggestion of As for value types - #146 already covers this issue. |
Having support for dot files out of the box would be wonderful (maybe as a 'feature'?). This could help standardise the way that rust cli apps are configured which seems like a good thing for the community. I know this is an old ticket, clap has come on so much in this time. Is clap able to do configs out of the box for 'free'? |
I ended up here looking for a rust port of https://github.com/bw2/ConfigArgParse |
@kbknapp you had asked for an example of such usage. I can think of two:
both allow all config file options to be stuffed in a file provided with |
@dankamongmen This issue has been merged into #748 since that one has more of a constructive discussion going. Perhaps you should go there. |
I agree with @malarinv — I ended up here essentially trying to find a Rust equivalent of ConfigArgParse package for Python. I've used ConfigArgParse in previous Python projects, and its feature set has been perfect for my needs in those projects. In ConfigArgParse, I really like the |
For example, imagine you have a program
foobar
that has many configuration options. You want them to be available on the command-line, but you also want to allow users to write a.foobarrc
(perhaps in TOML format) so that they can pre-specify their choices. Or maybe there might be multiple configuration files, one in the home directory ($HOME/.foobarrc
), one in the current directory (./.foobarrc
), and maybe even one specified on the command-line.If this is outside the scope of this library, perhaps adding more ways to extract information from an
App
(such asget_names(&self) -> [&str]
, which would list all the names of the arguments defined) would allow someone else to build this.I ask because I am building an application that could use something like this - ParView - and I can't find a good way to do this without having to write every config option in multiple places.
The text was updated successfully, but these errors were encountered: