Python语言写CLI命令行程序
小标 2018-11-14 来源 : 阅读 730 评论 0

摘要:本文主要向大家介绍了Python语言写CLI命令行程序,通过具体的内容向大家展示,希望对大家学习Python语言有所帮助。

本文主要向大家介绍了Python语言写CLI命令行程序,通过具体的内容向大家展示,希望对大家学习Python语言有所帮助。

使用Python写命令行程序,以argparse是基础,但是有两个更好的工具可以选择,click和oslo.config 
click
click可以用于简单的命令行程序,下面是我写的一个demo
https://github.com/xiaopeng163/click-demo 
$ cd click-demo
$ python setup.py install
$ clickctl
Usage: clickctl [OPTIONS] COMMAND [ARGS]...

  Click Demo Command Line Interface

Options:
  -v, --verbose  show debug message.
  --help         Show this message and exit.

Commands:
  init    Initializes a controller cluster on master node.
  join    join the controller cluster as agent node
  status  Get cluster node list
$ clickctl init --help
Usage: clickctl init [OPTIONS]

Options:
  --advertise-addr TEXT  The REST Server advertise address  [required]
  --help                 Show this message and exit.

$ clickctl init --advertise-addr=1.1.1.1:80
Try to initialized the cluster
$ clickctl -v init --advertise-addr=1.1.1.1:80
Try to initialized the cluster
The REST Server advertise address: 1.1.1.1:80
oslo.config
可以用于大型复杂命令行程序的开发,特别是命令行参数和ini格式配置文件同时结合使用的命令行程序
我在yabgp/yabmp里使用了oslo.config, 命令行的效果如下:
python bin/yabmpd -h
usage: yabmpd [-h] [--config-dir DIR] [--config-file PATH]
              [--log-backup-count LOG_BACKUP_COUNT]
              [--log-config-file LOG_CONFIG_FILE] [--log-dir LOG_DIR]
              [--log-file LOG_FILE] [--log-file-mode LOG_FILE_MODE]
              [--nouse-stderr] [--noverbose] [--use-stderr] [--verbose]
              [--version] [--bmp-bind_host BMP_BIND_HOST]
              [--bmp-bind_port BMP_BIND_PORT] [--bmp-write_dir BMP_WRITE_DIR]
              [--bmp-write_msg_max_size BMP_WRITE_MSG_MAX_SIZE]

optional arguments:
  -h, --help            show this help message and exit
  --config-dir DIR      Path to a config directory to pull *.conf files from.
                        This file set is sorted, so as to provide a
                        predictable parse order if individual options are
                        over-ridden. The set is parsed after the file(s)
                        specified via previous --config-file, arguments hence
                        over-ridden options in the directory take precedence.
  --config-file PATH    Path to a config file to use. Multiple config files
                        can be specified, with values in later files taking
                        precedence. Defaults to None.
  --log-backup-count LOG_BACKUP_COUNT
                        the number of backup log file
  --log-config-file LOG_CONFIG_FILE
                        Path to a logging config file to use
  --log-dir LOG_DIR     log file directory
  --log-file LOG_FILE   log file name
  --log-file-mode LOG_FILE_MODE
                        default log file permission
  --nouse-stderr        The inverse of --use-stderr
  --noverbose           The inverse of --verbose
  --use-stderr          log to standard error
  --verbose             show debug output
  --version             show program's version number and exit

bmp options:
  --bmp-bind_host BMP_BIND_HOST
                        Address to bind the BMP server to
  --bmp-bind_port BMP_BIND_PORT
                        Port the bind the BMP server to
  --bmp-write_dir BMP_WRITE_DIR
                        The BMP messages storage path
环境变量
另外因为如今容器非常流行,对于容器应用程序的配置,命令行不是很方便,一般采用环境变量的传递方式,如
os.environ.get('MONGODB_URL', 'mongodb://127.0.0.1:27017')    

本文由职坐标整理并发布,希望对同学们学习Python有所帮助,更多内容请关注职坐标编程语言Python频道!

本文由 @小标 发布于职坐标。未经许可,禁止转载。
喜欢 | 1 不喜欢 | 0
看完这篇文章有何感觉?已经有1人表态,100%的人喜欢 快给朋友分享吧~
评论(0)
后参与评论

您输入的评论内容中包含违禁敏感词

我知道了

助您圆梦职场 匹配合适岗位
验证码手机号,获得海同独家IT培训资料
选择就业方向:
人工智能物联网
大数据开发/分析
人工智能Python
Java全栈开发
WEB前端+H5

请输入正确的手机号码

请输入正确的验证码

获取验证码

您今天的短信下发次数太多了,明天再试试吧!

提交

我们会在第一时间安排职业规划师联系您!

您也可以联系我们的职业规划师咨询:

小职老师的微信号:z_zhizuobiao
小职老师的微信号:z_zhizuobiao

版权所有 职坐标-一站式IT培训就业服务领导者 沪ICP备13042190号-4
上海海同信息科技有限公司 Copyright ©2015 www.zhizuobiao.com,All Rights Reserved.
 沪公网安备 31011502005948号    

©2015 www.zhizuobiao.com All Rights Reserved

208小时内训课程