pallets / click

Python composable command line interface toolkit
https://click.palletsprojects.com
BSD 3-Clause "New" or "Revised" License
15.62k stars 1.4k forks source link

Click does not detect duplicate option flags #2748

Closed apmorton closed 3 months ago

apmorton commented 3 months ago

It is possible to inadvertently have multiple options that have overlapping flags. Click doesn't detect or warn about this scenario, and it becomes impossible to provide a value for the first option.

import click

@click.command()
@click.option('-a')
@click.option('-a', '--long-a')
def main(a: str, long_a: str):
    print(a)
    print(long_a)

if __name__ == '__main__':
    main()
$ rpy script.py -a 1
None
1

$ rpy script.py --long-a 2 -a 1
None
1

$ rpy script.py -a 1 --long-a 2
None
2

Environment:

davidism commented 3 months ago

It's not possible to detect this without expensive checks that would slow down command execution. Given that this wouldn't actually happen with correctly constructed commands, that would be a needless check in most cases. Perhaps a limiting or testing tool could detect this instead.

apmorton commented 3 months ago

I'm not familiar with the click internals, so maybe I'm missing something here, but I don't see why this check would be expensive?

This feels like something that boils down to keeping a set of strings somewhere and checking for overlap when adding new options?