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. hibernate学习以及文件以及注释

    <?xml version='1.0' encoding='UTF-8'?><!DOCTYPE hibernate-configuration PUBLIC "-//Hib ...

  2. Codeforces Round #538 (Div. 2) C. Trailing Loves (or L'oeufs?) (分解质因数)

    题目:http://codeforces.com/problemset/problem/1114/C 题意:给你n,m,让你求n!换算成m进制的末尾0的个数是多少(1<n<1e18    ...

  3. jmeter的学习路线

  4. hadoop题目(一)

    一.简要描述如何安装配置一个开源hadoop,只描述即可,列出完整步骤. 答:①创建一个用户和用户组,用来管理hadoop项目:   ②修改确定IP地址:vim /etc/sysconfig/netw ...

  5. Python参数笔记

    Python参数 1. 普通参数 必须要传入和定义时个数相同的参数 def miao(acfun, bilibili): print(acfun, bilibili) miao(11, 22) > ...

  6. jquery评分插件jquery.raty.js

    1.参考链接 官方地址. 教程一 教程二 2.案例1 引入文件: <!-- 评分插件 --> <script type="text/javascript" src ...

  7. c/c++ include 头文件的方式

    在编写c/c++代码时,#include 头文件有两种方式:一个是#include “文件名”,一个是#include <文件名>.区别在于: 前者在程序编译时系统首先在源程序所在的目录( ...

  8. commons-dbcp2 新版本2.6使用连接池在关闭服务器的时候会有内存溢出的BUG....

    这是异常信息.本人使用的mysql8.0数据库驱动版本mysql-connector-java Version 8.0.11,发生这种情况的原因主要是Dbcp2的XBasicDataSource在关闭 ...

  9. 06_java基础知识——break/continue和标签的配合使用

    package com.huawei.test.java03; /** * This is Description * * @author * @date 2018/08/29 */ public c ...

  10. ossfs 使用挂在到ecs -centos 6.8

    # 帮助文档 ```oss 的所有文档都在这个链接上https://promotion.aliyun.com/ntms/act/ossdoclist.html?spm=5176.7933691.744 ...