Search code examples
python-3.xdocopt

docopt not printing arguments


I am trying to learn how to use docopt.

Below is my file docopt_test.py. Ideally I should get the passed arguments printed after I run this, but I am only getting output that is printing the usage comment.

Can anyone kindly point me to what am I doing incorrect?

Thanks.

$python3 docopt_test.py client task environment working MCE.zip

Usage:
    docopt_test.py client task environment [--local_tmp_folder=<td>] [--filenm=<filename>]
    docopt_test.py (-h | --help)
    docopt_test.py --version
#docopt_test.py
"""
Usage:
    docopt_test.py client task environment [--local_tmp_folder=<td>] [--filenm=<filename>]
    docopt_test.py (-h | --help)
    docopt_test.py --version

Options:
  -h --help     Show this screen.
  --version     Show version.
  --entity=<entityname>  specify entity to load
"""
from docopt import docopt

if __name__ == '__main__':
    arguments = docopt(__doc__, argv=None, help=True,
                   version=None, options_first=True)
    print(arguments)

Solution

  • I guess I found out what went wrong earlier.

    I had put options_first=True while assigning the arguments, but was passing the optional arguments with [ ] at the end. This is the reason docopt was not able to parse my inputs.

    Therefore to resolve the issue,

    I had to either remove the options_first=True from the arguments

    arguments = docopt(__doc__,argv=None,help=True,version=None)
    

    or change the usage comment and provide my input in correct order.

    """
    Usage:
      docopt_test.py [--local_tmp_folder=<td>] [--filenm=<filename>] client task environment
    """
    
    $python3 docopt_test.py --local_tmp_folder=working --filenm=MCE.zip client task environment