Linux under the getopt and getopt_long usage

Recommended for you: Get network issues from WhatsUp Gold. Not end users.

A short, parameter options contract

Below is the agreement about the program name, parameters of POSIX standard:

  1. The program should not be less than 2 characters and not more than 9 characters,
  2. The program should contain only lowercase letters and Arabia digital,
  3. Option name must be single character live single digital, and the short cross' - 'prefix,
  4. Multiple needs no options parameter options, can be merged. Pi Ru: foo -a -b -c ---->foo -abc)
  5. Between the option and parameter by whitespace separated,
  6. Option argument is not optional.
  7. If optional parameters are value, to and for a string came in. For example: CMD -p " A, B, C". In this case, need to separate problem solving these parameters themselves.
  8. Option should occur before the operation number.
  9. Special parameters' -- 'that all parameters all over, then all the parameters considered operand.
  10. Options for how the arrangement does not have what relation, but mutually exclusive options, if an option operation results covering the other options when the results of the operation, the last option play a role; if the option to repeat, order processing.
  11. In order to affect program behavior allows operands, but need to document description.
  12. Read and write procedures specified file should be a single parameter - as standard input or output to meaningful.

Two, long options contract

Below is the agreement about the program name, parameters of GNU standard:

  1. To follow the POSIX agreement of the GNU program, each short option has long options a corresponding.
  2. Additional options for GNU short long options do not require a corresponding, only recommended to have.
  3. Length options can be abbreviated to maintain the uniqueness of the shortest string.
  4. Between the option parameters and long options or by whitespace characters live through a '=' to separate.
  5. Optional parameters are optional (valid only for short options).
  6. Long option allows a hyphen prefixed with.

Three, the getopt function prototype and routines

#include <unistd.h>
int getopt(int argc, char * const argv[], const char *optstring);
extern char *optarg;
extern int optind, opterr, optopt;

The function of the argc and argv parameters are usually directly from the main () parameters passed directly to. Optstring is a string option letters. If there is any character in the string of a back, so this option requires option parameters.

When a given getopt () the number of order parameters (argc), a pointer to an array of these parameters (argv) and option string (optstring), getopt () returns the first option, global variables and set. Using the same parameters again when you call this function, it will return the next option, global variables and setting the corresponding. If there are no longer recognizable options, will return to the -1, and this mission completed.

getopt() The set includes global variables:


#include <stdio.h>
#include <unistd.h>

int main (int argc, char **argv)
int oc;
char ec;
char *b_opt_arg;

while((oc = getopt(argc, argv, ":ngl:")) != -1)
case 'n':
printf("My name is Lyong.\n");
case 'g':
printf("Her name is Xxiong.\n");
case 'l':
b_opt_arg = optarg;
printf("Our love is %s\n", optarg);
case '?':
ec = (char)optopt;
printf("Invalid Option Character \' %c \'!\n", ec);
case ':':
printf("The lack of option parameters´╝ü\n");
return 0;

Four, the getopt_long/getopt_long_only function prototype and routines

GNU provides a getopt-long () and getopt-long-only () function to support command line parsing, long options which, long options string which is started with a short horizontal line, rather than a pair of short line.

getopt_long() Is long options and short options also supports the getopt (version). Here is the statement:

#include <getopt.h>

int getopt_long(int argc, char * const argv[], const char *optstring, const struct option *longopts, int *longindex);
int getopt_long_only(int argc, char * const argv[],const char *optstring,const struct option *longopts, int *longindex);

getopt_long()The first three parameters and the above getopt (the same), the fourth parameter is the point of the array option structure, option structure is called "long options table". If no longindex parameter is set to NULL, then it will point to a variable, this variable will be Fu value long options to find the index into the longopts value, which can be used for fault diagnosis.

The statement option structure in getopt.h as follows:

struct option{
const char *name;
int has_arg;
int *flag;
int val;

Const char *name - the option name, not in front of a short horizontal line, such as "help".

Int has_arg describes the options are options parameter, if there is a parameter of type

Such as: no_argument value 0, no parameter represents options

required_argument value1, representative must have parameters

optional_argument value2, on behalf of the optional parameters

Int *flag if the pointer to NULL, then getopt_long () returns the number of fields in the structure of Valvalue. If the pointer is not NULL, getopt_long () will make it to fill in the number of variables in the Val field, value and getopt_long () returns 0. If flag is not NULL, but found no long option, then the number value of the variable to which it points; invariant.

Int val: this is value found long option return value or flag, not NULL load in *flagvalue. Typically, if flag is not NULL, then Val is a true / false value, such as 1 or 0; on the other hand, if flag is NULL, then Val is usually a character constant, if the long and short options option, then the character constant parameters should be the options appear in optstring with the same.


#include <stdio.h>
#include <getopt.h>

static const char *optString = "Il:o:vh?";

static const struct option longOpts[] = { { "no-index", no_argument, NULL, 'I' },

{ "language", required_argument, NULL, 'l' },

{ "output", required_argument, NULL, 'o' },

{ "verbose", no_argument, NULL, 'v' },

{ "randomize", no_argument, NULL, 0 },

{ "help", no_argument, NULL, 'h' },

{ NULL, no_argument, NULL, 0 }};

opt = getopt_long( argc, argv, optString, longOpts, &longIndex );

while( opt != -1 ) {

switch( opt ) {

case 'I':

globalArgs.noIndex = 1; /* true */


case 'l':

globalArgs.langCode = optarg;

break; case 'o':

globalArgs.outFileName = optarg;


case 'v':



case 'h': /* fall-through is intentional */

case '?':



case 0: /* long option without a short arg */

if( strcmp( "randomize", longOpts[longIndex].name ) == 0 )

{ globalArgs.randomized = 1; }


; default: /* You won't actually get here. */

break; }

opt = getopt_long( argc, argv, optString, longOpts, amp;longIndex );


Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download

Posted by Amanda at January 02, 2014 - 1:35 PM