argtyped icon indicating copy to clipboard operation
argtyped copied to clipboard

Add support for documentation

Open Henri-ColibrITD opened this issue 10 months ago • 2 comments

Love the project ! But IMO it's missing some key features for me to use it. The main one being documentation. Here is an example to what I'd love to have:

parser = argparse.ArgumentParser(
    prog="DB initialization helper",
    description="This script initializes the database and optionally add constraints.",
)

parser.add_argument(
    "-n",
    "--table-name",
    help="The name of the table to initialize",
    default="test",
)
parser.add_argument(
    "--constraints",
    action=argparse.BooleanOptionalAction,
    default=True,
    help="Wether the constraints should be added or not",
)
arguments = parser.parse_args(sys.argv[1:])
print(arguments)

could be equivalent to

class Parser(Arguments):
    """This script initializes the database and optionally add constraints
    and data."""

    table_name: str = "test"
    """The name of the table to initialize"""
    constraints: Switch = True
    """Wether the constraints should be added or not"""
arguments = Parser(sys.argv[1:])

for now, the first one gives this help:

usage: DB initialization helper [-h] [-n TABLE_NAME] [--constraints | --no-constraints]

This script initializes the database and optionally add constraints and data.

optional arguments:
  -h, --help            show this help message and exit
  -n TABLE_NAME, --table-name TABLE_NAME
                        The name of the table to initialize
  --constraints, --no-constraints
                        Wether the constraints should be added or not (default: True)

while the later gives

usage: init_db.py [-h] [--table-name TABLE_NAME] [--constraints] [--no-constraints]

optional arguments:
  -h, --help            show this help message and exit
  --table-name TABLE_NAME
  --constraints
  --no-constraints

Here the improvements would be:

  1. support for docstring as help
  2. Switch could use argparse.BooleanOptionalAction so that the documentation looks nicer

There is also the support for shorthands which would be nice, but I'll create a separate issue for this.

Henri-ColibrITD avatar Feb 24 '25 10:02 Henri-ColibrITD

Btw, I'm happy to help implementing this feature

Henri-ColibrITD avatar Feb 24 '25 10:02 Henri-ColibrITD

Hi, thanks for the suggestion. I agree with both these improvements. The second one sounds pretty straightforward. For the first one though, at one point in the past I considered something similar, but my conclusion then was that it's not trivial to do. Python doesn't provide programmatic access to attribute docstrings, so your best bet is to parse the code, which takes some effort and usually isn't robust enough.

That said, if you'd like to submit a pull request, I'd be happy to review it.

huzecong avatar Feb 25 '25 00:02 huzecong