Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What's the difference between argp and getopt?

I think the title is self explanatory. I am making a program and I was wondering what I should use of the two and why.

like image 854
Pithikos Avatar asked Oct 06 '11 16:10

Pithikos


People also ask

What does the getopt function do?

The getopt function gets the next option argument from the argument list specified by the argv and argc arguments. Normally these values come directly from the arguments received by main . The options argument is a string that specifies the option characters that are valid for this program.

What is ARGP?

Argp is an interface for parsing unix-style argument vectors. See Program Arguments. Argp provides features unavailable in the more commonly used getopt interface. These features include automatically producing output in response to the ' --help ' and ' --version ' options, as described in the GNU coding standards.

Is getopt standard C?

To be clear, getopt is not part of the C standard, it is part of the POSIX standard. A lot of things that we take for granted in C come from POSIX rather than the C standard itself. I think you will run into many different approaches for handling argument parsing.

Is getopt deprecated?

The getopt module will not be deprecated. However, its documentation will be updated to point to argparse in a couple of places. At the top of the module, the following note will be added: The getopt module is a parser for command line options whose API is designed to be familiar to users of the C getopt function.


3 Answers

argp may be more flexible / powerful / etc, but getopt is part of the POSIX standard. Thats a choice you've to make based on whether you expect your program to be portable.

like image 56
jman Avatar answered Oct 14 '22 18:10

jman


From the Argp manual:

Argp provides features unavailable in the more commonly used getopt interface. These features include automatically producing output in response to the ‘--help’ and ‘--version’ options, as described in the GNU coding standards. Using argp makes it less likely that programmers will neglect to implement these additional options or keep them up to date.

like image 40
Brandon E Taylor Avatar answered Oct 14 '22 17:10

Brandon E Taylor


There's not much to choose I don't think. The Argp webpage says this:

Argp provides features unavailable in the more commonly used getopt interface. These features include automatically producing output in response to the ‘--help’ and ‘--version’ options, as described in the GNU coding standards. Using argp makes it less likely that programmers will neglect to implement these additional options or keep them up to date.

Argp also provides the ability to merge several independently defined option parsers into one, mediating conflicts between them and making the result appear seamless. A library can export an argp option parser that user programs might employ in conjunction with their own option parsers, resulting in less work for the user programs. Some programs may use only argument parsers exported by libraries, thereby achieving consistent and efficient option-parsing for abstractions implemented by the libraries.

like image 35
David Heffernan Avatar answered Oct 14 '22 16:10

David Heffernan