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

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 220,639评论 6 513
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 94,093评论 3 396
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 167,079评论 0 357
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 59,329评论 1 295
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 68,343评论 6 397
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 52,047评论 1 308
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 40,645评论 3 421
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 39,565评论 0 276
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 46,095评论 1 319
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 38,201评论 3 340
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 40,338评论 1 352
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 36,014评论 5 347
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 41,701评论 3 332
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 32,194评论 0 23
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 33,320评论 1 272
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 48,685评论 3 375
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 45,345评论 2 358

推荐阅读更多精彩内容

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