Jaymon / captain

command line python scripts for humans
MIT License
13 stars 1 forks source link

environment aware arg decorator #77

Open Jaymon opened 1 year ago

Jaymon commented 1 year ago

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

Jaymon commented 4 months ago

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:

  1. passed in via CLI
  2. environment using environ_key or environ_name param
  3. default value if present
  4. prompt if value is required and prompt is True
Jaymon commented 1 month ago

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.