diff options
author | Serhiy Storchaka <storchaka@gmail.com> | 2024-11-11 16:29:28 (GMT) |
---|---|---|
committer | GitHub <noreply@github.com> | 2024-11-11 16:29:28 (GMT) |
commit | 25aee21aa84061b5d8e08247c8581da1459f37e8 (patch) | |
tree | f00ac796f4fde69212091e8c196c867e9b397303 /Doc/library | |
parent | 79805d228440814c0674ab5190ef17f235503d2e (diff) | |
download | cpython-25aee21aa84061b5d8e08247c8581da1459f37e8.zip cpython-25aee21aa84061b5d8e08247c8581da1459f37e8.tar.gz cpython-25aee21aa84061b5d8e08247c8581da1459f37e8.tar.bz2 |
gh-126374: Add support of options with optional arguments in the getopt module (GH-126375)
Diffstat (limited to 'Doc/library')
-rw-r--r-- | Doc/library/getopt.rst | 26 |
1 files changed, 23 insertions, 3 deletions
diff --git a/Doc/library/getopt.rst b/Doc/library/getopt.rst index 3ab44b9..def0ea3 100644 --- a/Doc/library/getopt.rst +++ b/Doc/library/getopt.rst @@ -38,7 +38,8 @@ exception: be parsed, without the leading reference to the running program. Typically, this means ``sys.argv[1:]``. *shortopts* is the string of option letters that the script wants to recognize, with options that require an argument followed by a - colon (``':'``; i.e., the same format that Unix :c:func:`!getopt` uses). + colon (``':'``) and options that accept an optional argument followed by + two colons (``'::'``); i.e., the same format that Unix :c:func:`!getopt` uses. .. note:: @@ -49,8 +50,10 @@ exception: *longopts*, if specified, must be a list of strings with the names of the long options which should be supported. The leading ``'--'`` characters should not be included in the option name. Long options which require an - argument should be followed by an equal sign (``'='``). Optional arguments - are not supported. To accept only long options, *shortopts* should be an + argument should be followed by an equal sign (``'='``). + Long options which accept an optional argument should be followed by + an equal sign and question mark (``'=?'``). + To accept only long options, *shortopts* should be an empty string. Long options on the command line can be recognized so long as they provide a prefix of the option name that matches exactly one of the accepted options. For example, if *longopts* is ``['foo', 'frob']``, the @@ -67,6 +70,9 @@ exception: options occur in the list in the same order in which they were found, thus allowing multiple occurrences. Long and short options may be mixed. + .. versionchanged:: 3.14 + Optional arguments are supported. + .. function:: gnu_getopt(args, shortopts, longopts=[]) @@ -124,6 +130,20 @@ Using long option names is equally easy: >>> args ['a1', 'a2'] +Optional arguments should be specified explicitly: + +.. doctest:: + + >>> s = '-Con -C --color=off --color a1 a2' + >>> args = s.split() + >>> args + ['-Con', '-C', '--color=off', '--color', 'a1', 'a2'] + >>> optlist, args = getopt.getopt(args, 'C::', ['color=?']) + >>> optlist + [('-C', 'on'), ('-C', ''), ('--color', 'off'), ('--color', '')] + >>> args + ['a1', 'a2'] + In a script, typical usage is something like this: .. testcode:: |