Vishal Mahajan
Vishal Mahajan

Reputation: 84

Python - output not coming after parsing command line options

Below is my code, where I am not getting output... after running script with inputfile and output file...

def parseCommandLine(argv=[]):
    inputfile = ''
    outputfile = ''
    FCSNAME = ''

    try:
        opts, args = getopt.getopt(
            argv,
            "hiop", 
            [help,"ifile=","ofile=","pcsfile="])
    except getopt.GetoptError,msg:
        printUsage()
        print "-E-Badly formed command line vishal!"
        print "  ",msg
        sys.exit(1)

    #Processing command line arguments
    for opt, arg in opts:
        opt= opt.lower()

        # Help
        if opt in ("-h", "--help"):
            printUsage()
            sys.exit()
        elif opt in ("-i", "--ifile"):
            inputfile = arg
        elif opt in ("-o", "--ofile"):
            outputfile = arg
        elif opt in ("-p", "--pcsname"):
            PCSNAME = arg
        if opt in ("-v"):
            VERBOSE = 1    
    print 'Input file is "', inputfile
    print 'Output file is "', outputfile
    print 'PCS NAME is "', FCSNAME
            # Verbose


    return 0

Output: ./aaa_scr -i list -o vishal

Input file is " Output file is " FCS NAME is "

No output is coming.. please help.

Upvotes: 0

Views: 617

Answers (2)

Hussain
Hussain

Reputation: 5177

Exclude the 0th element from sys.argv. i.e. the program name.

import getopt
import sys
try:
    opts, args = getopt.getopt(
        sys.argv[1:],
        "i:o:p:",
        ["ifile=","ofile=","pcsfile="])
except getopt.GetoptError,msg:
    print "error : %s" % msg

inputfile, outputfile, FCSNAME = None, None, None
for opt, arg in opts:
    print opt, arg
    if opt in ("-i", "--ifile"):
        inputfile = arg
    elif opt in ("-o", "--ofile"):
        outputfile = arg
    elif opt in ("-p", "--pcsname"):
        FCSNAME = arg

print "inputfile %s" % inputfile
print "outputfile %s" % outputfile
print "FCSNAME %s" % FCSNAME

Also you have options that require an argument, so you need to handle those using : (colon)

I hope this helps.

Upvotes: 3

Vinod Sharma
Vinod Sharma

Reputation: 883

0th element of sys.argv list is the name of program which getopt don't like. So just remove it and then pass argv it to the getopt.

import sys 
import getopt

def printUsage():
      print "Usage"

def parseCommandLine(argv=[]):
    argv = argv[1:]
    inputfile = ''
    outputfile = ''
    PCSNAME = ''

    try:
          opts, args = getopt.getopt(argv, 'hi:o:p:')
    except getopt.GetoptError,msg:
        printUsage()
        print "-E-Badly formed command line vishal!"
        print "  ",msg
        sys.exit(1)

    #Processing command line arguments
    #print opts
    for opt, arg in opts:
        opt = opt.lower()

        # Help
        if opt in ("-h", "--help"):
            printUsage()
            sys.exit()
        elif opt in ("-i", "--ifile"):
            inputfile = arg 
        elif opt in ("-o", "--ofile"):
            outputfile = arg 
        elif opt in ("-p", "--pcsname"):
            PCSNAME = arg 
        if opt in ("-v"):
            VERBOSE = 1 
    print 'Input file is "', inputfile
    print 'Output file is "', outputfile
    print 'PCS NAME is "', PCSNAME

parseCommandLine(sys.argv)

Upvotes: 0

Related Questions