Daniel Daniel - 1 year ago 78
Perl Question

GetOption PERL to Python

How would I write this PERL line in python?

GetOptions('help|?' => \$help, 'man' => \$man, 'debug!' => \$debug, 'devpath:s' => \$devpath, 'dev2:s' => \$dev2,'rev1:s' => \$prj_rev1, 'rev2:s' => \$prj_rev2, 'prj:s' => \$prj)


Answer Source

There is a module in python called argparse. You can you the module to solve your problem.

Code example : test.py

import argparse
import os

commandLineArgumentParser = argparse.ArgumentParser()
commandLineArgumentParser.add_argument("-fname", "--fname",  help="first name")
commandLineArgumentParser.add_argument("-lname","--lname", help="last name")
commandLineArguments = commandLineArgumentParser.parse_args()

fname = commandLineArguments.fname
lname = commandLineArguments.lname

print "%s\n%s" %(fname,lname)

Run example

python test.py -fname ms -lname = dhoni


Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download