click option

https://click.palletsprojects.com/en/7.x/options/

Name Your Options

A name is chosen in the following order

  1. 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.

  2. If there is at least one name prefixed with two dashes, the first one given is used as the name.

  3. 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')
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值