Search code examples
pythondocopt

"docopt.DocoptLanguageError: --replicas must not have an argument" only with "replicas", not "replication"


Here is a working version of DocOpt (partially taken from naval fate):

"""
Instance Manager

Usage:
  instance_manager cluster create <name> <manager> <worker>... [--google|--virtualbox]
  instance_manager cluster delete <name>
  instance_manager cluster <name> add <worker> [--size=<size>]
  instance_manager cluster <name> remove <worker>
  instance_manager cluster <name> service add <service> [--replication=<replication>]
  instance_manager cluster <name> service remove <service>
  instance_manager cluster <name> service remove <service>
  instance_manager instance (add|remove) <x> <y> [--moored|--drifting]
  instance_manager -h | --help
  instance_manager --version

Options:
  -h --help     Show this screen.
  --version     Show version.
  --size=<size> Size of the container.
  --moored      Moored (anchored) instance.
  --drifting    Drifting instance.
  --google  Google Cloud.
  --virtualbox  VirtualBox.
  --replication Service Replication.
"""
from docopt import docopt


if __name__ == '__main__':
    arguments = docopt(__doc__, version='0.1.1rc')
    print(arguments)

Here is a non-working version of DocOpt.

"""
Instance Manager

Usage:
  instance_manager cluster create <name> <manager> <worker>... [--google|--virtualbox]
  instance_manager cluster delete <name>
  instance_manager cluster <name> add <worker> [--size=<size>]
  instance_manager cluster <name> remove <worker>
  instance_manager cluster <name> service add <service> [--replicas=<replicas>]
  instance_manager cluster <name> service remove <service>
  instance_manager cluster <name> service remove <service>
  instance_manager instance (add|remove) <x> <y> [--moored|--drifting]
  instance_manager -h | --help
  instance_manager --version

Options:
  -h --help     Show this screen.
  --version     Show version.
  --size=<size> Size of the container.
  --moored      Moored (anchored) instance.
  --drifting    Drifting instance.
  --google  Google Cloud.
  --virtualbox  VirtualBox.
  --replicas    Service Replication.
"""
from docopt import docopt


if __name__ == '__main__':
    arguments = docopt(__doc__, version='0.1.1rc')
    print(arguments)

Error:

docopt.DocoptLanguageError: --replicas must not have an argument

The only difference is that one uses the word replicas instead of replication.

diff first.py second.py 
9c9
<   instance_manager cluster <name> service add <service> [--replication=<replication>]
---
>   instance_manager cluster <name> service add <service> [--replicas=<replicas>]
24c24
<   --replication Service Replication.
---
>   --replicas    Service Replication.

This is unacceptable.


Solution

  • You need two spaces after --replication, you have only one. Same after --size=<size>.

    Also, in the second example there is both --replicas (no argument) in the [Options] and --replicas=<replicas> in the pattern (top section). These contradict each other, it is not clear whether an argument should be allowed or not.

    Lastly, no need to have two identical patterns, remove one:

    instance_manager cluster <name> service remove <service>
    

    The docs: https://github.com/docopt/docopt#option-descriptions-format

    Use two spaces to separate options with their informal description:

    --verbose More text.   # BAD, will be treated as if verbose option had
                           # an argument "More", so use 2 spaces instead
    -q        Quit.        # GOOD
    -o FILE   Output file. # GOOD
    --stdout  Use stdout.  # GOOD, 2 spaces
    

    Here is a working pattern with --replication:

    Instance Manager
    
    Usage:
      instance_manager cluster create <name> <manager> <worker>... [--google|--virtualbox]
      instance_manager cluster delete <name>
      instance_manager cluster <name> add <worker> [--size=<size>]
      instance_manager cluster <name> remove <worker>
      instance_manager cluster <name> service add <service> [--replication=<replicas>]
      instance_manager cluster <name> service remove <service>
      instance_manager instance (add|remove) <x> <y> [--moored|--drifting]
      instance_manager -h | --help
      instance_manager --version
    
    Options:
      -h --help      Show this screen.
      --version      Show version.
      --size=<size>  Size of the container.
      --moored       Moored (anchored) instance.
      --drifting     Drifting instance.
      --google       Google Cloud.
      --virtualbox   VirtualBox.
      --replication=<replicas>  Service Replication.
    
    

    Live (prefilled) test on http://try.docopt.org