Posts: 62
Threads: 26
Joined: Feb 2023
Feb-03-2023, 01:31 PM
(This post was last modified: Feb-03-2023, 01:32 PM by Calab.)
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.
Posts: 4,787
Threads: 76
Joined: Jan 2018
Feb-03-2023, 03:00 PM
(This post was last modified: Feb-03-2023, 03:01 PM by Gribouillis.)
Getopt is very old-fashioned, why not use argparse, which is much more friendly and powerful?
Posts: 6,792
Threads: 20
Joined: Feb 2020
Missing colon betweem p and i in the getopt() arguments
Posts: 62
Threads: 26
Joined: Feb 2023
(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.
Posts: 62
Threads: 26
Joined: Feb 2023
(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.
Posts: 6,792
Threads: 20
Joined: Feb 2020
Feb-03-2023, 06:27 PM
(This post was last modified: Feb-03-2023, 06:27 PM by deanhystad.)
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')], [])
Posts: 62
Threads: 26
Joined: Feb 2023
(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.
I've switched to argparse anyhow. It is definitely more powerful.
Posts: 6,792
Threads: 20
Joined: Feb 2020
Most of the standard library documentation is pretty good. The documentation for getopt is not.
|