A command line parameter parser

Source: Internet
Author: User
For work, you need to write a console tool program with many running parameters. Remember that there is a system function getopt in Linux that can help programmers parse command line parameters, but this function is not available in VC, the help information and header file getopt for this function in Linux are studied. h. The getopt and getopt_long functions are re-implemented. Because I have not read the source code of the getopt function in Linux, this function is definitely not perfect. However, it has been tried, still able to meet my personal needs, here the source code is pasted out, please be familiar with or read the getopt source code friend corrected.
Getopt. h

/*************************************** **************************************** * Filename: getopt. h ** Description: Command Line Parameter parser ** Date: 2008-06-19 ** Author: Chang Hailong (Email: hailongchang@163.com) ** copyright (c) 2008 changhailong ************************************* **************************************** * **/# ifdef _ cplusplusextern "C" {# endifextern int optind; // call getopt () again to obtain the index of the next argv pointer extern int optopt; // The last known option extern char * optarg; // point to the current option parameter (if any) pointer extern int getopt (INT argc, char * const argv [], const char * shortopts ); # ifdef _ cplusplus} # endif # ifdef _ cplusplusextern "C" {# endifstruct option {char * Name; // pointer to the long option name int has_arg; // indicates whether the option has the int * flag; int val; // indicates the short parameter}; extern int getopt_long (INT argc, char * const argv [], const char * shortopts, const struct option * longopts, int * longind ); # ifdef _ cplusplus} # endif # define no_argument 0 # define required_argument 1 # define optional_argument 2 # define case_sensitive // define whether the parameter processor is case sensitive

Getopt. c

/*************************************** **************************************** * Filename: getopt. c ** Description: command line parameter parser ** Date: 2008-06-19 ** Author: Chang Hailong (Email: hailongchang@163.com) ** copyright (c) 2008 changhailong ************************************* **************************************** * **/# include "getopt. H "# include" string. H "int optind = 1; // the index of the parameter to be scanned in argv [] // stores the returned value after resolution. If an argument is invalid '? ', Which stores argumentint optopt; char * optarg; // if the current parameter has subsequent parameters, this value points to this subsequent parameter ////////////////////////////////// //////////////////////////////////////// //// int paser_shotops (INT argc, char * const argv [], size_t length, // The length of the argument item to be parsed char * item_cont, // stores the argument const char * shortopts) {char * Pos = NULL; If (length = 2 & item_cont [0] = '-') // If the-H condition is met, the parsing can be performed {pos = strchr (shortopts, item_cont [1]); // when If (null = POS) {optind ++; // if not found in shortopts, return '? 'Optopt = item_cont [1]; return '? ';} Else {If (* (++ POS) =': ') // if this item carries a subsequent parameter, use optarg to store optarg = argv [++ optind]; optind ++; return item_cont [1] ;}} else // return '? '{Optopt = item_cont [0]; optind = argc; return '? ';}} //////////////////////////////////////// /// // Int paser_longopts (INT argc, char * const argv [], size_t length, // The length of the argument item to be parsed char * item_cont, // stores the argument const char * shortopts to be parsed, const struct option * longopts) {char * assist_arg = NULL; // secondary string int long_index = 0; // longopts index // if the length of the argument to be parsed is greater than 2, and the first and second characters are both '-' If (length> 2) & (item_cont [0] = '-')& & (Item_cont [1] = '-') {// loop longopts, find the project that matches the current parameter while (longopts [long_index]. Name! = NULL) {If (strcmp (item_cont + 2, longopts [long_index]. name) = 0) {If (longopts [long_index]. has_arg = required_argument) optarg = argv [++ optind]; If (longopts [long_index]. has_arg = optional_argument) {// because it is optional for subsequent parameters, obtain the subsequent parameters first, and then determine if _ Arg = argv [optind + 1]; If (assist_arg [0]! = '-') {Optarg = pai_arg; ++ optind ;}}// after finding a match in longopts, the Val value optopt = longopts [long_index] is returned. val; optind ++; return optopt;} elselong_index ++;} // if no match exists in longopts, then, the system prompts and continues parsing the parameter optopt = item_cont [2]; ++ optind; return '? ';} Else // parse the short parameters return paser_shotops (argc, argv, length, item_cont, shortopts );} //////////////////////////////////////// /// // int getopt (INT argc, char * const argv [], const char * shortopts) {char * arg_item = NULL; size_t Len = 0; If (argc = 1) Return-1; else {(; optind <argc; optind ++) {arg_item = argv [optind]; Len = strlen (arg_item); # ifndef alias = strlwr (arg_item); # endifreturn paser_shotops (argc, argv, len, arg_item, shortopts);} optind = 1; // if the resolution is complete, reset optind to 1 return-1 ;}} //////////////////////////////////////// /// // int getopt_long (INT argc, char * const argv [], const char * shortopts, const struct option * longopts, int * longind) {char * Pos = NULL; char * arg_item = NULL; size_t Len = 0; if (argc = 1) Return-1; else {for (; optind <argc; optind ++) {arg_item = argv [optind]; Len = strlen (arg_item ); # ifndef case_sensitivearg_item = strlwr (arg_item); # endif // Add 0 after the resolved parameter item to facilitate string operations * (arg_item + Len) = 0; return paser_longopts (argc, argv, Len, arg_item, shortopts, longopts);} optind = 1; // if the resolution is complete, reset optind to 1 return-1 ;}} //////////////////////////////////////// ///////////////////////////////////////

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.