Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

docopt in python is giving me issues

I have a set of three programs that I am trying to combine into one. They all work individually, but I am having issues when trying to get them to work together. The issue I am currently having is with the first section of code:

import os
import sys
from contextlib import closing

import colorama  # $ pip install colorama
import docopt  # $ pip install docopt
import socks  # $ pip install PySocks
import stem.process  # $ pip install stem
from sockshandler import SocksiPyHandler  # see pysocks repository
from stem.util import term

try:
    import urllib2
except ImportError: # Python 3
    import urllib.request as urllib2

args = docopt.docopt(__doc__, version='0.2')
colorama.init(strip=not (sys.stdout.isatty() or args['--color']))

When I run the program, I get this error:

Traceback (most recent call last):
  File "cilantro.py", line 34, in <module>
    args = docopt.docopt(__doc__, version='0.2')
  File "C:\Python34\lib\site-packages\docopt.py", line 558, in docopt
    DocoptExit.usage = printable_usage(doc)
  File "C:\Python34\lib\site-packages\docopt.py", line 468, in printable_usage
    raise DocoptLanguageError('"usage:" (case-insensitive) not found.')
docopt.DocoptLanguageError: "usage:" (case-insensitive) not found.

Why am I getting this error message? The same code works fine in the original program.

like image 242
Steve-O Avatar asked Aug 01 '26 04:08

Steve-O


1 Answers

When using docopt you need to write a __doc__ string for your script. docopt parses this string to work out how to handle the command line options and arguments.

To fix this, and something like the following text above your "import os" (so it's the first thing in the file):

"""
Name.
Describe what this script does

Usage:
 name <firstarg>
 name --countdown
 name sillycommand <SILLYNESS>
 name -h | --help

Options:
  --countdown  display a count down
"""

More details on what to put in this usage string can be found at the docopt documentation, http://docopt.org/

like image 118
Geoff Crompton Avatar answered Aug 03 '26 20:08

Geoff Crompton



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!