You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I want to prioritize CLI parameters over parameters from a config file or the environment but only if the CLI parameters are explicitly set (e.g. not derived via the clap default value).
If I understand correctly, merging with clap parameters only works if every CLI parameter has a default value.
My general expectation for CLI tools is, that explicitly passed command line parameters overwrite everything else (e.g. parameters from env or config files). If I want this, i have to use the load_env_first function. But since for this to work, every clap parameter needs a default value, those default values overwrite everything from env and config files, if I place the serialized defaults last.
Is it possible to achieve the following behavior using figment, clap and configuration files/environment variables?
# default values
$ ./app
=> host = 0.0.0.0
# value from env (and NOT the clap default)
$ APP_HOST=127.0.0.1 ./app
=> host = 127.0.0.1
# value from cli
$ ./app --host 127.0.0.1
=> host = 127.0.0.1
# if both env and cli params are set, the CLI param should be prioritized
$ APP_HOST=127.0.0.1 ./app --host 127.0.0.2
=> host = 127.0.0.2
reacted with thumbs up emoji reacted with thumbs down emoji reacted with laugh emoji reacted with hooray emoji reacted with confused emoji reacted with heart emoji reacted with rocket emoji reacted with eyes emoji
-
I want to prioritize CLI parameters over parameters from a config file or the environment but only if the CLI parameters are explicitly set (e.g. not derived via the clap default value).
If I understand correctly, merging with
clap
parameters only works if every CLI parameter has a default value.I have the following config struct:
And 2 functions to parse the configuration, with a different order for the config sources:
My general expectation for CLI tools is, that explicitly passed command line parameters overwrite everything else (e.g. parameters from env or config files). If I want this, i have to use the
load_env_first
function. But since for this to work, every clap parameter needs a default value, those default values overwrite everything from env and config files, if I place the serialized defaults last.Is it possible to achieve the following behavior using figment, clap and configuration files/environment variables?
Beta Was this translation helpful? Give feedback.
All reactions