gh-126390: Support for preserving order of options and nonoption arguments in gnu_getopt() by serhiy-storchaka · Pull Request #126393 · python/cpython
Expand Up
@@ -24,9 +24,6 @@
# TODO for gnu_getopt():
#
# - GNU getopt_long_only mechanism
# - allow the caller to specify ordering
# - RETURN_IN_ORDER option
# - GNU extension with '-' as first character of option string
# - an option string with a W followed by semicolon should
# treat "-W foo" as "--foo"
Expand Down Expand Up @@ -63,7 +60,7 @@ def getopt(args, shortopts, longopts = []): long options which should be supported. The leading '--' characters should not be included in the option name. Options which require an argument should be followed by an equal sign ('='). Options which acept an optional argument should be ('='). Options which accept an optional argument should be followed by an equal sign and question mark ('=?').
The return value consists of two elements: the first is a list of Expand Down Expand Up @@ -116,8 +113,13 @@ def gnu_getopt(args, shortopts, longopts = []): else: longopts = list(longopts)
return_in_order = False if shortopts.startswith('-'): shortopts = shortopts[1:] all_options_first = False return_in_order = True # Allow options after non-option arguments? if shortopts.startswith('+'): elif shortopts.startswith('+'): shortopts = shortopts[1:] all_options_first = True elif os.environ.get("POSIXLY_CORRECT"): Expand All @@ -131,8 +133,14 @@ def gnu_getopt(args, shortopts, longopts = []): break
if args[0][:2] == '--': if return_in_order and prog_args: opts.append((None, prog_args)) prog_args = [] opts, args = do_longs(opts, args[0][2:], longopts, args[1:]) elif args[0][:1] == '-' and args[0] != '-': if return_in_order and prog_args: opts.append((None, prog_args)) prog_args = [] opts, args = do_shorts(opts, args[0][1:], shortopts, args[1:]) else: if all_options_first: Expand Down
Expand Down Expand Up @@ -63,7 +60,7 @@ def getopt(args, shortopts, longopts = []): long options which should be supported. The leading '--' characters should not be included in the option name. Options which require an argument should be followed by an equal sign ('='). Options which acept an optional argument should be ('='). Options which accept an optional argument should be followed by an equal sign and question mark ('=?').
The return value consists of two elements: the first is a list of Expand Down Expand Up @@ -116,8 +113,13 @@ def gnu_getopt(args, shortopts, longopts = []): else: longopts = list(longopts)
return_in_order = False if shortopts.startswith('-'): shortopts = shortopts[1:] all_options_first = False return_in_order = True # Allow options after non-option arguments? if shortopts.startswith('+'): elif shortopts.startswith('+'): shortopts = shortopts[1:] all_options_first = True elif os.environ.get("POSIXLY_CORRECT"): Expand All @@ -131,8 +133,14 @@ def gnu_getopt(args, shortopts, longopts = []): break
if args[0][:2] == '--': if return_in_order and prog_args: opts.append((None, prog_args)) prog_args = [] opts, args = do_longs(opts, args[0][2:], longopts, args[1:]) elif args[0][:1] == '-' and args[0] != '-': if return_in_order and prog_args: opts.append((None, prog_args)) prog_args = [] opts, args = do_shorts(opts, args[0][1:], shortopts, args[1:]) else: if all_options_first: Expand Down