First, all control mechanisms for commands are managed by a single decorator called @argument
. The full signature for this decorator is:
--arg=value
), default None-i
in the CLI would be ‘i’), default None--information
in the CLI would be ‘information’), default None--help
, default None-h
is requested, default NoneArguments are positional after the defined command.
import angreal
@angreal.command(name="echo", about="an echo replacement")
@angreal.argument(name="phrase", help="the phrase to echo", required=True)
def task_echo(phrase):
print(phrase)
angreal echo --help ─╯
echo
an echo replacement
USAGE:
echo <phrase>
ARGS:
<phrase> the phrase to echo
OPTIONS:
-h, --help Print help information
An option is usually something that takes an argument in order control command behavior.
import angreal
green = "\33[32m"
red = "\33[31m"
end = "\33[0m"
@angreal.command(name="echo", about="an echo replacement")
@angreal.argument(name="phrase", help="the phrase to echo", required=True)
@angreal.argument(name="color", long="color", short='c', help="apply a color to the echo phrase")
def task_echo(phrase,color=None):
if color=="red":
print(red + phrase + end )
return
if color=="green":
print(green + phrase + end )
return
print(phrase)
$ angreal echo --help ─╯
echo
an echo replacement
USAGE:
echo [OPTIONS] <phrase>
ARGS:
<phrase> the phrase to echo
OPTIONS:
-c, --color <color> apply a color to the echo phrase
-h, --help Print help information
A flag is just a binary value that will set a resulting value to True without taking a value.
import angreal
green = "\33[32m"
red = "\33[31m"
end = "\33[0m"
@angreal.command(name="echo", about="an echo replacement")
@angreal.argument(name="phrase", help="the phrase to echo", required=True)
@angreal.argument(name="color", long="color", short='c', help="apply a color to the echo phrase")
@angreal.argument(name="yell", long="yell", short='y', takes_value=False, help="yell it from the tree tops", is_flag=True)
def task_echo(phrase,color=None,yell=False):
if yell:
phrase = phrase.upper() + "! ! !"
if color=="red":
print(red + phrase + end )
return
if color=="green":
print(green + phrase + end )
return
print(phrase)
$ angreal echo --help ─╯
echo
an echo replacement
USAGE:
echo [OPTIONS] <phrase>
ARGS:
<phrase> the phrase to echo
OPTIONS:
-c, --color <color> apply a color to the echo phrase
-h, --help Print help information
-y, --yell yell it from the tree tops