Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use the parsing command line option getopt_long in linux

2025-04-02 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

Shulou(Shulou.com)06/01 Report--

This article is about how to use the parsing command line option getopt_long in linux. The editor thinks it is very practical, so share it with you as a reference and follow the editor to have a look.

Linux parse command line option getopt_long usage

It is inevitable to use command-line options in the program, you can choose your own parsing command-line options, but there is ready-made, there is no need to recreate the wheel.

The following describes the use of getopt_long to resolve command line options.

The main use in the program is:

Whether parameter comments are required for short options and long options

-v-- version No query version number

-n-- name is the (user name) specified user

-d-- whether debug has been tested

1. The source of the function

The code is as follows:

[cpp]

# include / / getopt_long () header file location

Int getopt_long (int _ _ argc, char * const * _ argv

Const char * _ _ shortopts

Const struct option * _ _ longopts, int * _ longind)

Int getopt_long_only (int _ _ argc, char * const * _ argv

Const char * _ _ shortopts

Const struct option * _ _ longopts, int * _ longind)

2. Parameter introduction

Argc argv: passed directly from the main function

Shortopts: short option string. For example, "nsaw v", it should be pointed out here that the short option string does not need'-', but when the option needs to pass parameters, add ":" after the short option.

Longopts:struct option array, which is used to hold long option parameters.

Longind: returns the index value of the long option in the longopts structure array for debugging. Generally set to NULL

Here is an introduction to struct option

The code is as follows:

[cpp]

Struct option

{

Const char * name;// long option name

Whether parameters are required for int has_arg;//

Int * flag

Int val

}

Name: long option name

Has_arg: whether parameters are required. There are three cases of value.

The code is as follows:

[cpp]

# define no_argument 0 / / No parameters are required

# define required_argument 1 / / parameters must be specified

# define optional_argument 2 / / parameters are optional

Flag and val

Flag and val depend on each other in two main situations:

(1) flag is NULL, and the Val value is used to determine the long option, so you need to specify a unique Val value for the long option. It also builds a bridge between long options and short options.

(2) if flag is not NULL, the val value is stored in the storage space pointed to by flag to identify the occurrence of the long option.

3. Return value

If a short option is used in the program, a short option character (such as'n') is returned, and when the parameter is required, the parameter is stored in optarg before returning.

The long option is used in the program, and the return value is determined according to flag and val. When flag is NULL, the Val value is returned. So different treatments are done according to the vale value, which also means that the val must be unique. When the vale value is equal to the short option value, you can use the short option parsing function to resolve the long option; when flag is not NULL, the val value is stored in the storage space pointed to by flag, and getopt_long returns 0

Undefined long option or short option appears, getopt_long returns?

After parsing, getopt_long returns-1

4. Examples

Theory should be combined with practice

The code is as follows:

[cpp]

# include

# include

# include / / getopt_long () header file location

Int main (int argc, char** argv)

{

Const char * optstring= "NRV"

Int c,deb,index

Struct option opts [] = {{"username", required_argument,NULL,'n'}

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

{"debug", no_argument,&deb,1}

{0,0,0,0}}

While (c=getopt_long (argc,argv,optstring,opts,&index))! =-1)

{

Switch (c)

{

The user name is specified by case'naught or-- username.

Printf ("username is% s\ n", optarg)

Break

Case'vails, VOGULAGUP, case, or-- version, output the version number.

Printf ("version is 0.0.1\ n")

Break

Case 0://flag is not NULL

Printf ("debug is% d\ n", deb)

Break

Case'?': / / option is not defined

Printf ("?\ n")

Break

Default:

Printf ("c is% d\ n", c)

Break

}

}

Return 0

}

Run the screenshot:

To explain: getopt_long_only, this function is the same as getopt_long, except that you can use'- 'followed by a long option name, such as. / main-username jackie

Thank you for reading! This is the end of the article on "how to use the parsing command line option getopt_long in linux". I hope the above content can be of some help to you, so that you can learn more knowledge. if you think the article is good, you can share it out for more people to see!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Servers

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report