[Python] Create variable key/value pairs with argparse (python)

Discussão em 'Python' iniciado por Stack, Agosto 29, 2025.

  1. Stack

    Stack Membro Participativo

    I'm using argparse module to set my command line options. I'm also using a dict as a config in my application. Simple key/value store.

    What I'm looking for is a possibility to override JSON options using command line arguments, without defining all possible arguments in advance. Something like --conf-key-1 value1 --conf-key-2 value2, which would create a dict {'key_1': 'value1','key_2': 'value2'} ('-' in the argument is replaced by '_' in the dict). Then I can combine this dict with my JSON config (dict).

    So basically I would like to define --conf-* as an argument, where * can be any key and what comes after is the value.

    I did find configargparse module, but as far as I can see I start with a dict I already use.

    Any ideas how I could approach this?

    Continue reading...

Compartilhe esta Página