如何使用 docopt 传递多个参数
How to pass more than one arguments with docopt
我想使用 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()
我想使用 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()