python之Click的简单应用
<h3>一、介绍</h3><p>Click是一个Python包,用于以可组合的方式创建漂亮的命令行界面,只需要很少的代码。这是“命令行界面创建工具包”。它具有高度可配置性,但具有开箱即用的合理默认值。</p>
<p>安装:</p>
<div class="cnblogs_code">
<pre>pip install click</pre>
</div>
<p>官方文档(7.x版本)</p>
<p> </p>
<h3>二、使用</h3>
<div class="cnblogs_code">
<pre><span style="color: rgba(0, 0, 0, 1)">简单的使用步骤:
@click.command() 装饰一个函数,使之成为命令行接口;
@click.option() 装饰函数,为其添加命令行选项等。</span></pre>
</div>
<p> </p>
<p>官方示例</p>
<div class="cnblogs_code">
<pre><span style="color: rgba(0, 0, 255, 1)"><span style="color: rgba(0, 128, 0, 1)"># 此文件名为hello.py<br></span><br>import</span><span style="color: rgba(0, 0, 0, 1)"> click
@click.command()
@click.option(</span><span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">--count</span><span style="color: rgba(128, 0, 0, 1)">'</span>, default=1, help=<span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">Number of greetings.</span><span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(0, 0, 0, 1)">)
@click.option(</span><span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">--name</span><span style="color: rgba(128, 0, 0, 1)">'</span>, prompt=<span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">Your name</span><span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(0, 0, 0, 1)">,
help</span>=<span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">The person to greet.</span><span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(0, 0, 0, 1)">)
</span><span style="color: rgba(0, 0, 255, 1)">def</span><span style="color: rgba(0, 0, 0, 1)"> hello(count, name):
</span><span style="color: rgba(128, 0, 0, 1)">"""</span><span style="color: rgba(128, 0, 0, 1)">Simple program that greets NAME for a total of COUNT times.</span><span style="color: rgba(128, 0, 0, 1)">"""</span>
<span style="color: rgba(0, 0, 255, 1)">for</span> x <span style="color: rgba(0, 0, 255, 1)">in</span><span style="color: rgba(0, 0, 0, 1)"> range(count):
click.echo(</span><span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">Hello %s!</span><span style="color: rgba(128, 0, 0, 1)">'</span> %<span style="color: rgba(0, 0, 0, 1)"> name)
</span><span style="color: rgba(0, 0, 255, 1)">if</span> <span style="color: rgba(128, 0, 128, 1)">__name__</span> == <span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">__main__</span><span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(0, 0, 0, 1)">:
hello()</span></pre>
</div>
<p>在上面的例子中,函数hello接受两个参数,分别是count和name,他们的取值从命令行中获取,这里我们使用了click模块中的command、option、echo,他们的作用如下:</p>
<p>command:使函数hello成为命令行接口<br>option:增加命令行选项<br>echo:输出结果,使用echo进行输出是为了更好的兼容性,因为python 2中的print是个语句,python 3中的print 是一个函数<br>运行上面的脚本,可以通过命令指定--name,--count的值,由于我们在option中指定了prompt选项,那么如果我们执行脚本没有传递name这个参数时,Click会提示我们在交互模式下输入。</p>
<p><span>运行时的样子:</span></p>
<div class="highlight-text notranslate">
<div class="highlight">
<pre>$ python hello.py --count=3
Your name: John
Hello John!
Hello John!
Hello John!</pre>
</div>
</div>
<p> </p>
<p> </p>
<p><span>它会自动生成格式良好的帮助页面:</span></p>
<div class="highlight-text notranslate">
<div class="highlight">
<pre>$ python hello.py --help
Usage: hello
Simple program that greets NAME for a total of COUNT times.
Options:
--count INTEGERNumber of greetings.
--name TEXT The person to greet.
--help Show this message and exit.</pre>
</div>
</div>
<p> </p>
<h3>三、其他参数</h3>
<h4>1、option的参数</h4>
<p>option最基本的用法就是通过指定命令行选项的名称,从命令行读取参数值,再将其传递给函数。option常用的参数含义:</p>
<ul>
<li>default: 设置命令行参数的默认值</li>
<li>help:参数说明</li>
<li>type:参数类型,可以是str、int、float等</li>
<li>prompt:当在命令行中没有输入相应的参数时,会更具prompt提示用户输入</li>
<li>nargs:指定命令行参数接受的值的个数</li>
<li>required:是否为必填参数</li>
</ul>
<div class="cnblogs_code">
<pre><span style="color: rgba(0, 0, 255, 1)">import</span><span style="color: rgba(0, 0, 0, 1)"> click
@click.command()
@click.option(</span><span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">--desc</span><span style="color: rgba(128, 0, 0, 1)">'</span>, nargs=2, type=str)<span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)"> nargs确定参数个数,变量值会以tuple形式传入函数</span>
<span style="color: rgba(0, 0, 255, 1)">def</span><span style="color: rgba(0, 0, 0, 1)"> hello(desc):
click.echo(desc)
click.echo(</span><span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">Hello %s %s</span><span style="color: rgba(128, 0, 0, 1)">'</span> %<span style="color: rgba(0, 0, 0, 1)"> desc)
</span><span style="color: rgba(0, 0, 255, 1)">if</span> <span style="color: rgba(128, 0, 128, 1)">__name__</span> == <span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">__main__</span><span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(0, 0, 0, 1)">:
hello()
</span><span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)"> 执行</span>
python hello.py --desc <span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">帅哥</span><span style="color: rgba(128, 0, 0, 1)">"</span> <span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">xx</span><span style="color: rgba(128, 0, 0, 1)">"</span>
<span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)"> 结果</span>
(<span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">帅哥</span><span style="color: rgba(128, 0, 0, 1)">'</span>, <span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">xx</span><span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(0, 0, 0, 1)">)
Hello 帅哥 xx</span></pre>
</div>
<p> </p>
<h3>四、扩展用法</h3>
<h4>1、限定用户从选项列表中选择输入</h4>
<div class="cnblogs_code">
<pre><span style="color: rgba(0, 0, 255, 1)">import</span><span style="color: rgba(0, 0, 0, 1)"> click
@click.command()
@click.option(</span><span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">--sex</span><span style="color: rgba(128, 0, 0, 1)">'</span>, required=True, type=click.Choice([<span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">male</span><span style="color: rgba(128, 0, 0, 1)">'</span>, <span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">female</span><span style="color: rgba(128, 0, 0, 1)">'</span>]), prompt=<span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">你的性别</span><span style="color: rgba(128, 0, 0, 1)">'</span>)<span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)"> 限定-c的值为start,或者stop,required表示是否为必填参数</span>
<span style="color: rgba(0, 0, 255, 1)">def</span><span style="color: rgba(0, 0, 0, 1)"> set_sex(sex):
click.echo(</span><span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">你的性别是%s</span><span style="color: rgba(128, 0, 0, 1)">'</span> %<span style="color: rgba(0, 0, 0, 1)"> sex)
</span><span style="color: rgba(0, 0, 255, 1)">if</span> <span style="color: rgba(128, 0, 128, 1)">__name__</span> == <span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">__main__</span><span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(0, 0, 0, 1)">:
set_sex()
</span><span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)"> 1.输入参数</span>
python test.py --<span style="color: rgba(0, 0, 0, 1)">sex male
你的性别是male
</span><span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)"> 2.不输入参数</span>
<span style="color: rgba(0, 0, 0, 1)">python test.py
你的性别 (male, female): male
你的性别是male</span></pre>
</div>
<p> </p><br><br>
来源:https://www.cnblogs.com/Zzbj/p/11309130.html
頁:
[1]