pythoncommand-line-argumentsdocstringdocopt

How can text in the options configuration of Docopt be wrapped?


I have a few detailed option specifications in the docstring used for configuration of Docopt. Some of the items are quite lengthy. Is there a way of wrapping the text to make it more legible or to make it fit to a line width more easily?

Let's say the relevant bit of text in the docstring is the following:

Usage:
    program [options]

Options:
    -h, --help                      Show this help message.
    -c, --configuration=CONF        Configuration (file) [default: None]
    -f, --files=FILESLIST           Comma-delimited list of input data files [default: 169888_ttH_el.root]
    -v, --variables=VARIABLESLIST   Comma-delimited list of variables to plot [default: trk_pt]
    -t, --tree=TREE                 Tree in input data files [default: mini]
    -u, --username=USERNAME         Username
    -t, --topanalysis=DIRECTORY     Directory of TopRootCore or TopAnalysis [default: /home/user/Dropbox/TopAnalysis]
    -s, --superlongoption=TEST      This is a very long option that requires a bit of text to explain it. [default: 101001011011101010010100110101010]
    --version                       Show the version and exit.

Would it be possible wrap the text in a style something like the following?

Usage:
    program [options]

Options:
    -h, --help                      Show this help message.
    -c, --configuration=CONF        Configuration (file) [default: None]
    -f, --files=FILESLIST           Comma-delimited list of input data files
                                    [default: 169888_ttH_el.root]
    -v, --variables=VARIABLESLIST   Comma-delimited list of variables to plot
                                    [default: trk_pt]
    -t, --tree=TREE                 Tree in input data files [default: mini]
    -u, --username=USERNAME         Username
    -t, --topanalysis=DIRECTORY     Directory of TopRootCore or TopAnalysis
                                    [default: /home/user/Dropbox/TopAnalysis]
    -s, --superlongoption=TEST      This is a very long option that requires a
                                    bit of text to explain it.
                                    [default: 101001011011101010010100110101010]
    --version                       Show the version and exit.

Solution

  • How is option definition and description defined

    The "secrets" are:

    How to deal with description formatting

    There are few things, which help using longer option descriptions or option definitions.

    How was original code reorganized

    Here is example of reorganized doc string from your example.

    What was done:

    Rewritten example

    Final code looks as follows:

    """
    Usage:
        program [options]
    
    General options:
      These things are rather general, so placed in this group of option.
    
        -h, --help            Show this help message.
        --version             Show the version and exit.
        -c, --configuration=CONF
                              Configuration (file) [default: None]
    
    Directory and path related stuff:
      Whatever relates to file or directory, comes here.
    
        -f, --files=FILESLIST
                              Comma-delimited list of input data files
                              [default: 169888_ttH_el.root]
        -t, --tree=TREE       Tree in input data files [default: mini]
        -t, --topanalysis=DIRECTORY
                              Directory of TopRootCore or TopAnalysis
                              [default: /home/user/Dropbox/TopAnalysis]
    Other things:
      Remaining options live here.
    
        -v, --variables=VARIABLESLIST
                              Comma-delimited list of variables to plot
                              [default: trk_pt]
        -u, --username=USERNAME
                              Username
        -s, --superlongoption=TEST
                              This is a very long option that requires a bit of text
                              to explain it.
                              [default: 101001011011101010010100110101010]
    """
    if __name__ == "__main__":
        from docopt import docopt
        args = docopt(__doc__)
        print args