swansonk14 / typed-argument-parser

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

[suggestion] `tapify(...)` should support classes/objects by producing subargument parsers #101

Open baodrate opened 1 year ago

baodrate commented 1 year ago

With #96, tapify() was introduced, which automagically produces and executes the appropriate argparse code based on the type annotations of the passed function:

from tap import tapify

def add(num_1: float, num_2: float) -> float:
    print(f'The sum of {num_1} and {num_2} is {num_1 + num_2}.')

if __name__ == '__main__':
    tapify(add)

similar to python-fire, tapify should support objects and/or classes, such that subparsers are created for each method. e.g.:

class Calculator(object):
  def __init__(self, offset: int = 1):
      self._offset = offset

  def add(self, x: int, y: int):
    print(x + y + self._offset)

  def multiply(self, x: int, y: int):
    print(x * y + self._offset)

if __name__ == '__main__':
  tapify(Calculator, offset=1)

Should produce a main parser with one optional --offset argument and two subparsers (add and multiply) with the appropriate options.

swansonk14 commented 1 year ago

Hi @qubidt,

This seems like a great extension of tapify! We hope to tackle this soon but would appreciate any PRs.

Thanks, Jesse and Kyle