命令行接口框架

cmdstyle的Python项目详细描述


注意:

此项目已被放弃且不受支持。 请改用argparsedocopt


命令样式

CmdStyle是一个库,用于使用命令创建命令行接口 程序使用的样式,如svnhg

示例

示例文件:

#! /usr/bin/env python3.2

import sys

import cmdstyle

class HelloCommand(cmdstyle.Command):
    'Print a greeting'

    name = cmdstyle.PosParam()
    shout = cmdstyle.Flag(
        description='Print the greeting in upper case',
        shortname='')
    exclamations = cmdstyle.Option(
        description='The number of exclamation marks used to end the '
                    'greeting',
        default=1)

    def __call__(self):
        #Note that the library does not do validations
        exclamations_int = int(self.exclamations)

        greeting = 'Hello'
        if self.name:
            greeting += ', ' + self.name
        if self.shout:
            greeting = greeting.upper()
        greeting += exclamations_int * '!'

        print(greeting)


class ByeCommand(cmdstyle.Command):
    'Print bye'

    def __call__(self):
        print('Bye!')


program = cmdstyle.Program(
    'sample.py',
    'Sample Program',
    'Just a sample program',
    'http://...')

program.register(HelloCommand())
program.register(ByeCommand())

if __name__ == '__main__':
    program.run(sys.argv)

使用的样本:

$ ./sample.py
Use `sample.py help` for usage.


$ ./sample.py help
usage: sample.py <command> [options] [args]

Sample Program
Just a sample program

Available commands:
  bye
  hello

Use `sample.py help <command>` for help on a specific command.
See http://... for additional information.


$ ./sample.py help hello
hello: Print a greeting
usage: hello NAME
Options:

   --shout             Print the greeting in upper case
-e --exclamations ARG  The number of exclamation marks used to end the greeting


$ ./sample.py hello
Hello!


$ ./sample.py hello --shout --exclamations 3
HELLO!!!

欢迎加入QQ群-->: 979659372 Python中文网_新手群

推荐PyPI第三方库


热门话题
java编辑并重新运行spring引导单元测试,无需重新加载上下文即可加快测试速度   为什么我不能做演员?   java为什么是线程。join通常用于停止安卓中的线程   java从weblogic服务器调用JSON POST REST服务时收到400:错误请求   java在DeviceAdmin模式禁用时设置身份验证?   java SortedMap的keySet()能否始终安全地强制转换到SortedSet?   安卓 java。lang.NoSuchMethodException可包裹类   java JOGL库安装   javatomcat内存管理   java使用getString()中的变量   java将最小星号设置为评级栏   Java中字符串相等的println()方法。。。它到底是如何工作的?   java如何从文本中输出的数组中放入随机图像