[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添加其他的命令.可以在腳本中隨意嵌套,這里有一個例子,實現(xiàn)兩個管理數(shù)據(jù)庫的命令
@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()添加參數(shù)
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 標識對參數(shù)名進行標識蔑匣,如果輸入該參數(shù)军援,則會攔截既定的命令行執(zhí)行流程,跳轉去執(zhí)行一個回調函數(shù)
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.
返回當前上下文作為第一個參數(shù)傳遞給對應的方法.
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參數(shù)通過自己的函數(shù),但是sync命令沒辦法接受.sync命令只能接受自己的參數(shù),這讓每一個命令相對獨立,但是一個命令如何傳遞給參數(shù),這個回答就是content
每次一個命令被調用,一個新的上下文被處創(chuàng)建和指向父類上下文.通常,你看不到這些上下文.但是,上下文自動傳遞給參數(shù)和方法.命令可以通過 pass_context()來請求和傳遞上下文.在這種情況下上下文作文第一個參數(shù).
上下文還可以攜帶可以程序指定對象辞州。這意味著您可以構建如下腳本:
@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.
你想讓一個參數(shù)有一個選擇列表.
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