命令行参数读取和解析

 1 from argparse import ArgumentParser
 2
 3 p = ArgumentParser(description=‘eg:  $python mm_crawler -n 8 -o "D:/mm_pics" -l 500  4                                 this command means that your app will create 8 coroutines to download 500 pictures, and save them to D:/mm_pics‘)
 5 p.add_argument("-n", type = int, help="Set number of coroutines (default:10)", default=10)
 6 p.add_argument("-o", type = str, help=‘Set pictures save path (default:./pics/)‘, default="./pics/")
 7 p.add_argument("-l", type = int, help="number of limits of picture to crawl (default:0, no limit)", default=0)
 8
 9 # p.print_help()
10 args = p.parse_args()
11 args = vars(args)
12
13 #把这些值写到conf.py中
14 with open("conf.py",‘wb‘) as conf_file:
15     conf_file.write("LIMIT=%s\n" % str(args[‘l‘]))
16     conf_file.write("P_THREAD_NUM=%s\n" % str(args[‘n‘]))
17     conf_file.write("SAVE_DIR=‘%s‘\n" % args[‘o‘])
18 conf_file.close()
19
20 # print(args)
时间: 2024-08-26 10:11:49

命令行参数读取和解析的相关文章

osg学习笔记2, 命令行参数解析器ArgumentParser

ArgumentParser主要负责命令行参数的读取 #include <osgDB/ReadFile> #include <osgViewer/Viewer> int main(int argc, char **argv) { //命令行参数读取 osg::ArgumentParser arguments(&argc, argv); std::string filename; arguments.read("--model", filename); o

Qt之命令行参数

简述 在Qt之进程间通信(QProcess)一节,我们讲解了如何通过QProcess来进行进程间的通信.主要通过启动外部程序,然后通过命令行的方式传递参数. 这里,我们可以通过Qt Creator来设置命令行参数Arguments,来设置需要用到的信息.也可以用来测试其它进程传参功能. 简述 设置参数 解析 更多参考 设置参数 选择:项目 -> 构建和运行 -> 运行,在Arguments输入框中输入需要传递的参数. 为了演示,我传递了一个Json对象:{\"UserName\&qu

boost之program_options库,解析命令行参数、读取配置文件

一.命令行解析 tprogram_options解析命令行参数示例代码: [cpp] view plaincopy #include <iostream> using namespace std; #include <boost/program_options.hpp> namespace po = boost::program_options; int main(int argc, char*argv[]) { //int level; po::options_descripti

【Python】 配置解析ConfigParser &amp; 命令行参数解析optparser

ConfigParser ConfigParser包装了配置文件的读取和写入,使得python程序可以更加轻松操作配置文件了.这里的配置文件是指.ini的那种文件,基本格式如下 [section_a] a_key1 = a_value1 a_key2 = a_value2 [section_b] b_key1 = b_value1 b_key2 = b_value2 b_key3 = b_value3 将一个文件分隔成几个section,每个section中又有很多键值对,以这样的方式构建起配置

Python3-argparse模块-解析命令行参数

官方文档 http://python.usyiyi.cn/translate/python_352/library/argparse.html 代码示例 import argparse # 1.获取参数解析对象 parser = argparse.ArgumentParser(description="帮助信息前的一些描述信息,可以不写哦") # 2.添加可解析的参数 # add_argument()函数常用参数 # name or flags 选项字符串的名字或列表,例如foo或者

python 命令行参数解析 argparse简单分析

在python 2.7 后,不推荐使用 optparse, 而推荐使用 argparse. 其它的不多说,简单的分析下我遇到的问题:我是想用 argparse 来解析不定长的命令行参数 例如: import argparse import sys parser = argparse.ArgumentParser(description='test parsing arguments') parser.add_argument('pos1', nargs='*') parser.add_argum

第5章4节《MonkeyRunner源码剖析》Monkey原理分析-启动运行: 命令行参数解析(原创)

天地会珠海分舵注:本来这一系列是准备出一本书的,详情请见早前博文"寻求合作伙伴编写<深入理解 MonkeyRunner>书籍".但因为诸多原因,没有如愿.所以这里把草稿分享出来,所以错误在所难免.有需要的就参考下吧,转发的话还请保留每篇文章结尾的出处等信息. 设置好Monkey的CLASSPATH环境变量以指定"/system/framework /framework/monkey.jar"后,/system/bin/monkey这个shell脚本就会通

MFC解析启动命令行参数——CCommandLineInfo类

MFC中CCommandLineInfo类被用于分析启动应用时的命令行参数. MFC应用一般都会在它的应用对象中使用函数InitInstance()创建这个类的一个本地实例.然后把该对象传给CWinApp::ParseCommandLine(),ParseCommandLine()又重复调用ParseParam()填充CCommandLineInfo对象.最后,CCommandLineInfo对象被传给CWinApp::ProcessShellCommand()来处理命令行参数和选项.所以我们会

PHP 命令行参数解析工具类

<?php /** * 命令行参数解析工具类 * @author guolinchao */ class CommandLine { // 临时记录短选项的选项值 private static $shortOptVal = null; // options value private static $optsArr = array(); // command args private static $argsArr = array(); // 是否已解析过命令行参数 private static