Python3+getopt解析命令行参数】的更多相关文章

一.说明 在学C语言的时候就知道可以通过argc获取命令行参数个数,可以通过argv获取具体参数.但自己写的程序获取到的参数一是没有键值形式二是写的参数不能乱序,和系统命令不太一样. 再往后点知道有getopt这个东西,但印象中尝试理解其用法很多次都没什么结果:最近又越来多写程序,再次感觉很有必要掌握. 这里以Python3为例演示getopt,python感觉就是C的封装,C的getopt应该也类似. 二.程序代码 此程序中设置-h/-n/-p三个选项,-h不带值-n和-p带值:三个参数设置等…
高效工作的一个诀窍就是尽可能自动化, 简便化. 比如, 公司里, 要搜索多个集群下的应用日志来排查问题, 需要使用 pssh: pssh -i -h api_hangzhou.iplist "grep  101-70795118 /path/to/info.2015-03-03.*.log" pssh -i -h api_hangzhou.iplist "grep  101-70795118 /path/to/info.log*" 这样有什么不方便呢? 1.  记忆…
(一) 在Linux中,用命令行执行可执行文件时可能会涉及到给其加入不同的参数的问题,例如: ./a.out -a1234 -b432 -c -d 程序会根据读取的参数执行相应的操作,在C语言中,这个功能一般是靠getopt()这个函数,结合switch语句来完成的,首先来看下面的代码: #include <stdio.h>#include <unistd.h> int main(int argc,char *argv[]){  int ch;  opterr=0;    whil…
常常需要解析命令行参数,经常忘记,好烦,总结下来吧. 1.Python 中也可以所用 sys 的 sys.argv 来获取命令行参数: sys.argv 是命令行参数列表 参数个数:len(sys.argv)脚本名:    sys.argv[0]参数1:     sys.argv[1] 示例代码如下: #!/usr/bin/python # -*- coding: UTF-8 -*- import sys print('参数个数为: ' + str(len(sys.argv)) + '个参数.'…
在编写需要命令行参数的C程序的时候,往往我们需要先解析命令行参数,然后根据这些参数来启动我们的程序. C的库函数中提供了两个函数可以用来帮助我们解析命令行参数:getopt.getopt_long. getopt可以解析短参数,所谓短参数就是指选项前只有一个“-”(如-t),而getopt_long则支持短参数跟长参数(如"--prefix"). getopt函数 #include<unistd.h> int getopt(int argc,char * const arg…
目录 命令行参数及 argparse 包 argparse 传递 bool 参数错误做法 argparse 传递 bool 参数正确做法 1 argparse 传递 bool 参数正确做法 2 References 在使用 argparse 解析 bool 参数时,需要设定 add_argument 的 action 参数为 'store_true' 或者 'store_false',而不是设定 type 参数为 bool,具体原因是,如果设定 type=bool,那么无论该参数传 True 还…
linux通常使用GNU C提供的函数getopt.getopt_long.getopt_long_only函数来解析命令行参数. 移植到Windows下 getopt.h #ifndef _GETOPT_H #define _GETOPT_H #ifdef __cplusplus extern "C" { #endif /* For communication from `getopt' to the caller. When `getopt' finds an option tha…
一.命令行解析 tprogram_options解析命令行参数示例代码: #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_description desc("Allowed…
一.关于解析命令行参数的方法 关于“解析命令行参数”的方法我们一般都会用到sys.argv跟optparse模块.关于sys.argv,网上有一篇非常优秀的博客已经介绍的很详细了,大家可以去这里参考:https://www.cnblogs.com/aland-1415/p/6613449.html 这里为大家介绍一个比sys.argv更强大的optparse模块. 这里说一句题外话,点开optparse的源码,第一行注释是这样的:A powerful, extensible, and easy-…
linux 中解析命令行参数(getopt_long用法) http://www.educity.cn/linux/518242.html 详细解析命令行的getopt_long()函数 http://www.codeweblog.com/%E8%AF%A6%E7%BB%86%E8%A7%A3%E6%9E%90%E5%91%BD%E4%BB%A4%E8%A1%8C%E7%9A%84getopt_long-%E5%87%BD%E6%95%B0/ C/C++中有哪些简单好用的命令行参数解析工具? h…
getopt 与 getopts 都是 Bash 中用来获取与分析命令行参数的工具,常用在 Shell 脚本中被用来分析脚本参数. 两者的比较 (1)getopts 是 Shell 内建命令,getopt 是一个独立外部工具 (2)getopts 使用语法简单,getopt 使用语法较复杂 (3)getopts 不支持长参数(如:--option ),getopt 支持 (4)getopts 不会重排所有参数的顺序,getopt 会重排参数顺序(这里的区别下面会说明) (5)getopts 出现…
一:posix约定: 下面是POSIX标准中关于程序名.参数的约定: 程序名不宜少于2个字符且不多于9个字符: 程序名应只包含小写字母和阿拉伯数字: 选项名应该是单字符或单数字,且以短横 '-' 为前綴: 多个不需要选项参数的选项,可以合并.(譬如:foo  -a -b -c  ----> foo  -abc) 选项与其参数之间用空白符隔开: 选项参数不可选. 若选项参数有多值,要将其并为一个字串传进来.譬如:myprog -u "arnold,joe,jane".这种情况下,需…
转载:http://blog.csdn.net/hcx25909/article/details/7388750 每一天你都在使用大量的命令行程序,是不是感觉那些命令行参数用起来比较方便,他们都是使用getopt来实现的. 在Linux下使用getopt写程序是一种比较cool的事情,下面来简单的介绍一下getopt的使用. === getopt使用 === 在讨论参数处理之前,我们先明确两个概念:选项.选项参数gcc -g -o test test.c我们经常使用上面的命令来编译程序,这里g和…
假设有一程序 testopt,其命令行选项参数有: -i            选项 -l            选项 -r           选项 -n <值> 带关联值的选项 则处理参数的代码如下: #include <stdio.h> #include <unistd.h> int main(int argc, char *argv[]) { // 选项参数 int opt; ) { switch (opt) { case 'i': // 开关选项 case '…
在Linux中,我们常常用到 ls -l 等等之类带有选项项的命令,下面,让我们用C++来实现该类似的命令. 在实现之前,首先,我们来介绍一下一个重要函数:getopt() 表头文件 #include<unistd.h> 定义函数 int getopt(int argc,char * const argv[ ],const char * optstring);   函数说明: 用来分析命令行参数.参数 argc 和 argv 是由 main() 传递的参数个数和内容. 参数 optstring…
 1  新建项目 main.cpp #include <QCoreApplication> #include <QCommandLineParser> #include <QDebug> #include <stdio.h> int main(int argc, char** argv) { QCoreApplication app(argc, argv); app.setApplicationVersion("1.0.0.0");…
