Hi,

I got this code snippet with the following usage:

def main():
    # sample usage
    manager = LoadManager()
    manager.msg = ('www.example.com', '/')
    manager.start(threads=5, interval=2, rampup=2)


if __name__ == '__main__':
    main()

Works fine,but i would like to supply the following arguments through command line:
msg
threads
interval
rampup
Plz could anyone tell me how to accomplish this

i tried:

def main():
    
    # default parameters
threads = 1
rampup = 0
interval = 0
msg='www.example.com'
# parse command line arguments
opt, args = optionparse.parse(__doc__)

if not opt and not args:
    print "sorry"
    optionparse.exit()
try:
    if opt.threads:
        threads = int(opt.threads)
    if opt.rampup:
        rampup = int(opt.rampup)
    if opt.interval:
        interval =  int(opt.interval)
    if opt.duration:
        msg = str(opt.msg)
    
except:
   print 'Invalid Argument'
   sys.exit(1)


    manager = LoadManager()
    manager.msg = ('argv[1]', '/')
    manager.start(argv[2],argv[3], argv[4])


if __name__ == '__main__':
    main()

This doesnt work...It gives me the error msg cant find module optionparse.
i included import optionparse,but still gives me the same error....Plz advise on the rest of the code as well,i'm unsure whether this is methos is correct for passing the arguments...

Cheers
David

See the standard modules getopt and optparse http://docs.python.org/lib/module-getopt.html and http://docs.python.org/lib/module-optparse.html

Stop posting useless crap...


ub007, if you want to use command arguments use this example:

import sys
for argument in sys.argv:
    print argument

print "Number of arguments:", str(len(sys.argv))

x=0
while x < len(sys.argv):
    print "sys.argv[" + str(x) + "] =", sys.argv[x]
    x+=1

I ran:

C:\Documents and Settings\Administrator\Desktop>thel.py hi super wow :O hax

Output:

C:\Documents and Settings\Administrator\Desktop\thel.py
hi
super
wow
:O
hax
Number of arguments: 6
sys.argv[0] = C:\Documents and Settings\Administrator\Desktop\thel.py
sys.argv[1] = hi
sys.argv[2] = super
sys.argv[3] = wow
sys.argv[4] = :O
sys.argv[5] = hax

You may be interested in a little Python module I wrote to make handling of command line arguments even easier (open source and free to use) - Commando

Simplest form:

import sys

def main(args):
    pass # your stuff here

if __name__ == "__main__":
    main(sys.argv[1:])

Shodow14l be friendly ok?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.