4

I have a code like below with multiple functions inside for various calculations.

I am using python fire to pass arguments instead of defining argparse and call the functions from cli. Every time I add a new parameter I have to add self for it in init. I am looking for a better way.

I have found python dataclasses can be a way around it. I have looked into python fire command grouping and multiple commands.

class MyClass:
    def __init__(
        self,
        input_path: str,
        output_path: str = '',
        same_size: bool = False,
        crop_size: int = 300,
        padding: int = 20,
        write_json: bool = False,
        write_image: bool = False,
        line_thickness: int = 2,
        side_color: Tuple = (255, 255, 0),
        top_color: Tuple = (255, 0, 0),
    ) -> None:
        super(MyClass, self).__init__()
        self.input_path = input_path
        self.output_path = output_path
        self.same_size = same_size
        self.crop_size = crop_size
        self.padding = padding
        self.write_json = write_json
        ...
        ...

    def something(
        self,
    ) -> None:
        ...
        ...
if __name__ == '__main__':
    fire.Fire(MyClass)

My question is how to get MyDataClass dataclass values in MyClass properly using python fire?

@dataclass
class MyDataClass:
    input_path: str
    output_path: str = ''
    same_size: bool = False
    crop_size: int = 300
    padding: int = 20
    write_json: bool = False
    write_image: bool = False
    line_thickness: int = 2
    side_color: Tuple = (255, 255, 0)
    top_color: Tuple = (255, 0, 0)
class MyClass:
    # init is not here anymore.

    def something(
        self,
    ) -> None:
        ...
        ...
B200011011
  • 3,798
  • 22
  • 33
  • I seem to have the same issue. Fire doesn't seem to generate a useful `--help` from a `@dataclass` – arod Jan 22 '23 at 02:05

0 Answers0