doit icon indicating copy to clipboard operation
doit copied to clipboard

Task title created by `title_with_actions` does not replace parameter and options values.

Open smarie opened this issue 6 years ago • 2 comments

Consider the following dodo.py file:

from doit.tools import title_with_actions
DOIT_CONFIG = {'default_tasks': ['mytask']}

def task_mytask():
    return {
            # 'name': 'mysubtask',
            'params': [{
                'name': 'myparam',
                'short': 'p',
                'long': 'myparam',
                'default': 'param_default',
            }],
            'actions': ["echo myparam is %(myparam)s"],
            'title': title_with_actions,
            'verbosity': 2,
        }

Executing it yields:

>>> doit
.  mytask => Cmd: echo myparam is %(myparam)s
myparam is param_default

You can notice that the title created for the action is not correct: one would expect to see Cmd: echo myparam is param_default, in other words one would expect that this title, since it is created after task instantiation, actually represents what will be executed.

I see two ways to fix this:

  • fixing only the title_with_actions method. This would force us to write an ugly if/elif statement in it to support all kind of BaseAction subtypes.
  • fixing the __str__ in all BaseAction subtypes directly. For example for CmdAction:
    def __str__(self):
        # try to replace all options in the various commands
        try:
            _action = self.expand_action()
        except Exception:
            _action = self._action

        return "Cmd: %s" % _action

smarie avatar Jul 29 '19 14:07 smarie

Your example can be written like this:

DOIT_CONFIG = {'default_tasks': ['mytask']}

def task_mytask():
    return {
            # 'name': 'mysubtask',
            'params': [{
                'name': 'myparam',
                'short': 'p',
                'long': 'myparam',
                'default': 'param_default',
            }],
            'actions': ["echo myparam is %(myparam)s"],
            'title': lambda t: str(t.actions[0]) % t.options,
            'verbosity': 2,
        }

It is trivial to write your own, and does not require changes in doit core. Said that, I would not oppose to a change in title_with_actions.

schettino72 avatar Jul 29 '19 21:07 schettino72

Thanks for the tip ! Even if users can customize many things, I guess that they prefer not to do so, so as to keep their code readable. So I'll try to propose a PR.

smarie avatar Jul 30 '19 16:07 smarie