网络编程
位置:首页>> 网络编程>> Python编程>> python argparse模块通过后台传递参数实例

python argparse模块通过后台传递参数实例

作者:七野  发布时间:2021-05-08 04:13:17 

标签:python,argparse,传递,参数

我就废话不多说了,大家还是直接看代码吧!


cmd.py
# -*- coding: utf-8 -*-
from PySide import QtGui, QtCore
import os,sys
import tory
import argparse
parser = argparse.ArgumentParser()
parser.add_argument("-v", "--verbosity", help="increase output verbosity")
parser.add_argument("-l", "--listdir", help="check root")
args = parser.parse_args()

if args.verbosity:
app = QtGui.QApplication(sys.argv)
trans = tory.Tuopan()
trans.show()
sys.exit(app.exec_())

if args.listdir:
root=os.listdir(args.listdir)
for f in root:
 print f

tory.py
#coding=utf-8
import sys
from PySide import QtGui
from PySide import QtCore
class Tuopan(QtGui.QWidget):
def __init__(self):
 super(Tuopan, self).__init__()
 self.setWindowTitle("Tray!")
 self.resize(200,200)    
if __name__ == '__main__':
app = QtGui.QApplication(sys.argv)
trans = Tuopan()

trans.show()
sys.exit(app.exec_())

随后在cmd执行命令

python [cmd.py] -v 1 运行一个界面
python [cmd.py] -l d:/ 遍历d:/

即可。

python argparse模块通过后台传递参数实例

python argparse模块通过后台传递参数实例

补充知识:Python ConfigParser & argparse模块超粗略小结

Config文件结构

[section0]
option0 = value0
option1 = value1
option2 = value2
[section1]
option0 = value0
option1 = value1
option2 = value2

ConfigParser对象常用方法

ConfigParser.read(filename):读取配置文件。
ConfigParser.sections():返回一个包含所有sections的list。
ConfigParser.options(section):返回包含section中所有options的list。
ConfigParser.items():返回一个list,其中元素为元组(option,value)。
ConfigParser.get(section, option):读取option的具体值,返回str
ConfigParser.getint(section, option):以int类型返回option值。
ConfigParser.add_section(section)
ConfigParser.set(section, option, value):可直接修改现有option
ConfigParser.write(file(filename, 'w'))

argparse

arguparse模块定义了ArgumentParser类


import argparse
parser = argparse.ArgumentParser()
parser.add_argument('-l', dest = 'name', help = 'name value')#添加命令行参数
args = parser.parse_args()#解析命令行参数
name = args.name#获取命令行参数

来源:https://blog.csdn.net/qq_38641985/article/details/83276152

0
投稿

猜你喜欢

手机版 网络编程 asp之家 www.aspxhome.com