serhii.net

In the middle of the desert you can say anything you want

19 Dec 2021

Python click getting default values from config file

Found a post1 about it.

But I like much more Click’s way to do this (Options — Click Documentation (8.0.x)):

@click.option(
    "--username", prompt=True,
    default=lambda: os.environ.get("USER", "")
)

Of course, os.get.environ can be replaced by python-decouple’s config().

Lastly, ini files support interpolation2 (%(whatever)s)! Final solution:

[settings]
EXPORT=../../exports
CATS_INPUT=%(EXPORT)s/cats.json
@click.option(
    "--input-file",
    "-i",
    type=click.Path(exists=True, path_type=Path),
    default=lambda: config("CATS_INPUT"),
)

Also TIL if I use quotes in the ini file, they’ll become part of the final filename.

Nel mezzo del deserto posso dire tutto quello che voglio.
comments powered by Disqus