Problem passing arguments to a Python script


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Problem passing arguments to a Python script
# 1  
Old 01-18-2011
Problem passing arguments to a Python script

I have part of the script below and I tried calling the script using

Code:
./tsimplex.py --fstmod=chris.cmod --nxz=8x6 --varp=0.25

but am getting the error
Code:
option --fstmod must not have an argument

Any idea on how to fix this would be highly appreciated


Code:
#! /usr/bin/python

import getopt
import popen2
import sys
import types
import os

__author__="user"
__date__ ="$20-Nov-2010 17:43:52$"

def print_info():
    print ""
    print "-------------------------------------------------------------"
    print ""
    print "SCRIPT: ../Scripts/tsimplex.csh"
    print ""
    print "VERSION: %s"%(Version)
    print ""
    print "CREATED BY: CHRISTOPHER DIMECH"
    print ""
    print "PURPOSE: Runs Tommy tsimplex"
    print ""
    print "USAGE:"
    print ""
    print "   $s"%(print_usage(1))
    print ""
    print "WHERE:"
    print ""
    print "   (O)  fout.cmod       Best sound speed model found"
    print "   (O)  fout.back       Backup file for "
    print "   (O)  fout.expl       Exploration file"
    print "   (O)  fout.log        Log file with misfit values"
    print "   (I)  tpath           Path to TOMMY tsimplex"
    print "   (I)  fbase.base      Model dimensions file"
    print "   (I)  fdata.dat       Travel times file"
    print "   (I)  fstmod.cmod     Starting Sound Speed Model"
    print "   (I)  frestore.back   Restore population from a backup file"
    print "   (I)  ftag            Appends a tag to the output file names"
    print "   (I)  nxz             Model size to invert, e.g. 3x2"
    print "   (I)  varp            Sound speed variation allowed"
    print "   (I)  sigma0          "
    print "   (I)  maxiter         Number of iterations to perform"
    print "   (I)  vrb             Verbosity level for log file"
    print "                        vrb options = none, low, medium, high"
    print "   (I) -tpf             Create a tommy parameter file"
    print "   (I) -tee             "
    print "   (I) -usage           Details of the arguments"
    print "   (I) -help            Details on using this script"
    print ""
    print "EXAMPLES:"
    print ""
    print "-------------------------------------------------------------"
    print ""

def print_usage(asstring=0):
    arg1="../Scripts/tsimplex.csh"
    arg2="[-tpath=] [-base=fbase.base] [-data=fdata.dat]"
    arg3="[-fstmod=fstmod.vmod] [-restore=frestore.back -ftag=]"
    arg4="-nxz= -varp= -sigma= [-vrb=vrb] -tpf [-tee] [-usage] [-help]"
#    usage="$arg1 $arg2 $arg3 $arg4"
    usage="%s %s %s %s"%(arg1,arg2,arg3,arg4)

    if asstring == 0:
        print usage
    else:
        return usage

if __name__ == "__main__":

    x = "ls -lrt"
    os.system(x)

    Version="V03"
    tpath = None
    fbase = None
    fdata = None
    fstmod = None
    frestore = None
    ftag = None
    fsr = None
    frc = None
    frestoretag = None
    nxz = None
    varp = None
    sigma = None
    maxiter = None
    dtau = None
    mdacc = None
    mindist = None
    maxitertp = None
    vrb = None
    tpf = None
    tee = None
    usage = None
    eg = None
    help = None
    info = None

    long_opts = ['tpath',
    'fbase',
    'fdata',
    'fstmod',
    'frestore',
    'frestoretag',
    'nxz',
    'varp',
    'sigma',
    'maxiter',
    'dtau',
    'mdacc',
    'mindist',
    'maxitertp',
    'vrb',
    'tpf',
    'tee',
    'usage',
    'eg',
    'help',
    'info']

    try:
        optlist, args = getopt.getopt(sys.argv[1:], "", long_opts)

    except getopt.GetoptError, err:
        # print help information and exit:
        print str(err) # will print something like "option -a not recognized"
        print_usage()
        sys.exit(2)

    for opt, val in optlist:
        if opt == "--help":
            print_info()
            sys.exit()
        elif opt == "--eg":
            print_usage()
            sys.exit()
        elif opt == "--info":
            print_info()
            sys.exit()
        elif opt == "--usage":
            print_usage()
            sys.exit()
        elif opt == "--tpath":
            tpath = val
            print " -tpath = tpath"
            print " -tpath = ",val
            print ""
        elif opt == "--fbase":
            fbase = val.split('.base')[0]
            print " -fbase = fbase.base"
            print " -fbase = ",val
            print ""
        elif opt == "--fdata":
            fdata = val.split('.dat')[0]
            print " -fdata = fdata.dat"
            print " -fdata = ",val
            print ""
        elif opt == "--fstmod":
            fstmod = val.split('.cmod')[0]
            print " -fstmod = fstmod.cmod"
            print " -fstmod = ",val
            print ""
        elif opt == "--frestore":
            frestore = val
            print " -frestore = frestore.back"
            print " -frestore = ",val
            print ""
        elif opt == "--ftag":
            ftag = val
            print " -ftag = ftag"
            print " -ftag = ",val
            print ""
        elif opt == "--nxz":
            nxp = val.split('x')[0]
            nzp = val.split('x')[1]
            print " -nxz = nxz"
            print " -nxz = ",val
            print ""
        elif opt == "--varp":
             varpint = val.split('.')[0]
             varpfrc = val.split('.')[1]
             print " -varp = varp"
             print " -varp = ",val
             print ""
        elif opt == "--sigma":
            sigma = val
            print " -sigma = sigma"
            print " -sigma = ",val
            print ""
        elif opt == "--maxiter":
            maxiter = val
            print " -maxiter = maxiter"
            print " -maxiter = ",val
            print ""
        elif opt == "--dtau":
            dtau = val
            print " -dtau = dtau"
            print " -dtau = ",val
            print ""
        elif opt == "--mdacc":
            mdacc = val
            print " -mdacc = mdacc"
            print " -mdacc = ",val
            print ""
        elif opt == "--mindist":
            mindist = val
            print " -mindist = mindist"
            print " -mindist = ",val
            print ""
        elif opt == "--maxitertp":
            maxitertp = val
            print " -maxitertp = maxitertp"
            print " -maxitertp = ",val
            print ""
        elif opt == "--vrb":
            vrb = val
            print " -vrb = vrb"
            print " -vrb = ",val
            print ""
        elif opt == "--tpf":
            tpf = val
            print " -tpf"
            print ""
        elif opt == "--tee":
            tee = val
            print " -tee (Create tommy parameter file)"
            print ""
        else:
            print ""
            print "ERROR: Unrecognised argument,",opt
            print ""
            sys.exit(-1)

    # -------------------------------------------------------------------
    # USER HAS NOT ASKED FOR INFORMATION ABOUT THE USE OF THIS SCRIPT
    # 1. Exit script if argument not recognized
    # 2. Set appropriate defaults if options not set
    # -------------------------------------------------------------------


