Search code examples
pythoncommand-line-interfacedocopt

docopt doesn't apply default value


I have the following usage string:

    usage = """Usage: 
    counts_editing_precent_total_editing_to_csv.py out <output> files <pileupPercentFiles>... [--percentageField=<kn>] [--numReadsField=<kn>]
    counts_editing_precent_total_editing_to_csv.py -h | --help

Options:
     -h --help  show this screen.
     --percentageField=<kn>  the column of the percent field in the input file  [default: 7] .
     --numReadsField=<kn>  the column of the num of reads field in the input file  [default: 4] .

    """

and then I execute this code

    args = docopt(usage)
    print(args)

I run the following command:

python <filename>.py out a files a b c

The output is:

{'--help': False,
 '--numReadsField': None,
 '--percentageField': None,
 '-h': False,
 '<output>': 'a',
 '<pileupPercentFiles>': ['a', 'b', 'c'],
 'files': True,
 'out': True}

As you can see, the default values aren't used. I have seen other questions like this where the solution was 'Have two spaces between the command and it's description' and I made sure that I have. I really can't tell the difference from my example to the one which appears in the docs.


Solution

  • I ran it and it works. Recall that you should place the usage string before the from docopt import docopt at the top of the file. Here is a script to reproduce it.

    """Usage:
        counts_editing_precent_total_editing_to_csv.py out <output> files <pileupPercentFiles>... [--percentageField=<kn>] [--numReadsField=<kn>]
        counts_editing_precent_total_editing_to_csv.py -h | --help
    
    Options:
         -h --help  show this screen.
         --percentageField=<kn>  the column of the percent field in the input file  [default: 7] .
         --numReadsField=<kn>  the column of the num of reads field in the input file  [default: 4] .
    
    """
    from docopt import docopt
    ARGUMENTS = docopt(__doc__)
    print(ARGUMENTS)