2

The below code is purely for illustration purposes. I want to write a task like this:

>echo '11da33' | inv remove-number
da

How can I work with stdin inside of an invoke task? I checked the documentation but found nothing

kharandziuk
  • 12,020
  • 17
  • 63
  • 121

1 Answers1

2

Accepting user input inside invoke is pretty straightforward. Here I used standard input but if you want advanced features you can use click prompt as well, but the approach is same.

@invoke.task()
def greet(ctx):
    print("Whom would you like to greet? ")
    if name := input():
        print(f"Hello {name}!")
    else:
        print("Hello World!")

When you run the task.

$ inv greet                
Whom would you like to greet? 
universe # user input
Hello universe! # output

You can pipe the input as well.

$ echo universe | inv greet
Whom would you like to greet? 
Hello universe!
sreenivas
  • 2,067
  • 1
  • 19
  • 29