Closed curio77 closed 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.
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. :-/
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
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
).
How do I access non-option command line arguments?
For example, running
./app -opt foo bar baz
won't complain aboutbar
andbaz
, but I also don't see any methods inmulticonfig
giving me access to these.flag.Args()
doesn't work (acts as ifflag
hasn't been used);os.Args
(expectedly) yields the entire command line.