swansonk14/typed-argument-parser

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

Opened this issue · 1 comments

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.

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