美文网首页
Python 的 Click 模块

Python 的 Click 模块

作者: dasdadf | 来源:发表于2019-04-09 23:06 被阅读0次
Click

简介

Click 官方文档
argparse 模块相比,更直观,更直观,并且支持 nesting commands
类似于 git 那种,除了这个特性支持的比较好,没觉得简洁到哪里去⊙﹏⊙

Click is a Python package for creating beautiful command line interfaces in a composable way with as little code as necessary. It’s the “Command Line Interface Creation Kit”. It’s highly configurable but comes with sensible defaults out of the box.

It aims to make the process of writing command line tools quick and fun while also preventing any frustration caused by the inability to implement an intended CLI API.

Click in three points:

  • arbitrary nesting of commands
  • automatic help page generation
  • supports lazy loading of subcommands at runtime

模块的使用

-h参数的支持(同@click.group通用)

CONTEXT_SETTINGS = dict(help_option_names=['-h', '--help'])

@click.command(context_settings=CONTEXT_SETTINGS)
def cli():
    pass

添加参数

argumentoption的区别

click supports two types of parameters for scripts: options and arguments. There is generally some confusion among authors of command line scripts of when to use which, so here is a quick overview of the differences. As its name indicates, an option is optional. While arguments can be optional within reason, they are much more restricted in how optional they can be.

To help you decide between options and arguments, the recommendation is to use arguments exclusively for things like going to subcommands or input filenames / URLs, and have everything else be an option instead.

添加argument(@click.argument)

# 我是真心不喜欢这种,没有办法添加help~限制又多
@click.argument('filename',
                type=click.File('r'))

添加option(@click.option)

@click.option('-f', '-fasta', 'file_name',
              type=click.File('r'),
              required=True,
              help=u'The fasta file input.')

添加密码参数

# 如果输入 -p 则传入其后的参数
# 如果没有输入 -p,则提示输入
@click.option('-p', '--password',
              prompt=True, hide_input=True,
              help='The password for email')

高亮显示

click.secho(
    '**** The failed files are stored at {} ****'.format(file_failed),
    fg='red')

暂时到这里

相关文章

  • python Click 模块的使用

    python Click 模块的使用 https://click.palletsprojects.com/en/7...

  • Python爬虫下载知乎视频

    开发工具 Python版本:3.6.4 相关模块: PIL模块; requests模块; click模块; 以及一...

  • python click模块

    python click模块用于编写命令行程序,它的目的是使用更少的代码,加快编写CLI程序的速度。 安装 cli...

  • Python 的 Click 模块

    简介 Click 官方文档同 argparse 模块相比,更直观,更直观,并且支持 nesting command...

  • Python-Click模块学习

    Click基础学习 1.函数变cli @click.command() 2.不同的command实例可以关联到gr...

  • click模块

    使用 @click.command() 装饰一个函数,使之成为命令行接口;使用 @click.option() 装...

  • Python Click

    Python Click 介绍 最近发现一个非常nice的python库: Click, 对热衷于写脚本的"Scr...

  • click模块用法

    click 模块用法.就是代替命令行. 参考运行结果. prompt='Your name', 这个会在命令行提示...

  • Python常用模块

    Python常用模块之time模块 Python常用模块之os模块 Python常用模块之sys模块 Python...

  • Python超好用的命令行界面实现工具

    Click 是一个简洁好用的Python模块,它能用尽量少的代码实现漂亮的命令行界面。它不仅开箱即用、还能支持高度...

网友评论

      本文标题:Python 的 Click 模块

      本文链接:https://www.haomeiwen.com/subject/eipqiqtx.html