I want to pass data to script, like this
if __name__ == '__main__':
usage = 'python pull.py [-h <host>][-p <port>][-r <risk>]arg1[,arg2..]'
parser = OptionParser(usage)
parser.add_option('-o', '--host', dest='host', default='127.0.0.1',
┊ help='mongodb host')
parser.add_option('-p', '--port', dest='port', default=27017,
┊ help="mongodb port")
parser.add_option('-r', "--risk", dest='risk', default="high",
┊ help="the risk of site, choice are 'high', 'middle', 'low', 'all'")
options, args = parser.parse_args()
in this script, if I want to set ./test.py -r high and middle, how can I set ['high', 'middle']
in optparse
?
https://docs.python.org/2/library/optparse.html#standard-option-types
e.g:
If you want to be able to pass multiple values to
--risk
, you should useaction="append"
:Also beware of combining
action="append"
withdefault=['high']
, because you'll end up in always having 'high' in youroptions.risk
.Usage: