swansonk14 / typed-argument-parser

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

Refactor `tapify` to enable subparsers #112

Closed swansonk14 closed 7 months ago

swansonk14 commented 1 year ago

One important use case from Tap/tapify that is missing is the ability to create a subparser that can parse arguments for different functions depending on which subparser is called. This is made difficult because tapify both instantiates a Tap class and runs the function without exposing the Tap class, which is needed for subparsing. We propose to refactor tapify by first creating a function called convert_to_tap that takes a function and dynamically creates a Tap subclass definition with the arguments of that function. In tapify, this Tap class can be instantiated and used to parse arguments and run the function. This Tap class can also be used explicitly or can be used as a subparser. Please see a potential implementation here: https://github.com/swansonk14/chemfunc/blob/main/chemfunc/utils.py#L13

swansonk14 commented 1 year ago

Ensure that the arguments in the help string are in the same order as they are in the function.