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

pass in arbitrary options/arguments as raw text? #693

Open
AlJohri opened this issue Jan 9, 2020 · 1 comment
Open

pass in arbitrary options/arguments as raw text? #693

AlJohri opened this issue Jan 9, 2020 · 1 comment

Comments

@AlJohri
Copy link

AlJohri commented Jan 9, 2020

I want to write a task such as invoke run bash -c echo "hello" where the task run is defined as follows in bash:

docker run -it myapp "$@"

How do I do the equivalent of "$@" in pyinvoke?

@dlh
Copy link

dlh commented Feb 18, 2020

I think you have 2 options:

  1. Manually parse the arbritray args yourself, using a -- seperator.
  2. Add an option to the task.
@task
def example1(c):
    rest_args = sys.argv[sys.argv.index("--") + 1 :] if "--" in sys.argv else []
    print("rest_args = {}".format(rest_args))
    # use shlex.join() if you're on Python 3.8
    c.run(
        "docker run -it myapp {}".format(" ".join([shlex.quote(x) for x in rest_args]))
    )


@task
def example2(c, docker_args=""):
    print("docker_args = {!r}".format(docker_args))
    c.run("docker run -it myapp {}".format(docker_args))
$ inv --dry --echo example1 -- a b "c with spaces"
rest_args = ['a', 'b', 'c with spaces']
docker run -it myapp a b 'c with spaces'

$ inv --dry --echo example2 --docker-args 'a b "c with spaces"'
docker_args = 'a b "c with spaces"'
docker run -it myapp a b "c with spaces"

The parser appears to support accessing the remainder args, however I'm unsure how to access that property.

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

2 participants