pyspider源代码-run.py click模块

[run文档]https://github.com/binux/pyspider/blob/master/pyspider/run.py

import os
import sys
import six
import copy
import time
import shutil
import logging
import logging.config
import click
import pyspider

click

click.group

Commands can be attached to other commands of type Group
. This allows arbitrary nesting of scripts. As an example here is a script that implements two commands for managing databases
命令可以通过group添加其他的命令.可以在脚本中随意嵌套,这里有一个例子,实现两个管理数据库的命令

@click.group()
def cli():
    pass

@click.command()
def initdb():
    click.echo('Initialized the database')

@click.command()
def dropdb():
    click.echo('Dropped the database')

cli.add_command(initdb)
cli.add_command(dropdb)

可以简写为

@click.group()
def cli():
    pass

@cli.command()
def initdb():
    click.echo('Initialized the database')

@cli.command()
def dropdb():
    click.echo('Dropped the database')
if __name__ == '__main__':
    cli()
>>>python text.py  dropdb
Dropped the database

click.option

To add parameters, use the option()
and argument()
decorators
使用option()和argument()添加参数

import click


@click.command()
@click.option('--count', default=1, help='number of greetings')
@click.argument('name')
def hello(count, name):
    for x in range(count):
        click.echo('Hello %s!' % name)
if __name__ == '__main__':
    hello()
>>>python text.py xxx --count=2
Hello xxx!
Hello xxx!

click.version_option

Adds a --version option which immediately ends the program printing out the version number. This is implemented as an eager option that prints the version and exits the program in the callback.
添加一个--version选项,让程序在打印出version之后立即退出,Click 提供 eager 标识对参数名进行标识,如果输入该参数,则会拦截既定的命令行执行流程,跳转去执行一个回调函数

import click


@click.command()
@click.option('--count', default=1, help='number of greetings')
@click.argument('name')
@click.version_option(version='1', prog_name='pyspider')#添加version_option选项
def hello(count, name):
    for x in range(count):
        click.echo('Hello %s!' % name)
if __name__ == '__main__':
    hello()
>>>python text.py xxx --count=2 --version 1
pyspider, version 1

click.pass_context

更新
Marks a callback as wanting to receive the current context object as first argument.

返回当前上下文作为第一个参数传递给对应的方法.

import click
@click.command()
@click.option('--count', default=1, help='Number of greetings.')
@click.option('--name', prompt='Your name',
              help='The person to greet.')
@click.pass_context
def hello(ctx,**kwargs):
    click.echo(ctx)
    click.echo(kwargs)
  

if __name__ == '__main__':
    hello()

run

>>> python test.py 
Your name: aaa
<click.core.Context object at 0x7f74d2cf0210>
{'count': 1, 'name': u'aaa'}

As you can see from the earlier example, the basic command group accepts a debug argument which is passed to its callback, but not to the sync command itself. The sync command only accepts its own arguments.
This allows tools to act completely independent of each other, but how does one command talk to a nested one? The answer to this is the Context
.
Each time a command is invoked, a new context is created and linked with the parent context. Normally, you can’t see these contexts, but they are there. Contexts are passed to parameter callbacks together with the value automatically. Commands can also ask for the context to be passed by marking themselves with the pass_context()
decorator. In that case, the context is passed as first argument.
The context can also carry a program specified object that can be used for the program’s purposes. What this means is that you can build a script like this:
上一个比较简单的版本,这一组命令接受debug参数通过自己的函数,但是sync命令没办法接受.sync命令只能接受自己的参数,这让每一个命令相对独立,但是一个命令如何传递给参数,这个回答就是content

每次一个命令被调用,一个新的上下文被处创建和指向父类上下文.通常,你看不到这些上下文.但是,上下文自动传递给参数和方法.命令可以通过 pass_context()来请求和传递上下文.在这种情况下上下文作文第一个参数.

上下文还可以携带可以程序指定对象。这意味着您可以构建如下脚本:

@click.group()
@click.option('--debug/--no-debug', default=False)
@click.pass_context
def cli(ctx, debug):
    ctx.obj['DEBUG'] = debug

@cli.command()
@click.pass_context
def sync(ctx):
    click.echo('Debug is %s' % (ctx.obj['DEBUG'] and 'on' or 'off'))

if __name__ == '__main__':
    cli(obj={})

click.Choice

Sometimes, you want to have a parameter be a choice of a list of values. In that case you can use Choice
type. It can be instantiated with a list of valid values.

你想让一个参数有一个选择列表.

import click
@click.command()
@click.option('--hash-type', type=click.Choice(['md5', 'sha1']))
def digest(hash_type):
    click.echo(hash_type)
if __name__ == '__main__':
    
    digest()
python text.py --hash-type md5
md5

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • **2014真题Directions:Read the following text. Choose the be...
    又是夜半惊坐起阅读 9,921评论 0 23
  • 时间紧,任务重但是我相信你可以完成任务! 目前情况: 法规:看完一遍 管理:看完一遍 经济:看完一遍 实务:60节...
    sun清风悠扬阅读 596评论 9 6
  • 人际交往非常现实,人们关注的不是你是谁,而是你能给我什么。你不必为了他人而改变自己,但你也不能一无所有。 这是一个...
    鉴峰阅读 367评论 0 0
  • 出差来到她的城市,第一时间告诉她,我说想她,想要她,她说很忙,没有精力。我理解,但我就说了一句:你还依旧爱我,对吗...
    Kevin_M阅读 292评论 0 1
  • 人生已经过去半,知道美好的年华老去,不用在曲意逢迎,知道自己责任,也知道自己爱好,只想做自己的喜欢的有意义的事
    东东少阅读 188评论 0 1