跳转到主要内容

一个促进Python中shell和批处理脚本框架

项目描述

简介

BlazeCommandHelper (BCH) 是一个框架,它促进快速创建shell和批处理脚本。它提供了一个核心命令(bch),用于从系统目录、用户目录和已安装的Python包中定位子命令(通过“blazech.commands” setuptools端点)。

它还提供了一个库,用于基于argparse解析器快速创建控制台应用程序。

功能

  • 从多个位置定位插件命令

  • 提供日志功能

  • 提供配置文件功能(需要改进)

目标是提供一个API,促进环境、命令行选项和配置文件之间的交互,类似于 pip

用法

使用easy_install或pip安装BCH。安装完成后,创建一个用于运行第一个命令的文件

# file location:
# *nix: ~/.blazech/command_hwp.py
# windows: %APPDATA%\.blazech\command_hwp.py

# file contents
from blazech.commands import BaseCommand

class Command(BaseCommand):
    name = 'hello-world'
    help = 'say hello'

    def create_arguments(self):
        #self.parser is the argparse parser for this sub-command
        self.parser.add_argument(
            '-n', '--name',
            help='who do you want to say hello to',
            default='world'
        )

    def execute(self, args):
        print 'hello %s' % args.name

来运行

# bch -h
usage: bch [-h] [-v] [-q] {hello-world} ...

positional arguments:
  {hello-world}
    hello-world     say hello
<...snip...>

$ bch hello-world
hello world

$ bch hello-world -n foo
hello foo

替代用法

BCH也可以用作库来帮助快速创建命令行脚本

# setup.py
#<...snip...>
entry_points="""
        [console_scripts]
        fooapp = foopackage.commands:script_entry
    """

运行 python setup.py develop(或安装等)。然后,您需要

# foopackage.commands
from blazech.application import Application
from blazech.commands import BaseCommand

def script_entry():
    app = Application('fooapp')
    app.load_commands(globals())
    # could also use
    # import foopackage.altcommands
    # app.load_commands(foopackage.altcommands)
    app.run_cmd()

class BarCommand(BaseCommand):
    name = 'bar'
    help = 'process bar file'

    def create_arguments(self):
        #self.parser is the argparse parser for this sub-command, see
        # argparser docs for usage
        self.parser.add_argument(
            'fpath',
            help = 'path of the file to process',
        )

    def execute(self, args):
        if args.fpath:
            print 'bar file is: %s' % args.fpath

您应该能够

$ fooapp -h
$ fooapp bar -h
$ fooapp bar ../my/bar/file.txt

问题和评论

请访问: http://groups.google.com/group/blazelibs

当前状态

主要用例对我适用,但由于时间限制,可能进展缓慢。

可以通过 easy_install BlazeCommandHelper==dev 安装开发版本。

变更日志

0.2.0 – 2011.04.29

  • 功能:增加了作为库使用支持

  • 错误:修复了一些与设置相关的错误

  • 错误:修复了Windows驱动器计算错误

  • 错误:重构了测试,所有测试现在都通过

项目详情


由以下支持