swansonk14 / typed-argument-parser

Typed argument parser for Python
MIT License
494 stars 40 forks source link

Feature request: improve help text parsing from code comments #105

Closed Ririshi closed 1 year ago

Ririshi commented 1 year ago

I'm using TAP for almost every script I write and something that is bothering me is that I cannot easily place help texts using comments when the line gets long because of a argument name, type name or default value. Specifically, I use Black for formatting, which breaks the default value into multiple lines, and the comment is placed on the last line, behind the parenthesis (ExampleOne parser in the code below).

I could manually move the comment back to the argument name's line, just behind the opening parenthesis (ExampleTwo), but this line is oftentimes quite long already because of the argument and type name. The comment still ends up exceeding the recommended maximum of 88 characters in those cases. Black then reformats it again to look like ExampleThree. To solve this, I would like TAP to pick up a comment behind the closing parenthesis and use it as its help text. This way, it's compatible with Black formatting.

Having the option of placing the comment on the line above the argument (ExampleFour) would be nice as well.

Finally, some arguments might require some more extensive explanation, requiring multiple comment lines (especially when limiting each line to 88 characters). It would be nice if support was added for this. This would work well if comments were allowed above the argument in question, but it might also work for multi-line comments that start behind a closing parenthesis (if one exists).

There would need to be some kind of order of precedence for comments if multiple are added. For this, I propose the following order:

  1. At the end of the argument name's line
  2. Line(s) above argument name
  3. Behind the closing parenthesis, if one exists (possibly with more lines following it)

My reasoning behind this order is that most short comments will fit on the same line. The next option is on the line above, because that leads (imo) to best readability in the code if the comment cannot be placed on the same line as the argument name. The third option works well for developers who place the comment on the same line and then use Black or some other formatting tool which automatically puts the comment behind the closing parenthesis.

from tap import Tap

class ExampleOne(Tap):
    short_arg: int  # This is short and sweet and fits on one line
    long_name_tuple_argument: tuple[int, int, int] = (
        100,
        200,
        300,
    )  # Allowing comments placed here as help text would reduce line length

class ExampleTwo(Tap):
    long_name_tuple_argument: tuple[int, int, int] = (  # This comment is shown as help text but leads to a long line
        100,
        200,
        300,
    )

# Black reformats ExampleTwo as follows, because the comment line is too long again
class ExampleThree(Tap):
    long_name_tuple_argument: tuple[
        int, int, int
    ] = (  # This comment is shown as help text but leads to a long line
        100,
        200,
        300,
    )

class ExampleFour(Tap):
    # Allowing a comment here allows the argument and default value to stay on one line
    long_name_tuple_argument: tuple[int, int, int] = (100, 200, 300)
swansonk14 commented 1 year ago

Hi @Ririshi,

We're glad to hear that you're enjoying Tap! Thank you for the comprehensive issue! We currently do have an option for providing multiline comments in order to avoid long lines. Specifically, you can use triple quotes on the line following an argument to write a comment that will be converted to a help string for that argument. You can see an example in the README here: https://github.com/swansonk14/typed-argument-parser#tap-help

Please feel free to reopen the issue and let us know if there is another use case that is not addressed by this solution.

Best, Jesse and Kyle