0

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.

1
  • Please help.. your help is appreciated. Commented Mar 12, 2015 at 6:57

2 Answers 2

3

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.

Sign up to request clarification or add additional context in comments.

Comments

0

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)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.