jsonargparse icon indicating copy to clipboard operation
jsonargparse copied to clipboard

CLI with non primitive types + subcommands

Open AlejandroBaron opened this issue 5 months ago • 3 comments

Hello!

I'm trying to do combine the CLI + config utility with subcommands and i'm not able to.

The CLI takes an object as an argument and in the subcommand it calls an object submethod.

This is a minimal example with 3 files

python example.py --config config.yaml subcommand

myclass.py

class MyClass:

    def __init__(self, x: int):
        self.x = x

    def method(self):
        print(self.x*2)

example.py (entrypoint)

from jsonargparse import CLI
from myclass import MyClass

class MyCLI:

    def __init__(self, myobj: MyClass):

        self.myobj = myobj

    def subcommand(self):
        self.myobj.method()

if __name__ == "__main__":
    CLI(MyCLI)

config.yaml

myobj:
  class_path: myclass.MyClass
  init_args:
    x: 2

Is this not supported yet?

AlejandroBaron avatar Jan 11 '24 08:01 AlejandroBaron