HPInc / HP-Digital-Microfluidics

HP Digital Microfluidics Software Platform and Libraries
MIT License
2 stars 0 forks source link

Allow specification of single-task exercisers #175

Closed EvanKirshenbaum closed 5 months ago

EvanKirshenbaum commented 5 months ago

The current Exerciser framework requires the user to specify a task name before the arguments, e.g.,

joey display --min-time 1hr

There are times when you want a script to unconditionally run a single task, and it would be more user-friendly if the task didn't have to be specified.

Telling the Exerciser about its tasks is done in

    def add_task(self, task: Task, *,
                 name: Optional[str] = None,
                 description: Optional[str] = None,
                 aliases: Optional[Sequence[str]] = None) -> Exerciser:
        name = task.name if name is None else name
        desc = task.description if description is None else description
        aliases = task.aliases if aliases is None else aliases
        parser = self.subparsers.add_parser(name, help=desc, description=desc, aliases=aliases)
        task.add_args_to(parser, exerciser=self)
        self.add_common_args_to(parser)
        parser.set_defaults(task=task)

        return self

I think it should be possible to simply pull out the set-up:

    def setup_task(self, task: Task, parser: ArgumentParser) -> None:
        task.add_args_to(parser, exerciser=self)
        self.add_common_args_to(parser)
        parser.set_defaults(task=task)

    def add_task(self, task: Task, *,
                 name: Optional[str] = None,
                 description: Optional[str] = None,
                 aliases: Optional[Sequence[str]] = None) -> Exerciser:
        name = task.name if name is None else name
        desc = task.description if description is None else description
        aliases = task.aliases if aliases is None else aliases
        self.setup_task(task, self.subparsers.add_parser(name, help=desc, description=desc, aliases=aliases)

        return self

and then add

    def only_task(self, task: Task) -> Exerciser:
        self.setup_task(task, self.parser)

        return self

probably with some checks to make sure that add_task() hasn't been called and won't be called.

As an alternative, it may be worthwhile to have the Exerciser's constructor take an optional Task argument which, if present, will be used as its only task. The advantage to this is that it means that add_subparsers won't be called, which is probably a requirement, as having it there may well confuse things.

Migrated from internal repository. Originally created by @EvanKirshenbaum on Jun 16, 2022 at 10:18 AM PDT. Closed on Jun 16, 2022 at 11:01 AM PDT.
EvanKirshenbaum commented 5 months ago

This issue was referenced by the following commit before migration:

EvanKirshenbaum commented 5 months ago

I decided against including an only_task() method. You either pass a Task to __init__() (and have no subtasks) or add Tasks one by one with add_task().

Migrated from internal repository. Originally created by @EvanKirshenbaum on Jun 16, 2022 at 11:01 AM PDT.