Parsing Arguments with getopt
来源:互联网 发布:完整性保护算法 编辑:程序博客网 时间:2024/05/21 17:01
getopt
used for parse arguments passed tomain(int argc, char**argv) function under cmdline environments.
each parameters filtered by checking if it is startwith "-" to know if it is an valid one. and the characters aside from the initial '-' are taken as option characters.
it normally returns the option charactor(maybe defined by our option string or others, just by filter rule mentioned above) and will return -1 while no more arguments.
always we call it in a loop and use a switch-case statement to parse all passed arguments we need for our programs.
to make the parse process continiously and know the current parse status, other varables are imported. whilegetopt() returned, it also updates the external variableoptind and a static variablenextchar.
The variable optind is the index of the next element to be processed inargv. The system initializes this value to 1. The caller can reset itto 1 to restart scanning of the sameargv, or when scanning a new argument vector.
just as name presented, nextchar is the next char to be scanned in the option-element in which the last option character we returned was found.
If getopt() does not recognize an option character, it prints an error message tostderr, stores the character inoptopt, and returns'?'. The calling program may prevent the error message by settingopterr to 0.
If getopt() finds an option character inargv that was not included inoptstring, or if it detects a missing option argument, it returns '?' and sets the external variableoptopt to the actual option character. If the first character (following any optional '+' or '-' describedabove) ofoptstring is a colon (':'), thengetopt() returns ':' instead of '?' to indicate a missing option argument. If an error was detected,and the first character ofoptstring is not a colon, and the external variableopterr is nonzero (which is the default),getopt() printsan error message.
optstring is a string containing the legitimate option characters. If such a character is followed by a colon, the option requires an argument, sogetopt() places a pointer to the following text in the sameargv-element, or the text of the following argv-element, in optarg. Two colons mean an option takes an optional arg; if there is text in the currentargv-element (i.e., in the same word as the option name itself, for example, "-oarg"), then it is returned inoptarg, otherwiseoptarg is set to zero. This is a GNU extension.a must head file needs tobe imported is "unistd.h"
here is an sample from GNU site:
#include <ctype.h>#include <stdio.h>#include <stdlib.h>#include <unistd.h>intmain (int argc, char **argv){ int aflag = 0; int bflag = 0; char *cvalue = NULL; int index; int c; opterr = 0; while ((c = getopt (argc, argv, "abc:")) != -1) switch (c) { case 'a': aflag = 1; break; case 'b': bflag = 1; break; case 'c': cvalue = optarg; break; case '?': if (optopt == 'c') fprintf (stderr, "Option -%c requires an argument.\n", optopt); else if (isprint (optopt)) fprintf (stderr, "Unknown option `-%c'.\n", optopt); else fprintf (stderr, "Unknown option character `\\x%x'.\n", optopt); return 1; default: abort (); } printf ("aflag = %d, bflag = %d, cvalue = %s\n", aflag, bflag, cvalue); for (index = optind; index < argc; index++) printf ("Non-option argument %s\n", argv[index]); return 0;}
and the sample input and results also be provided
% testoptaflag = 0, bflag = 0, cvalue = (null)% testopt -a -baflag = 1, bflag = 1, cvalue = (null)% testopt -abaflag = 1, bflag = 1, cvalue = (null)% testopt -c fooaflag = 0, bflag = 0, cvalue = foo% testopt -cfooaflag = 0, bflag = 0, cvalue = foo% testopt arg1aflag = 0, bflag = 0, cvalue = (null)Non-option argument arg1% testopt -a arg1aflag = 1, bflag = 0, cvalue = (null)Non-option argument arg1% testopt -c foo arg1aflag = 0, bflag = 0, cvalue = fooNon-option argument arg1% testopt -a -- -baflag = 1, bflag = 0, cvalue = (null)Non-option argument -b% testopt -a -aflag = 1, bflag = 0, cvalue = (null)Non-option argument -
besides getopt, other API also provided like "getopt_long" by linux man page
ref:
http://linux.die.net/man/3/getopt
http://www.gnu.org/software/libc/manual/html_node/Example-of-Getopt.html
java port also provide:
http://www.gnu.org/software/gnuprologjava/api/gnu/getopt/Getopt.html
some other String related API always needed:
http://blog.csdn.net/eager7/article/details/8131437
http://blog.csdn.net/bg2bkk/article/details/37569555
you got it! :-D
- Parsing Arguments with getopt
- Sqoop fails with "Error parsing arguments for import"
- Parsing JSON With SBJSON
- Parsing Strings with split
- yum stuck on “Parsing package install arguments”
- Parsing URLs with the DOM
- Parsing URLs with the DOM
- Parsing XML Files with PowerShell
- Parsing XML Files with SAX
- Parsing XML with SAX_(MyMoviesWithHttpClient)
- Parsing XML with XmlPull_(MyMoviesWithHttpClient)
- Parsing with Compositional Vector Grammars
- Parsing URLs with the DOM!
- 11.12. Parsing XML with NSXMLParser
- wordcount出现错误Use GenericOptionsParser for parsing the arguments.
- parsing xml with sax and pull
- SuperParsing: Scalable Nonparametric Image Parsing with Superpixels
- PyQuery Tutorial: Basic HTML Parsing with PyQuery
- Android研究院之应用程序通讯录(十二)
- Android 仿ios 可滑动切换效果按钮 实例代码
- 关于Switch的自定义样式
- uva 10003 Cutting Sticks (DP)
- 黑马程序员---c语言基础--基本运算
- Parsing Arguments with getopt
- poj2393
- 堆和堆排序
- 选择排序
- poj 2240 floyd
- spring mvc json null空转""空串
- 浅谈oracle数据库索引
- 30个实例详解TOP命令
- 基础练习 数的读法