I am creating a command line application in Python using the Click library that accepts a name as input but if no name is entered it returns the default value.
Here is the code I have so far.
hello.py
import click
@click.version_option(1.0)
@click.command()
@click.argument('string', default='World')
@click.option('-r', '--repeat', default=1, help='How many times should be greeted.')
def cli(string,repeat):
'''This string greets you.'''
for i in xrange(repeat):
click.echo('Hello %s!' % string)
if __name__ == '__main__':
cli()
When I run it.
$ hello
Hello World!
$ hello Bob
Hello Bob!
$ hello Bob -r 3
Hello Bob!
Hello Bob!
Hello Bob!
This is exactly what I want.
Now, I would like to be able to accept input from stdin like the following examples.
$ echo John | hello
Hello John!
$ echo John | hello -r 3
Hello John!
Hello John!
Hello John!