很好的输入参数解析方法 ,转载记录下 转载在: https://www.cnblogs.com/onmyway20xx/p/7346709.html Apache Commons CLI 简介 Apache Commons CLI 是 Apache 下面的一个解析命令行输入的工具包,该工具包还提供了自动生成输出帮助文档的功能. Apache Commons CLI 支持多种输入参数格式,主要支持的格式有以下几种: POSIX(Portable Operating System Interface…
一.说明 shell中获取参数可以直接使用$1.$2等形式来获取,但这种方式有明显的限制:每个参数的位置是固定的.比如如果在设计上$1是ip地址$2是端口,那在执行时就必须第一个参数是ip第二个参数是端口而不能反过来. shell提供了getopt和getopts来解析参数,getopt比getopts功能强一些getopts比getopt简单一些:总体而言getopt和getopts都差强人意. 二.使用getopt解析参数 getopt比getopts强一些复杂一些:能在命令行中单独使用.支…
源码: #include <boost/program_options.hpp> namespace po = boost::program_options; int main(int argc, char** argv) { int compression; po::options_description desc("Allow options"); desc.add_options() ("help", "produce help mess…
使用的背景 在工作中我们经常要制定运行脚本的一些参数,因为有些东西是随着我么需求要改变的,所以在为们写程序的时候就一定不能把写死,这样我们就要设置参数 在python中我们可以通过sys 模板的argv 但是这模板功能远没有今天我们介绍的optparse模块强大. 介绍下最基本的用法: 使用optionparser模块来解析 optionparser的执行过程: 导入optionparser : from optparse import OptionParser构造optionparser的对象…
package foo; import org.apache.commons.cli.BasicParser; import org.apache.commons.cli.CommandLine; import org.apache.commons.cli.CommandLineParser; import org.apache.commons.cli.Options; public class test { public static void main(String[] args) thro…
下载地址: https://jcenter.bintray.com/org/apache/commons/com.springsource.org.apache.commons.cli/1.2.0/ package com.test;import org.apache.commons.cli.CommandLineParser;import org.apache.commons.cli.Options;import org.apache.commons.cli.BasicParser;impor…
说好不造轮子的,就管不住这手 #include <cstdio> #include <string> #include <vector> bool ParseChar( const std::string& buf, std::size_t& pos, char& ch, bool& escape ) { char c; if ( pos == buf.length() ) return false; // Get the charact…
#include <QGuiApplication> #include <QQmlApplicationEngine> #include <QQuickView> #include <QCommandLineParser> int main(int argc, char *argv[]) { QGuiApplication app(argc, argv); QGuiApplication::setApplicationName("Qt")…
Python 提供了 getopt 模块来获取命令行参数. $ python test.py arg1 arg2 arg3 Python 中也可以所用 sys 的 sys.argv 来获取命令行参数: sys.argv 是命令行参数列表. len(sys.argv) 是命令行参数个数. 注:sys.argv[0] 表示脚本名. 实例 test.py 文件代码如下: #!/usr/bin/python3 import sys print ('参数个数为:', len(sys.argv), '个参数…
getopt(分析命令行参数) getopt(分析命令行参数) 短参数的定义 返回值 范例 getopt_long 相关函数表头文件#include<unistd.h> 函数声明int getopt(int argc,char * const argv[ ],const char * optstring); 全局变量 extern char *optarg; extern int optind, opterr, optopt;  //索引/错误输出标志/最后一个未知选项 函数说明getopt(…
# -*- coding:utf-8 -*- import sys def test(): """ 参数列表:sys.argv 参数个数:len(sys.argv) 脚本名: sys.argv[0] 参数1: sys.argv[1] 参数2: sys.argv[2] """ print "参数个数为:"+ str(len(sys.argv)) print "参数列表为:"+ str(sys.argv) pr…
getopt 命令 使用getopt命令,可以解析任何命令行选项和参数,但是用法比较复杂.getopt的命令用法如下: $ getopt --help 用法: getopt optstring parameters getopt [options] [--] optstring parameters getopt [options] -o|--options optstring [options] [--] parameters 选项: -a, --alternative 允许长选项以 - 开始…
python解析命令行参数主要有三种方法:sys.argv.argparse解析.getopt解析 方法一:sys.argv —— 命令行执行:python test_命令行传参.py 1,2,3 1000 # test_命令行传参.py import sys def para_input(): print(len(sys.argv)) # 参数序列的长度,此时所有参数存放在一个list之中 if len(sys.argv) < 2: sys.exit("python error"…
flag包是Go语言标准库提供用来解析命令行参数的包,使得开发命令行工具更为简单 常用方法 1.flag.Usage 输出使用方法,如linux下ls -h的帮助输出 2.flag.Type(参数名, 默认值, 使用提示) Type为类型 如String, Int, Uint 调用相应的flag.Sring flag.Int flag.Uint方法 方法返回相应类型的指针 3.flag.TypeVar(指针, 参数名, 默认值, 使用提示) 与flag.Type方法基本相同,不同的是多一个指针参…