Python argparse用法
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用法的更多相关文章
- python argparse用法总结
转:python argparse用法总结 1. argparse介绍 argparse是python的一个命令行解析包,非常适合用来编写可读性非常好的程序. 2. 基本用法 prog.py是我在li ...
- 【转】python argparse用法总结
转自:https://www.jianshu.com/p/fef2d215b91d 1. argparse介绍 是python的一个命令行解析包,非常编写可读性非常好的程序 2. 基本用法 prog. ...
- 转载:python argparse用法总结
https://www.jianshu.com/p/fef2d215b91d 1. argparse介绍 是python的一个命令行解析包,非常编写可读性非常好的程序 2. 基本用法 prog.py是 ...
- Python argparse 用法总结
使用argparse的四个基本步骤 创建 ArgumentParser() 对象 调用 add_argument() 方法添加参数 使用 parse_args() 解析添加的参数 # example ...
- Python argparse 处理命令行小结
Python argparse 处理命令行小结 1. 关于argparse是python的一个命令行解析包,主要用于处理命令行参数 2. 基本用法test.py是测试文件,其内容如下: import ...
- python argparse库
argparse用法总结 https://blog.csdn.net/qq_24551305/article/details/90155858 args = parse.parse_args()par ...
- Python高级用法总结
Python很棒,它有很多高级用法值得细细思索,学习使用.本文将根据日常使用,总结介绍Python的一组高级特性,包括:列表推导式.迭代器和生成器.装饰器. 列表推导(list comprehensi ...
- Python argparse 模块
Python argparse 模块 test.py: import argparse argparser = argparse.ArgumentParser(add_help=False) argp ...
- python argparse sys.argv
python argparse sys.argv class WeiLearningArgumentParser(argparse.ArgumentParser): def __init__(self ...
随机推荐
- Java与C/C++有什么区别?
(1)Java为解释型语言,其运行过程为:程序源代码经过Java编译器编译成字节码,然后由JVM解释执行.而C/C++为编译型语言,源代码经过编译和链接生成可执行的二进制代码,因此,Java的执行速度 ...
- 判断浏览器大于等于ie9
方便自己查找 <script> (function (window) { var theUA = window.navigator.userAgent.toLowerCase(); if ...
- 牛客练习赛 43 B-Tachibana Kanade Loves Probability
链接:https://ac.nowcoder.com/acm/contest/548/B 题目描述 立华奏在学习初中数学的时候遇到了这样一道大水题: “设箱子内有 n 个球,其中给 m 个球打上标记, ...
- python网络编程(UDP+广播)
UDP广播案例,一端发送,多端接受: 发送端: # UDP广播案例 from socket import * from time import sleep # 设定目标地址 dest=('176.21 ...
- Javascript学习二---DOM元素操作
Javascript 主要包括:JS的语法,DOM和BOM操作以及ECMAScript语法. 1 获取元素的方法 获取元素方法: 通过ID:document.getElementById(); 一个 ...
- hive防止数据被误删除
1.HDFS层面开启trash功能(fs.trash.interval) 被删除的数据在HDFS中的/user/$USER/.Trash目录中,开启这个功能秩序要将配置属性fs.trash.inter ...
- Matlab_ eval 批量定义变量
本文备忘 Matlab 中批量定义变量的方法. 参考: [1] matlab中批量赋值与声明变量(eval函数) [2] MathWorks eval 缘起与说明 当变量名称多到一定程度时,变量名的管 ...
- 剑指Offer 62. 二叉搜索树的第k个结点 (二叉搜索树)
题目描述 给定一棵二叉搜索树,请找出其中的第k小的结点.例如, (5,3,7,2,4,6,8) 中,按结点数值大小顺序第三小结点的值为4. 例如, 5 / \ 3 7 / \ / \ 2 4 6 ...
- Centos中MySQL数据的备份和恢复
1.MySQL数据备份 MySQL在Centos备份中用到了mysqldump这个文件,首先得把它找出来: [root@instance-3snz20bz ~]# whereis mysqldump ...
- Axure10种非交互功能简介(引自人人都是产品经理)
一.notes:控件和页面注释 越来越多的PM开始用Axure来写PRD,但行内并不存在约定成俗的文档规范.作者目前为止见过的Axure版PRD中,大部分采用原型+旁边文字标注的方法来表达产品逻辑.其 ...