Add support for documentation
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:
- support for docstring as help
-
Switchcould useargparse.BooleanOptionalActionso 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.
Btw, I'm happy to help implementing this feature
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.