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,900評論 0 23
  • 時間緊,任務重但是我相信你可以完成任務! 目前情況: 法規:看完一遍 管理:看完一遍 經濟:看完一遍 實務:60節...
    sun清風悠揚閱讀 596評論 9 6
  • 人際交往非常現實,人們關注的不是你是誰,而是你能給我什么。你不必為了他人而改變自己,但你也不能一無所有。 這是一個...
    鑒峰閱讀 366評論 0 0
  • 出差來到她的城市,第一時間告訴她,我說想她,想要她,她說很忙,沒有精力。我理解,但我就說了一句:你還依舊愛我,對嗎...
    Kevin_M閱讀 292評論 0 1
  • 人生已經過去半,知道美好的年華老去,不用在曲意逢迎,知道自己責任,也知道自己愛好,只想做自己的喜歡的有意義的事
    東東少閱讀 188評論 0 1