Python Forum
Why isn't getops handling all parameters?
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Why isn't getops handling all parameters?
#1
Question 
For some reason, getops is only seeing 9 parameter entries of a bash command line that has 12 parameter entries. I can't figure out why. None of the parameters have special characters and the code is very simple...

parameters = sys.argv[1:]
opts, args = getopt.getopt(parameters, "u:c:t:r:pi")

print(parameters)
print(opts)
This is the command I enter:
./myscript.py -c Param1 -t Param2 -r Param3 -u Param4 -p Param5 -i Param6

This is the output I see:
Output:
['-c', 'Param1', '-t', 'Param2', '-r', 'Param3', '-u', 'Param4', '-p', 'Param5', '-i', 'Param6'] [('-c', 'Param1'), ('-t', 'Param2'), ('-r', 'Param3'), ('-u', 'Param4'), ('-p', '')]
What I expect to see:
Output:
['-c', 'Param1', '-t', 'Param2', '-r', 'Param3', '-u', 'Param4', '-p', 'Param5', '-i', 'Param6'] [('-c', 'Param1'), ('-t', 'Param2'), ('-r', 'Param3'), ('-u', 'Param4'), ('-p', 'Param5'), ('-i', 'Param6')]
getops is seeing the -p directive, but not the value it is being passed. It completely ignores the -i directive.

I'm using Python v 3.7.11 on a RHEL 7.9 system.
Reply
#2
Getopt is very old-fashioned, why not use argparse, which is much more friendly and powerful?
Calab likes this post
Reply
#3
Missing colon betweem p and i in the getopt() arguments
Reply
#4
(Feb-03-2023, 03:04 PM)deanhystad Wrote: Missing colon betweem p and i in the getopt() arguments
P and I are optional directives, not required every time.
Reply
#5
(Feb-03-2023, 03:00 PM)Gribouillis Wrote: Getopt is very old-fashioned, why not use argparse, which is much more friendly and powerful?
Because I hadn't heard about argparse. :)

Will take a look at argparse.

Still doesn't explain the issue though.
Reply
#6
Quote:P and I are optional directives, not required every time.
Colon indicates the option is followed by a value. It has nothing to do with the option being required or optional. Your option string "u:c:t:r:pi" tells getopt that u, c, t and r are followed by an option value, p and i are not followed by a value. When parsing your arguments, getopt sees the value after p as a potential option i, not a value for p.
import getopt
import sys

args = sys.argv[1:]
print("args", args)
print("a:b:c", getopt.getopt(sys.argv[1:], "a:b:c"))
print("a:b:c:", getopt.getopt(sys.argv[1:], "a:b:c:"))
Output:
args ['-a', 'A', '-b', 'B', '-c', 'C'] a:b:c ([('-a', 'A'), ('-b', 'B'), ('-c', '')], ['C']) a:b:c: ([('-a', 'A'), ('-b', 'B'), ('-c', 'C')], [])
Calab likes this post
Reply
#7
(Feb-03-2023, 06:27 PM)deanhystad Wrote:
Quote:P and I are optional directives, not required every time.
Colon indicates the option is followed by a value. It has nothing to do with the option being required or optional. Your option string "u:c:t:r:pi" tells getopt that u, c, t and r are followed by an option value, p and i are not followed by a value. When parsing your arguments, getopt sees the value after p as a potential option i, not a value for p.
import getopt
import sys

args = sys.argv[1:]
print("args", args)
print("a:b:c", getopt.getopt(sys.argv[1:], "a:b:c"))
print("a:b:c:", getopt.getopt(sys.argv[1:], "a:b:c:"))
Output:
args ['-a', 'A', '-b', 'B', '-c', 'C'] a:b:c ([('-a', 'A'), ('-b', 'B'), ('-c', '')], ['C']) a:b:c: ([('-a', 'A'), ('-b', 'B'), ('-c', 'C')], [])

Ohhhhhhh... Now that makes perfect sense. I misread the documentation and was thinking I was crazy. Doh

I've switched to argparse anyhow. It is definitely more powerful.
Reply
#8
Most of the standard library documentation is pretty good. The documentation for getopt is not.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
Star python exception handling handling .... with traceback mg24 3 1,216 Nov-09-2022, 07:29 PM
Last Post: Gribouillis

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020