From cbb555ee1bf15479dab1089f7d9b1636b7b0ad76 Mon Sep 17 00:00:00 2001 From: Joern Hees Date: Tue, 14 Nov 2017 18:56:01 +0100 Subject: [PATCH 1/2] Doc: unify long args to use two "--" --- Doc/library/argparse.rst | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/Doc/library/argparse.rst b/Doc/library/argparse.rst index 53e670161dd5b3..22548c3a52c0f1 100644 --- a/Doc/library/argparse.rst +++ b/Doc/library/argparse.rst @@ -1461,15 +1461,15 @@ allows long options to be abbreviated to a prefix, if the abbreviation is unambiguous (the prefix matches a unique option):: >>> parser = argparse.ArgumentParser(prog='PROG') - >>> parser.add_argument('-bacon') - >>> parser.add_argument('-badger') - >>> parser.parse_args('-bac MMM'.split()) + >>> parser.add_argument('--bacon') + >>> parser.add_argument('--badger') + >>> parser.parse_args('--bac MMM'.split()) Namespace(bacon='MMM', badger=None) - >>> parser.parse_args('-bad WOOD'.split()) + >>> parser.parse_args('--bad WOOD'.split()) Namespace(bacon=None, badger='WOOD') - >>> parser.parse_args('-ba BA'.split()) - usage: PROG [-h] [-bacon BACON] [-badger BADGER] - PROG: error: ambiguous option: -ba could match -badger, -bacon + >>> parser.parse_args('--ba BA'.split()) + usage: PROG [-h] [--bacon BACON] [--badger BADGER] + PROG: error: ambiguous option: --ba could match --badger, --bacon An error is produced for arguments that could produce more than one options. This feature can be disabled by setting :ref:`allow_abbrev` to ``False``. From 7d3b8a867cca3b14d736f395043100db0c8f0b2d Mon Sep 17 00:00:00 2001 From: Joern Hees Date: Tue, 14 Nov 2017 18:59:05 +0100 Subject: [PATCH 2/2] bpo-32027: Doc: add note that argparse allow_abbrev=False deactivates flag combinations --- Doc/library/argparse.rst | 20 +++++++++++++++++--- 1 file changed, 17 insertions(+), 3 deletions(-) diff --git a/Doc/library/argparse.rst b/Doc/library/argparse.rst index 22548c3a52c0f1..f4e9189669c9cf 100644 --- a/Doc/library/argparse.rst +++ b/Doc/library/argparse.rst @@ -176,7 +176,7 @@ ArgumentParser objects * add_help_ - Add a ``-h/--help`` option to the parser (default: ``True``) - * allow_abbrev_ - Allows long options to be abbreviated if the + * allow_abbrev_ - Allows options to be combined or abbreviated if the abbreviation is unambiguous. (default: ``True``) .. versionchanged:: 3.5 @@ -542,9 +542,10 @@ allow_abbrev Normally, when you pass an argument list to the :meth:`~ArgumentParser.parse_args` method of an :class:`ArgumentParser`, -it :ref:`recognizes abbreviations ` of long options. +it recognizes flag combinations and :ref:`abbreviations ` of +long options. -This feature can be disabled by setting ``allow_abbrev`` to ``False``:: +These features can be disabled by setting ``allow_abbrev`` to ``False``:: >>> parser = argparse.ArgumentParser(prog='PROG', allow_abbrev=False) >>> parser.add_argument('--foobar', action='store_true') @@ -553,6 +554,15 @@ This feature can be disabled by setting ``allow_abbrev`` to ``False``:: usage: PROG [-h] [--foobar] [--foonley] PROG: error: unrecognized arguments: --foon +Note that this also deactivates the combination of simple flags:: + + >>> parser = argparse.ArgumentParser(prog='PROG', allow_abbrev=False) + >>> parser.add_argument('-a', action='store_true') + >>> parser.add_argument('-b', action='store_false') + >>> parser.parse_args(['-ab']) + usage: PROG [-h] [-a] [-b] + PROG: error: unrecognized arguments: -ab + .. versionadded:: 3.5 @@ -1474,6 +1484,10 @@ unambiguous (the prefix matches a unique option):: An error is produced for arguments that could produce more than one options. This feature can be disabled by setting :ref:`allow_abbrev` to ``False``. +.. note:: + Setting ``allow_abbrev=False`` will also :ref:`deactivate ` + the common feature of combining short flags with ``action='store_const'``. + .. _args: Beyond ``sys.argv``