Skip to content

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

Open

Description

@AlJohri

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?

Activity

  1. dlh commented on Feb 18, 2020

    @dlh

    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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions