pyinvoke / invoke

Pythonic task management & command execution.
http://pyinvoke.org
BSD 2-Clause "Simplified" License
4.31k stars 365 forks source link

Recommended way to forward arguments to commands #995

Open rmorshea opened 1 month ago

rmorshea commented 1 month ago

It'd like to know the recommended way to forward arguments to a command. For example, I have a test task that runs PyTest and I'd like to forward any extra arguments along to pytest. The best way I've found to do this is delimit my task's arguments from those of pytest with --. If I do this invoke seems to ignore all the following arguments. Ultimately this means I can do the following:

import sys
from invoke import task

try:
    EXTRA_ARGS = sys.argv[sys.argv.index("--") + 1: ]
except ValueError:
    EXTRA_ARGS = []

@task
def test(ctx):
    ctx.run(f"pytest {' '.join(EXTRA_ARGS)}"

Where usage could then be:

invoke test -- test_something.py::test_feature --maxfail=2 

While this works, I searched through the documentation and even a bit of the code, but didn't see anything that would explicitly allow for this sort of "argument forwarding" pattern. Is this behavior intended or is there a better way to achieve this? If it is something that invoke intentionally supports, perhaps these extra args (everything following --) could be attached to the Context.