pydoit / doit

CLI task management & automation tool
http://pydoit.org
MIT License
1.85k stars 175 forks source link

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

Open smarie opened 5 years ago

smarie commented 5 years ago

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:

    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

Fund with Polar

schettino72 commented 5 years ago

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.

smarie commented 5 years ago

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.