koding / multiconfig

Load configuration from multiple sources in Go
http://godoc.org/github.com/koding/multiconfig
MIT License
454 stars 64 forks source link

Accessing non-option arguments #62

Closed curio77 closed 7 years ago

curio77 commented 7 years ago

How do I access non-option command line arguments?

For example, running ./app -opt foo bar baz won't complain about bar and baz, but I also don't see any methods in multiconfig giving me access to these. flag.Args() doesn't work (acts as if flag hasn't been used); os.Args (expectedly) yields the entire command line.

cihangir commented 7 years ago

multiconfig does not provide a way to get the params that are not defined. A hack-y way I could suggest is to pass the rest as a string and apply your logic on the value.

curio77 commented 7 years ago

Care to elaborate why this is so? From the point of view of application development, I consider it wrong to have the application run with additional arguments it simply ignores. At the very least, this may surprise the user (and so goes against the principle of least surprise concerning the CLI); what's worse, it also prevents the developer from cleanly accessing non-option arguments in cases where these are to be considered. Taken together, these are show stoppers in my book. :-/

cihangir commented 7 years ago

Thanks for the question.

The opinion that multiconfig has is, it helps you configure your app in multiple ways while working in harmony.

Lets assume you are using multiloader, how we can pass the additional arguments to the given Struct that we have read from Env vars? Should we read everything that is available to the app in the env vars? It has its own problems. If we switch to tag reader, how we can pass these optional arguments in the tags? Same issues apply to file reader and validators as well. So instead doing one-off magics for each of the Loaders we kept things as simple as possible.

If you still want to use multiconfig 1) you can embed a new struct into your config struct for your optional params 2) you can define the field as array of strings and operate on that array 3) you can pass it as whole string

curio77 commented 7 years ago

Thanks for elaborating.

However, I'm not sure I get just how you mean I should add a field to the config struct and have the non-option CLI arguments (bar, baz in the opening post's CLI call) assigned to it? For example, adding a string-slice-type field expects that to be filled explicitly, how would bar and baz end up in there? To make myself clear, those are not intended as additional values for the -opt option (let's assume that to be of type string, swallowing (only) the subsequent foo).