Last edited by kristinu; 01-18-2011 at 06:19 PM..
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

C shell script passing arguments problem.

I found something insteresting when I tested passing arguments into my scripts. My scripts is as below. % cat passarg.env #!/bin/csh echo "passarg: argv = $argv argv = $argv" passarg1.env $* % cat passarg1.env #!/bin/csh echo "passarg1: argv = $argv argvp=$argv" set str = "test... (5 Replies)
Discussion started by: bestard
5 Replies

2. Shell Programming and Scripting

Passing arguments to php script

i want to be able to pass arguments to a php script if it is being piped: cat myphpscript.php | php - $1 $2 $3 blah blah This usually works for other script languages...i.e. ruby: cat myrubyscript.rb | ruby - $1 $2 $3 blah blah so my question is, how can i pass arguments to my php... (1 Reply)
Discussion started by: SkySmart
1 Replies

3. Shell Programming and Scripting

Passing arguments while running the script

Hi, I have a requirement for creating a MQ (queue) where the inputs has to be passed as arguments. Running the script as below ./hi.sh "Servername" "QueueManagername" "QueuecreationCommand" cat hi.sh echo "Welcome to $1" runmqsc $2 < $3 But the queue creation command is... (9 Replies)
Discussion started by: Anusha M
9 Replies

4. Shell Programming and Scripting

Passing arguments to a bash script

Hi, I wanted to pass an argument to a bash script. So that the argument is used inside the awk command inside the bash script. I know the noraml way of passing argument to a bash script as below : sh myScript.sh abc Inside the bash script i can use like this myArg1=$1 wc $myArg But... (8 Replies)
Discussion started by: shree11
8 Replies

5. Shell Programming and Scripting

Passing arguments to a perl script

Hi I need to pass comma seperated arguments to a perl script? It is like: Exect.pl -d GUI1,GUI2,GUI3 and I need to store these argsGUI1,GUI2,GUI3 in an array. can anyone suggest how to do that: (1 Reply)
Discussion started by: rkrish
1 Replies

6. Shell Programming and Scripting

Passing arguments to python

How can I pass arguments to a python script??? (3 Replies)
Discussion started by: kristinu
3 Replies

7. Shell Programming and Scripting

passing arguments to sql script

Hi Gurus, i have one requirement in unix script, i have a file called abc.txt in that few lines are there with the empid, i need to read each line and pass to .sql script. ex: abc.txt 2345 2346 1243 1234 i need to pass these arguments to .sql script rom unix ex: select * from... (1 Reply)
Discussion started by: Devendar
1 Replies

8. Shell Programming and Scripting

problem passing arguments to script

Hi, I am writing a script, which is invoked from other system using ssh. I have problems reading the arguments passing to the script. If the argument has a space in it (ex "rev 2.00"), the script considers "rev" as 1 argument and "2.00" as another. Instead i want "rev 2.00" to be considered... (5 Replies)
Discussion started by: cjjoy
5 Replies

9. UNIX for Advanced & Expert Users

Passing blank arguments to a script

All, I have a cron job script that receives several command line arguments. At some point if there are validation problems and the job cannot be run, it duplicates the entire command line into a temporary text file which is later executed as a script. Unfortunately when I pass the list of received... (7 Replies)
Discussion started by: rm-r
7 Replies

10. Shell Programming and Scripting

Passing arguments to a script

I've written a script (bgrep) for a more advanced grep command (& attached a cut down version below). I'm trying allow all grep options to be used, or in any combination. The script works fine if I type say bgrep -i -files product it will return a non-case sensitive list of matches for... (3 Replies)
Discussion started by: Kevin Pryke
3 Replies
Login or Register to Ask a Question