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.
Yes, it can be done with optparse.
This is an example:
which prints:
Full working example below:
More information at http://docs.python.org/library/optparse.html#adding-new-actions
Here is a known working example (from http://docs.python.org/library/email-examples.html):
I think the usages is something like:
Could be easier to accept a comma-separated list of servers:
and split the value within your script.
You could try to check out argparse. It provides "nargs" parameter meaning you would be able to do something along
I know that's not exactly what requested but it might be a decent compromise without too much hassle. :)