auto_cli#

petpal.meta.auto_cli.auto_cli(petpal_class: object)#

Generate a command line interface for a PETPAL function

Parameters:

petpal_class (object) – Class defined in PETPAL that can be instantiated without specifying __init__ arguments. Must contain function __call__ with a docstring.

Important

The provided class must be designed such that any __init__ arguments can be set with defaults, and those defaults should only be changed for testing and development purposes. Otherwise, it would be impossible to tell which CLI arguments should be assigned to the __init__ function and which should be assigned to __call__.

Additionally, any __call__ arguments and keyword arguments must be able to be specified from the command line, such as strings, integers, or floats.

Keyword arguments are only intended to be used with the CLI when it would be excessive and cumbersome to implement all possible arguments of a function. Any expected user inputs should be specified, documented, and type hinted in the __call__ args, reserve kwargs only for providing user flexibility where necessary, as it carries the risk of unintended behavior.

Example

import numpy as np
from petpal.meta.auto_cli import auto_cli
import external_func

class MyClass:
    mri_img: ants.ANTsImage
    pet_img: ants.ANTsImage

    def my_func(self, **kwargs):
        return external_func(self.mri_img, self.pet_img, **kwargs)

    def __call__(self, mri_img_path, pet_img_path, out_img_path, **kwargs):
        self.mri_img = ants.image_read(mri_img_path)
        self.pet_img = ants.image_read(pet_img_path)
        output_img = my_func(**kwargs)
        ants.image_write(output_img, output_img)

def main():
    # Creates CLI for class my_class
    # __call__ args are interpreted as required arguments
    # **kwargs is interpreted as optional keyword arguments
    # usage: python my_class.py my-class --mri-img-path [MRI_IMG_PATH] \
    #          --pet-img-path [PET_IMG_PATH] --kwargs [kwarg1=val1 kwarg2=val2 ...]
    # Add file path to pyproject.toml to generate a command shortcut

    auto_cli(petpal_class=my_class)

if __name__=='__main__':
    main()