import argparse
import sys parser = argparse.ArgumentParser(description='this is for test.') parser.add_argument("-i", "--app_id", help="Custom app id")
parser.add_argument("-d", "--config_dir", help="Custom config directory")
parser.add_argument("-f", "--config_file", help="Custom config file name "
"(in config directory)")
args = parser.parse_args(sys.argv[1:]) # Set default parameters
app_id = 0000
config_dir = 'test_dir'
config_file = 'test_file' if args.app_id:
app_id = args.app_id if args.config_dir:
config_dir = args.config_dir if args.config_file:
config_file = args.config_file print('APP_ID: {}, CONFIG_DIR: {}, CONFIG_FILE: {}.'.format(app_id, config_dir, config_file))

  

python.exe .\test.py -h
usage: test.py [-h] [-i APP_ID] [-d CONFIG_DIR] [-f CONFIG_FILE]

this is for test.

optional arguments:
-h, --help show this help message and exit
-i APP_ID, --app_id APP_ID
Custom app id
-d CONFIG_DIR, --config_dir CONFIG_DIR
Custom config directory
-f CONFIG_FILE, --config_file CONFIG_FILE
Custom config file name (in config directory)

python.exe .\test.py -i 123 -d dir -f file
APP_ID: 123, CONFIG_DIR: dir, CONFIG_FILE: file.

Python argparse用法的更多相关文章

  1. python argparse用法总结

    转:python argparse用法总结 1. argparse介绍 argparse是python的一个命令行解析包,非常适合用来编写可读性非常好的程序. 2. 基本用法 prog.py是我在li ...

  2. 【转】python argparse用法总结

    转自:https://www.jianshu.com/p/fef2d215b91d 1. argparse介绍 是python的一个命令行解析包,非常编写可读性非常好的程序 2. 基本用法 prog. ...

  3. 转载:python argparse用法总结

    https://www.jianshu.com/p/fef2d215b91d 1. argparse介绍 是python的一个命令行解析包,非常编写可读性非常好的程序 2. 基本用法 prog.py是 ...

  4. Python argparse 用法总结

    使用argparse的四个基本步骤 创建 ArgumentParser() 对象 调用 add_argument() 方法添加参数 使用 parse_args() 解析添加的参数 # example ...

  5. Python argparse 处理命令行小结

    Python argparse 处理命令行小结 1. 关于argparse是python的一个命令行解析包,主要用于处理命令行参数 2. 基本用法test.py是测试文件,其内容如下: import ...

  6. python argparse库

    argparse用法总结 https://blog.csdn.net/qq_24551305/article/details/90155858 args = parse.parse_args()par ...

  7. Python高级用法总结

    Python很棒,它有很多高级用法值得细细思索,学习使用.本文将根据日常使用,总结介绍Python的一组高级特性,包括:列表推导式.迭代器和生成器.装饰器. 列表推导(list comprehensi ...

  8. Python argparse 模块

    Python argparse 模块 test.py: import argparse argparser = argparse.ArgumentParser(add_help=False) argp ...

  9. python argparse sys.argv

    python argparse sys.argv class WeiLearningArgumentParser(argparse.ArgumentParser): def __init__(self ...

随机推荐

  1. 简述我理解的C#

    第一章:开发入门 一.基础知识    1.计算机语言发展进程:计算机语言大致经过了机器语言.汇编语言.高级语言三个阶段的发展历程. 汇编语言:使用助记符来替代机器指令机器语言这种反人类的设计,阻碍了软 ...

  2. RAID阵列盘有一块状态变为外来处理方法

    感谢: https://blog.csdn.net/cmzsteven/article/details/63680933

  3. Linux下数据库备份

    1.登录数据库服务器并切换到数据库用户下 [root@*** ~]# su - oracle 2.测试用户名密码是否能正确连接数据库 [oracle@*** ~]$ sqlplus username/ ...

  4. 解决Visual C++ for Linux: -L"~/projects/path_to_lib_folder" 无法设置library search path的问题

    最近倒腾Linux C/C++项目.以目前的情况来说,要生成编译(build)一个Linux工程脚本,首选的工具必定是CMake.这也是我之前Linux项目的首选.不过自从VS IDE支持Linux ...

  5. Thread线程join方法自我理解

    Thread线程join方法自我理解 thread.join():等待thread线程运行终止,指的是main-thread(main线程)必须等待thread线程运行结束,才能继续thread.jo ...

  6. ajax csrf

    data 里加 csrfmiddlewaretoken:$("[name='csrfmiddlewaretoken']").val(), 还要再 form表单里加{% csrf_t ...

  7. this与super

    this是引用,表示当前对象,堆中每一个对象都有this,保存的地址指向自身:super不是引用,是表示当前对象的父类特征. this可以使用在构造方法中,即this(..),必须出现在代码第一行,代 ...

  8. 18-09-11 软件rpm yum rm卸载 和批量删除

    一 在Linux下删除文件用rm命令,具体用法如下: rm [选项] 文件 选项说明: -f -force 忽略不存在的文件,强制删除,无任何提示 -i --interactive 进行交互式地删除 ...

  9. Linux中彻底删除Google-Chrome浏览器

    sudo apt-get autoremove --purge google-chrome-stable   卸载chrome后, 删除-/.config/google-chrome,重新安装.

  10. PyQt5 -pycharm 环境搭建

    1.安装PyQt5 在CMD窗口执行命令: pip3 install PyQt5 安装 pyqt_toools pip3 install PyQt5-tools 2.配置PyCharm 1)打开PyC ...