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)):

    "--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:

    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.

  1. Knowledge Bits — Setting Default Option Values from Config Files with Click ↩︎

  2. configparser — Configuration file parser — Python 3.10.1 documentation ↩︎

Nel mezzo del deserto posso dire tutto quello che voglio.