Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Getopt overwrites variables unexpectedly #68744

Closed
jak mannequin opened this issue Jul 3, 2015 · 2 comments
Closed

Getopt overwrites variables unexpectedly #68744

jak mannequin opened this issue Jul 3, 2015 · 2 comments
Labels
stdlib Python modules in the Lib dir type-bug An unexpected behavior, bug, or error

Comments

@jak
Copy link
Mannequin

jak mannequin commented Jul 3, 2015

BPO 24556
Nosy @bitdancer

Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.

Show more details

GitHub fields:

assignee = None
closed_at = <Date 2015-07-03.14:33:46.094>
created_at = <Date 2015-07-03.09:32:15.511>
labels = ['invalid', 'type-bug', 'library']
title = 'Getopt overwrites variables unexpectedly'
updated_at = <Date 2015-07-03.14:33:46.092>
user = 'https://bugs.python.org/Jak'

bugs.python.org fields:

activity = <Date 2015-07-03.14:33:46.092>
actor = 'r.david.murray'
assignee = 'none'
closed = True
closed_date = <Date 2015-07-03.14:33:46.094>
closer = 'r.david.murray'
components = ['Library (Lib)']
creation = <Date 2015-07-03.09:32:15.511>
creator = 'Jak'
dependencies = []
files = []
hgrepos = []
issue_num = 24556
keywords = []
message_count = 2.0
messages = ['246153', '246181']
nosy_count = 2.0
nosy_names = ['r.david.murray', 'Jak']
pr_nums = []
priority = 'normal'
resolution = 'not a bug'
stage = 'resolved'
status = 'closed'
superseder = None
type = 'behavior'
url = 'https://bugs.python.org/issue24556'
versions = ['Python 2.7', 'Python 3.4']

@jak
Copy link
Mannequin Author

jak mannequin commented Jul 3, 2015

The getopt library has, what I assume is, some unexpected behaviour when adding extra text to command line parameter that getopt expects as a flag.

Using input parameters a, b and c as an example below, where a and b both take values and c is a flag.

Example code:
options, remainders = getopt.getopt(sys.argv[1:], "a:b:c")

Normal output is given when you supply sensible values for a, b and c:

Input: -a value1 -b value2 -c
Output: [('-a', 'value1'), ('-b', 'value2'), ('-c', '')]

Unexpected output happens when you give extra text after the '-c' that begins with a letter matching that of a previous parameter:

Input -a value1 -b value2 -cbanana
Output: [('-a', 'value1'), ('-b', 'value2'), ('-c', ''), ('-b', 'anana')]

Looping through the output variables, as most example programs do, results in the value for '-b' being over-written.

@jak jak mannequin added stdlib Python modules in the Lib dir type-bug An unexpected behavior, bug, or error labels Jul 3, 2015
@bitdancer
Copy link
Member

This behavior is correct:

rdmurray@session:~>getopt a:b:c -a value1 -b value2 -cbanana
-a value1 -b value2 -c -b anana --

@ezio-melotti ezio-melotti transferred this issue from another repository Apr 10, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
stdlib Python modules in the Lib dir type-bug An unexpected behavior, bug, or error
Projects
None yet
Development

No branches or pull requests

1 participant