Disable abbreviation in argparse

前端 未结 4 2080
不思量自难忘°
不思量自难忘° 2020-12-18 22:39

argparse uses per default abbreviation in unambiguous cases.

I don\'t want abbreviation and I\'d like to disable it. But didn\'t find it in the documentation.

<
4条回答
  •  心在旅途
    2020-12-18 23:06

    No, apparently this is not possible. At least in Python 2.7.2.

    First, I took a look into the documentation - to no avail.

    Then I opened the Lib\argparse.py and looked through the source code. Omitting a lot of details, it seems that each argument is parsed by a regular expression like this (argparse:2152):

        # allow one or more arguments
        elif nargs == ONE_OR_MORE:
            nargs_pattern = '(-*A[A-]*)'
    

    This regex will successfully parse both '-' and '--', so we have no control over the short and long arguments. Other regexes use the -* construct too, so it does not depend on the type of the parameter (no sub-arguments, 1 sub-argument etc).

    Later in the code double dashes are converted to one dash (only for non-optional args), again, without any flags to control by user:

        # if this is an optional action, -- is not allowed
        if action.option_strings:
            nargs_pattern = nargs_pattern.replace('-*', '')
            nargs_pattern = nargs_pattern.replace('-', '')
    

提交回复
热议问题