Search code examples
pythonargumentspython-3.6program-entry-point

Python command line call to main does not go into main function


I have a python file with the following main function:

if __name__ == '__main__':
    args = docopt(__doc__)

    print('source: %s' % args['--src'])
    print('target: %s' % args['--tgt'])

Now when I call this function:

python test.py --src file1 --tgt file2

I get:

Usage:
    test.py --src=<file> --tgt=<file>
Options:
    -h --help            Show this screen.
    --src=<file>         src
    --tgt=<file>         tgt

But the main function logic is just not called. How to fix this?

I tried:

python test.py --src=file1 --tgt=file2

but I get the same result.


Solution

  • Check out your docstring. I believe the issue is due to a missing line break between Usage and Options sections there.

    I tried this docstring and it worked fine:

    """
    Usage:
        test.py --src=<file> --tgt=<file>
    
    Options:
        -h --help       Show this screen.
        --src<file>     src
        --tgt=<file>    tgt
    """
    from docopt import docopt
    
    if __name__ == '__main__':
        args = docopt(__doc__)
        print('source: %s' % args['--src'])
        print('target: %s' % args['--tgt'])