omni-us / jsonargparse

Implement minimal boilerplate CLIs derived from type hints and parse from command line, config files and environment variables
https://jsonargparse.readthedocs.io
MIT License
302 stars 41 forks source link

Question. How to use switches together with environment variables? #497

Closed ilejn closed 2 months ago

ilejn commented 2 months ago

Hello, I have simple command line flags, that perfectly work with action="store_true". I would like to have it controlled via environment as well, but it does not work well.

Ideally I want to specify myscript --debug or DEBUG=true myscript and it works indeed, unless I try DEBUG=false myscript. The latest one is converted to True as well.

The question is what is the best way to achieve the goal.

Switching to type=bool works, but not convenient as for commandline. Attempt to have two parameters with same dest is not successful (obviously).

ilejn commented 2 months ago

My bad, the attempt to have two parameters with same dest IS successful. I have

parser.add_argument(
    "--silent",
    action="store_true",
    dest="silent_flag",
    default=False,
    help="no log"
)
parser.add_argument(
    "--silentflag",
    "--silent-flag",
    dest="silent_flag",
    type=bool,
    default=False,
    help="no log"
)