You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
by default GNU getopt() permutes the contents of argv as it scans, so that eventually all the nonoptions are at the end. It supports two other scanning modes -- one compatible with Posix getopt() (stop at first nonoption argument), and other allows to return options and nonoption arguments in order.
getopt.gnu_getopt() returns two lists -- the list of option-and-value pairs and the list of nonoption arguments. Thus, the relative order of options and nonoption arguments is lost.
I propose to add support for the missed feature. If the first character of the option string is minus ('-'), non-option arguments that are followed by options will be added to the list of option-and-value pairs as a pair that has None as its first element and the list of non-option arguments as its second element. Non-option arguments that follow the last option will be returned as the second element of the gnu_getopt() result.
Returning each nonoption argument as a separate (None, value) tuple instead of (None, list). But if you need a list of consequent arguments, it will be less convenient to build it from separate argument. And if you need separate arguments, you can simply iterate the list.
Returning the last portion of nonoption arguments also as a (None, list) pair. This makes handling nonoption arguments followed and not followed by options in user code uniform. But then the second element of the gnu_getopt() result will always be an empty list. I am not sure about this.
Add the third function (gnu_getopt_iter()?) which is a generator function and emits (option, value) or (None, list) pairs.
Feature or enhancement
by default GNU
getopt()
permutes the contents of argv as it scans, so that eventually all the nonoptions are at the end. It supports two other scanning modes -- one compatible with Posixgetopt()
(stop at first nonoption argument), and other allows to return options and nonoption arguments in order.getopt.gnu_getopt()
returns two lists -- the list of option-and-value pairs and the list of nonoption arguments. Thus, the relative order of options and nonoption arguments is lost.I propose to add support for the missed feature. If the first character of the option string is minus (
'-'
), non-option arguments that are followed by options will be added to the list of option-and-value pairs as a pair that has None as its first element and the list of non-option arguments as its second element. Non-option arguments that follow the last option will be returned as the second element of thegnu_getopt()
result.For example:
I considered also alternative options:
(None, value)
tuple instead of(None, list)
. But if you need a list of consequent arguments, it will be less convenient to build it from separate argument. And if you need separate arguments, you can simply iterate the list.(None, list)
pair. This makes handling nonoption arguments followed and not followed by options in user code uniform. But then the second element of thegnu_getopt()
result will always be an empty list. I am not sure about this.gnu_getopt_iter()
?) which is a generator function and emits(option, value)
or(None, list)
pairs.Linked PRs
The text was updated successfully, but these errors were encountered: