environment aware arg decorator
Jaymon opened this issue · 2 comments
It would be cool to make it easier to set a value either through the command line or through the environment, so something like:
# foo.py
class Default(Command):
@envarg("--foo", dest="foo", envsrc="FOO")
def handle(self, foo):
self.output.out(foo)
Could work like this:
$ python foo.py --foo=1
or like this:
$ FOO=2 python foo.py
and it would do all the requirement checking and things like that, so:
$ python foo.py
would fail because --foo
wasn't passed in.
I might not even need something like @envarg
, it might be good enough to just pass in envsrc
into the @arg
decorator
I think I could also add a prompt
param that if set to to True would prompt for the value also, so the order of operation would be:
- passed in via CLI
- environment using
environ_key
orenviron_name
param - default value if present
- prompt if value is required and
prompt
is True
Thinking about this a little more, for environment variables:
@arg("--foo", "-f", "$FOO")
Which basically says you can get the argument from the --foo
or -f
flags or the FOO
environment variable.
And for the prompt, I think maybe something like this?
@arg("--foo", missing=prompt)
Where the missing
argument can take a callback or something. Likewise, maybe:
@arg("--foo", action="prompt")
Would say that the foo
value should prompt for a value? Or maybe the action should be prompt_if_missing
or something like that.