Philippe Hebert
Philippe Hebert

Reputation: 2028

Python invoke: Get the flags from a parent command?

Given the following python invoke script:

from invoke import task

@task
def pre(c):
   print("pre")

@task(pre=[pre])
def command(c, flag):
   print(f"command flag={flag}")

Called with the following shell command:

inv command --flag

I would like to read the value of flag to conditionally do some actions in pre. Is there a way I can read the flag property passed to command from within pre using invoke's API? I couldn't find anything in the docs about it.

I am aware that push comes to shove, I can import sys and read the args directly, but I'd rather avoid doing that work manually if I can.

Upvotes: 1

Views: 450

Answers (1)

Philippe Hebert
Philippe Hebert

Reputation: 2028

After reading through a few GH issues, I found that the feature has yet to be implemented at this point in time.

Discussion can be found here, and a PR for a similar feature can be found here.

The proposed solution in the first discussion is to call the pre/post task directly in the main task:

@task
def pre(c, flag):
   print("pre")

@task
def command(c, flag):
   pre(c, flag)
   print(f"command flag={flag}")

Upvotes: 0

Related Questions