Search code examples
python-click

Is there way to customize col_max value without change python-click source code?


I'm facing a very concrete problem with python-click 8.1.3. The helptext created by Click wastes too much column space when an option name is a tad long. Depicted in picture below:

Helptext space waste

I trace into Click's source code, and pinpoint a hardcoded value in HelpFormatter.write_dl(), the col_max parameter determines first-column max-width, which is 30, and I hope to reduce it to 16.

PyCharm debugging HelpFormatter.write_dl()

As a Click-library user, how can I achieve this without modifying Click's source code? Maybe some class inheritance or patching trick?

Thank you in advance.


Solution

  • You can do something like this:

    class MyHelpFormatter(click.HelpFormatter):
        def write_dl(self, rows, col_max=5, col_spacing=2):
            super().write_dl(rows, col_max, col_spacing)
    
    
    click.Context.formatter_class = MyHelpFormatter
    

    Check this answer for a similar example