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
Option new methods #163
Option new methods #163
Conversation
…with the option - added Option.IsSetDefault() to help distinguish cases where the option was really set or set with default values
Can you explain what the use cases for these are. |
Sure. For some programs that have a cli interface for which options can be saved into a file, you may want to have the order of preference for the options to be:
For instance:
./myprog --config=my.ini
So basically, I first use a Parser to get the options from cli, then a second Parser to get the ones from the config file, then I fuse the results by comparing if the Option is set in the file with Option.IsSet() or set on the cli with Option.IsSetDefault(), and if not, I do a Option.Field().Set(iniOption.Field()). |
Does this solve it: #120 ? |
Ah yes it does, although it is not straightforward. Here is a working example based on the one in #120:
The output:
|
Actually, I think it may be possible to have a better approach for this without having to expose any new method, but only having a new tag such as is-ini to identify an ini path. You could potentially have more than one.
The options would be set in order of priority: cli, ini file, default value. The steps would be:
Let me know what you think. I can provide another pull request with this implementation and associated tests, although it may take a couple of days as I am busy with other things. |
Why didn't you try the approach as shown in #120 (comment), it's pretty simple and straightforward? |
Because I want the cli options to always override my ini ones, no matter the order in which they appear. |
I would rather have an option on the ini parser that determines whether it will override already set options or not. |
Ok, something like this? This is straightforward to implement and simplifies my code too ;)
|
Any thought on the last commit? |
Sorry for taking so long, but the idea I think was not to have an additional tag, but an option on the actual |
Such as:
? |
Yes, I think |
…with the option - added Option.IsSetDefault() to help distinguish cases where the option was really set or set with default values
Ok, finally found some time to do this: the option ParseAsDefaults has been added to IniParser that override default values when set. |
isSet bool | ||
preventDefault bool | ||
tag multiTag | ||
isSet, isSetDefault bool |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't really like this, can you put isSetDefault
on a separate line?
…with the option - added Option.IsSetDefault() to help distinguish cases where the option was really set or set with default values
@@ -172,6 +177,11 @@ func (option *Option) IsSet() bool { | |||
return option.isSet | |||
} | |||
|
|||
// IsSetDefault returns true if option has been set with its default value. | |||
func (option *Option) IsSetDefault() bool { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Does this need to be public?
preventDefault bool | ||
|
||
defaultLiteral string | ||
} | ||
|
||
func (option *Option) Field() reflect.Value { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Does this need to be public?
Hopefully, this is helpful for everyone...