https://click.palletsprojects.com/en/7.x/options/
Name Your Options
A name is chosen in the following order
-
If a name is not prefixed, it is used as the Python argument name and not treated as an option name on the command line.
-
If there is at least one name prefixed with two dashes, the first one given is used as the name.
-
The first name prefixed with one dash is used otherwise.
@click.command()
@click.option('-s', '--string-to-echo')
def echo(string_to_echo):
click.echo(string_to_echo)
@click.command()
@click.option('-s', '--string-to-echo', 'string')
def echo(string):
click.echo(string)
可以用單-
,也可用雙--
,也可以不用,三者會選擇一個作爲最終變量
“-f”, “–foo-bar”, the name is foo_bar
“-x”, the name is x
“-f”, “–filename”, “dest”, the name is dest
“–CamelCase”, the name is camelcase
“-f”, “-fb”, the name is f
“–f”, “–foo-bar”, the name is f
“—f”, the name is _f
Basic Value Options
default
來設置默認值
@click.command()
@click.option('--n', default=1)
def dots(n):
click.echo('.' * n)
requirement
來設置該參數是必須填寫的,type
來設置參數的類型,如果不設置默認是str
# How to make an option required
@click.command()
@click.option('--n', required=True, type=int)
def dots(n):
click.echo('.' * n)
使用_
來避免保留字衝突,from
是python的保留字,所以要使用_
來區別
# How to use a Python reserved word such as `from` as a parameter
@click.command()
@click.option('--from', '-f', 'from_')
@click.option('--to', '-t')
def reserved_param_name(from_, to):
click.echo('from %s to %s' % (from_, to))
通過show_default
參數來默認輸出幫助信息的默認值
@click.command()
@click.option('--n', default=1, show_default=True)
def dots(n):
click.echo('.' * n)
$ dots --help
Usage: dots [OPTIONS]
Options:
--n INTEGER [default: 1]
--help Show this message and exit.
nargs
來傳入多個參數
@click.command()
@click.option('--pos', nargs=2, type=float)
def findme(pos):
click.echo('%s / %s' % pos)
$ findme --pos 2.0 3.0
2.0 / 3.0
元組類型
@click.command()
@click.option('--item', type=(str, int))
def putitem(item):
click.echo('name=%s id=%d' % item)
本質上做了如下的操作
@click.command()
@click.option('--item', nargs=2, type=click.Tuple([str, int]))
def putitem(item):
click.echo('name=%s id=%d' % item)
Multiple Options
@click.command()
@click.option('--message', '-m', multiple=True)
def commit(message):
click.echo('\n'.join(message))
$ commit -m foo -m bar
foo
bar
Counting
Boolean Flags
import sys
@click.command()
@click.option('--shout/--no-shout', default=False)
def info(shout):
rv = sys.platform
if shout:
rv = rv.upper() + '!!!!111'
click.echo(rv)
$ info --shout
LINUX!!!!111
$ info --no-shout
linux
Choice Options
規定值可選
@click.command()
@click.option('--hash-type',
type=click.Choice(['MD5', 'SHA1'], case_sensitive=False))
def digest(hash_type):
click.echo(hash_type)
Prompting
用戶輸入
@click.command()
@click.option('--name', prompt=True)
def hello(name):
click.echo('Hello %s!' % name)
$ hello --name=John
Hello John!
$ hello
Name: John
Hello John!
Password Prompts
Dynamic Defaults for Prompts
Callbacks and Eager Options
將某個參數指定調用的方法
def print_version(ctx, param, value):
if not value or ctx.resilient_parsing:
return
click.echo('Version 1.0')
ctx.exit()
@click.command()
@click.option('--version', is_flag=True, callback=print_version,
expose_value=False, is_eager=True)
def hello():
click.echo('Hello World!')
Yes Parameters
樣用戶確認操作
def abort_if_false(ctx, param, value):
if not value:
ctx.abort()
@click.command()
@click.option('--yes', is_flag=True, callback=abort_if_false,
expose_value=False,
prompt='Are you sure you want to drop the db?')
def dropdb():
click.echo('Dropped all tables!')
Values from Environment Variables
@click.command()
@click.option('--username')
def greet(username):
click.echo('Hello %s!' % username)
if __name__ == '__main__':
greet(auto_envvar_prefix='GREETER')