Recommended way to forward arguments to commands

Open
#995 3 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Assessment

Difficulty
5/5
Estimated time
Over a week
Newbie friendliness
25/100
Issue type
Feature
Clarity
Needs clarification
Activity status
Stale
Tech stack
python
Domain
cli

Research direction

Start with Invoke's command-line parsing and Context entry points, focusing on the -- delimiter described in the issue. Determine whether forwarding is intended or needs an explicit API; done means the supported behavior is documented or the requested Context handling is specified and covered by an appropriate test.

Written by the indexing model from the issue text.

Description

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.

Dominant language
Python
Stars
4.8k
Forks
412
PR merge metrics
No merged PRs in 30d

Contributor guide

No contributing guide indexed for this repository

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from pyinvoke/invoke

All issues in pyinvoke/invoke

Similar issues

More Python issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.