Search code examples
docopt

Why is a parameter parsed as a bool?


I usually use docopt to handle the command line parameters but I now have a case where the parameters is parsed unexpectedly (it must be a silly mistake of mine as it always works great)

"""
API to do something

Usage:
    api.py [options]

Options:
    --port PORT     port to listen on   [default: 64645]
    --url   URL     elasticsearch address   [default: http://elk.example.com:9200]
"""

This is parsed via a conf = docopt.docopt(__doc__) call after which I have conf set to

{
    '--port': '64645',
    '--url': False
}

The --url part is not correct but I cannot understand why.


Solution

  • It is because there are too many spaces between --url and URL, try:

    """
    API to do something
    
    Usage:
        api.py [options]
    
    Options:
        --port PORT     port to listen on   [default: 64645]
        --url URL       elasticsearch address   [default: http://elk.example.com:9200]
    """