zoukankan      html  css  js  c++  java
  • Flask-Script Manager

    Flask-Script Manager

    Flask Script和Flask本身的工作方式类似,只需定义和添加从命令行中被Manager实例调用的命令;

    1 创建并运行命令

    首先,创建一个Python模板运行命令脚本,可起名为manager.py;
    在该文件中,必须有一个Manager实例,Manager类追踪所有在命令行中调用的命令和处理过程的调用运行情况;

    Manager只有一个参数——Flask实例,也可以是一个函数或其他的返回Flask实例;
    调用 manager.run() 启动 Manager 实例接收命令行中的命令;

    #-*-coding:utf8-*-  
    from flask_script import Manager  
    from debug import app  
      
    manager = Manager(app)  
      
    if __name__ == '__main__':  
        manager.run()  
    

    其次,创建并加入命令;
    有三种方法创建命令,即创建Command子类、使用@command修饰符、使用@option修饰符;

    第一种——创建Command子类
    Command子类必须定义一个run方法;
    举例:创建Hello命令,并将Hello命令加入Manager实例;

    #-*-coding:utf8-*-  
    from flask_script import Manager  
    from flask_script import Command  
    from debug import app  
      
    manager = Manager(app)  
      
    class Hello(Command):  
        'hello world'  
        def run(self):  
            print 'hello world'  
      
    manager.add_command('hello', Hello())  
      
    if __name__ == '__main__':  
        manager.run()  
    

    执行如下命令:

    python manager.py hello
    > hello world
    

    第二种——使用Command实例的@command修饰符

    #-*-coding:utf8-*-  
    from flask_script import Manager  
    from debug import app  
      
    manager = Manager(app)  
     
    @manager.command  
    def hello():  
        'hello world'  
        print 'hello world'  
      
    if __name__ == '__main__':  
        manager.run()  
    

    该方法创建命令的运行方式和Command类创建的运行方式相同;

    python manager.py hello
    > hello world
    

    第三种——使用Command实例的@option修饰符
    复杂情况下,建议使用@option;
    可以有多个@option选项参数;

    #-*-coding:utf8-*-  
    from flask_script import Manager  
    from debug import app  
      
    manager = Manager(app)  
     
    @manager.option('-n', '--name', dest='name', help='Your name', default='world')  
    @manager.option('-u', '--url', dest='url', default='www.csdn.com')  
    def hello(name, url):  
        'hello world or hello <setting name>'  
        print 'hello', name  
        print url  
      
    if __name__ == '__main__':  
        manager.run()  
    

    运行方式如下:

    python manager.py hello
    >hello world
    >www.csdn.com
    
    python manager.py hello -n sissiy -u www.sissiy.com
    > hello sissiy
    >www.sissiy.com
    
  • 相关阅读:
    谈谈对程序猿的管理
    OFMessageDecoder 分析
    [LeetCode-21]Construct Binary Tree from Preorder and Inorder Traversal
    leetcode第一刷_Rotate Image
    [二次开发]dede文章页面怎样显示作者的头像
    MapReduceTopK TreeMap
    安卓3d引擎
    LeetCode::Sort List 具体分析
    杨帆之工作日志-2014.6.24
    CF1109F Sasha and Algorithm of Silence's Sounds
  • 原文地址:https://www.cnblogs.com/kai-/p/12539122.html
Copyright © 2011-2022 走看看