Ayush
Ayush

Reputation: 949

How to pass more than one arguments with docopt

I want to pass two mandatory argument, one optional argument to my program using docopt. The code I am using is:

"""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

When I run above program using above command, I get error message displaying usage that I've written. What is going wrong here, how can I use more than one 'Arguments'?

Upvotes: 1

Views: 1149

Answers (1)

J. P. Petersen
J. P. Petersen

Reputation: 5031

Enclose arguments in <...>, otherwise they are just threated as commands. This should work:

"""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()

Upvotes: 5

Related Questions