Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Recommended way to forward arguments to commands #995

Open
rmorshea opened this issue May 29, 2024 · 2 comments
Open

Recommended way to forward arguments to commands #995

rmorshea opened this issue May 29, 2024 · 2 comments

Comments

@rmorshea
Copy link

rmorshea commented May 29, 2024

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.

@rmorshea rmorshea changed the title Recommended way to forward arguments to Recommended way to forward arguments to commands May 29, 2024
@rmorshea
Copy link
Author

I've found the logic that allows this to work and tests which enshrine it. So this approach does seem safe.

@rmorshea
Copy link
Author

Given that this seems to be a well defined behavior it would be neat if I could define a task like:

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant