Search code examples
pythonclickpython-click

How to maintain case-Sensitive key in Python Click options


I am using Python Click to generate a json file. However, I need to maintain the case-sensitivity of keys. Python Click converts all keys into lower case implicitly and hence my generated json has lower case keys, not the one I like.

Example code is below

@click.option('--tempId', default=None, help='tempId')

In the above example, tempId is converted into tempid. Is there way to handle this? I am aware that values can be converted to lower or upper, but my requirement is to main the Key case as such.


Solution

  • This seems to work for me:

    @cli.command('test')
    @click.option('--tempId', '--tempId', 'tempId', default=None, help='tempId')
    def test(tempId):
        print(tempId)
    

    where if I invoke it with --tempId it finds the key, but if I invoke it with --tempid it doesn't.