I'm writing a little python script to get stats from several servers or a single server, and I'm using OptionParser to parse the command line input.
#!/usr/bin/python
import sys
from optparse import OptionParser
...
parser.add_option("-s", "--server", dest="server", metavar="SERVER", type="string",
help="server(s) to gather stats [default: localhost]")
...
my GOAL is to be able to do something like
#test.py -s server1 -s server2
and it would append both of those values within the options.server object in some way so that I could iterate through them, whether they have 1 value or 10.
Any thoughts / help is appreciated. Thanks.
optparse is deprecated since python 3.2 and is not developed anymore.
optparse is a more convenient, flexible, and powerful library for parsing command-line options than the old getopt module. optparse uses a more declarative style of command-line parsing: you create an instance of OptionParser , populate it with options, and parse the command line.
OptionParser is a class for command-line option analysis. It is much more advanced, yet also easier to use, than GetoptLong, and is a more Ruby-oriented solution.
import optparse
parser = optparse.OptionParser()
parser.add_option('-t', '--test', action='append')
options, args = parser.parse_args()
for i, opt in enumerate(options.test):
print 'option %s: %s' % (i, opt)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With