pydoit / doit

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

Support command line parameters on `doit run` #273

Open Pluckerpluck opened 5 years ago

Pluckerpluck commented 5 years ago

EDIT: Add this answer to documentation HOW-TO section

So I've recently started using doit, and it seem good. But I realized I wanted to add a --debug flag to the build, which would set some tasks to perform slightly different roles (like logging to a debug file).

Now I could create a debug task, or I could use a command line variable, but the --debug flag seems cleanest, and is also more standard in terms of how other programs work.

Currently attempting to use options on doit or doit run will result in:

ERROR: Error parsing Command: option --debug not recognized (parsing options: ['--debug'])

So my question is whether support can be added for passing parameters in this way?


I see a few potential options:

  1. Invalid options are simply ignored and not sent as parameters to any tasks rather than erroring
  2. You define global parameters, which allows them on any task and then: a. They are passed to any tasks that provide the same name in their params, others are ignored b. They are stored as global variables, retrieved by get_var

Just a thought I had trying to create my build scripts.

Fund with Polar

schettino72 commented 5 years ago

This discussion is more suitable to email list, anyway...

Invalid options are simply ignored and not sent as parameters to any tasks rather than erroring

This would go against doit design, as ignoring a parameter when you do not want it to be ignored would be hard to understand/debug.

You define global parameters, which allows them on any task and then: a. They are passed to any tasks that provide the same name in their params, others are ignored b. They are stored as global variables, retrieved by get_var

It is already possible to achieve that with a slightly different syntax than standard parameter options: http://pydoit.org/task_args.html#command-line-variables-doit-get-var


Another option is to just replace the standard run command with a plugin. like

myrun.py

from doit.doit_cmd import set_var
from doit.cmd_run import Run

opt_debug = {
    'name':'debug',
    'short': '',
    'long':'debug',
    'type': bool,
    'default': False,
    'help': """a global flag"""
}

class MyRun(Run):
    cmd_options = Run.cmd_options + (opt_debug,)

    def execute(self, params, args):
        set_var('debug', params.get('debug'))
        return super().execute(params, args)

doit.cfg

[COMMAND]
run = myrun:MyRun

dodo.py

from doit import get_var

def task_x():
    return {
        'actions': ['echo hi {}'.format(get_var('debug'))],
        'verbosity': 2,
    }

doit --debug