Skip to content

Inferring dest from a single-dash long option #138697

@serhiy-storchaka

Description

@serhiy-storchaka

Bug report

In argparse, in general, short options are options that consist of a single character preceded by a dash (e.g. -f), and long options are either preceded by a double dash or are longer that a single character (e.g. --foo and -foo). Short option can be combined together and can have value immediately following an option, long options should be separate and have explicit separation from value.

But when dest is inferred from the option strings, single-dash long options are treated as short options. For example, in add_argument('-f', '--foo') dest is 'foo', but in add_argument('-f', '-foo') dest is 'f'.

This inconsistency should be fixed. But since this is a breaking change, it should not be backported.

I doubt it will break much user code (it did not broke existing tests), because it only affects the code that uses both short option and single-dash long option for the same argument, place the short option option first, and does not have explicit dest argument. Users that use short options and single-dash long options most likely use workarounds: either place the single-dash long option first or add an explicit dest argument.

Linked PRs

Metadata

Metadata

Labels

3.15new features, bugs and security fixesstdlibStandard Library Python modules in the Lib/ directorytype-bugAn unexpected behavior, bug, or error

Projects

Status

No status

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions