0

I'm having trouble understanding the meaning of semi-colons in shortops. I have such a longopt ["help", "runNum=","allplots=","sys=","redblue=","signalinj=","ranking="] and I assign values to parameters with

try:
    opts, args = getopt.getopt(sys.argv[1:],"h:", ["help","runNum=","allplots=","sys=","redblue=","signalinj=","ranking="])
except getopt.GetoptError:
    print("Bad option. ")
    sys.exit(2)

for o, a in opts:
    if o=="-h" or o=='--help':
        print("Ex: python3 app.py --runNum=55 --allplots=1 --sys=1 --ranking=0 --redblue=0 --signalinj=0")
    elif o=="--runNum":
        runNum = 'r'+str(a)
    elif o=='--allplots':
        _allplots=a
    elif o=='--sys':
        _sysflag=a
    elif o=='--ranking':
        _ranking=a
    elif o=='--redblue':
        _redblueflag=a
    elif o=='--signalinj':
        _signal_inj=a

When I run the app with python3 app.py without any shotopts, it works fine but running with - option returns "Bad option" error instead of "Ex: python3 app.y ..." and --help returns UnboundLocalError: local variable 'runNum' referenced before assignment error. So how can I make it print "Ex: python3 app.y ...".

I also tried "h:o" but no improvement. This semi colon usage is still a mystery to me. i appreciate it if someone can explain this and tell me what to put on shortopts.

mrq
  • 274
  • 1
  • 2
  • 14
  • The "runNum" error happens because you are missing a double quote on the second line. Note that your code block above is not colored correctly because of that. – Tim Roberts Mar 23 '22 at 23:32
  • `-h` doesn't work because you have `"h:"`. The "colon" means that the -h parameter must have an argument. Change that to `"h"`. – Tim Roberts Mar 23 '22 at 23:33
  • I'm sorry but the missing " is just a copy paste error. I have it in my original code. I did try with `"h"` as you said but this time I got the same `UnboundLocalError` – mrq Mar 24 '22 at 00:01

0 Answers0