我想使用docopt将两个强制参数和一个可选参数传递给我的程序。我使用的代码是:
"""Setup
Usage: myprog.py server_name config [--help] [options]
Arguments:
SERVER_NAME Server Name (a1, a2)
CONFIG Config file with full path
Options:
-h --help
-r --start Start the server if yes [default: 'no']
"""
from docopt import docopt
class ServerSetup(object):
def __init__(self, server_name, config_file, start_server):
self.server = server_name
self.config = config_file
self.start_server = start_server
def print_msg(self):
print self.server
print self.config
print self.start_server
if __name__ == '__main__':
args = docopt(__doc__)
setup = ServerSetup(server_name=args['SERVER_NAME']),
config=args['CONFIG']
start_rig=args['-r'])
setup.print_msg()
$python myprog.py a1/abc/file1.txt
当我使用上面的命令运行上面的程序时,我会收到一条错误消息,显示我所写的用法。这里出了什么问题,我如何使用多个"Arguments"?
将参数封装在<…>中,否则,它们只是被当作命令来威胁。这应该有效:
"""Setup
Usage: myprog.py [options] <SERVER_NAME> <CONFIG>
Arguments:
SERVER_NAME Server Name (a1, a2)
CONFIG Config file with full path
Options:
-h, --help
-r, --start Start the server if yes [default: 'no']
"""
from docopt import docopt
class ServerSetup(object):
def __init__(self, server_name, config_file, start_server):
self.server = server_name
self.config = config_file
self.start_server = start_server
def print_msg(self):
print self.server
print self.config
print self.start_server
if __name__ == '__main__':
args = docopt(__doc__)
print args
setup = ServerSetup(server_name=args['<SERVER_NAME>'],
config_file=args['<CONFIG>'],
start_server=args['--start'])
setup.print_msg()