Skip to content

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

@baodrate

Description

@baodrate

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or request

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions