Create an account

Very important

  • To access the important data of the forums, you must be active in each forum and especially in the leaks and database leaks section, send data and after sending the data and activity, data and important content will be opened and visible for you.
  • You will only see chat messages from people who are at or below your level.
  • More than 500,000 database leaks and millions of account leaks are waiting for you, so access and view with more activity.
  • Many important data are inactive and inaccessible for you, so open them with activity. (This will be done automatically)


Thread Rating:
  • 214 Vote(s) - 3.58 Average
  • 1
  • 2
  • 3
  • 4
  • 5
CLI parsing in C, part 2

#1
Now that we've gone over

[To see links please register here]

, understanding its extended version, getopt_long, shouldn't be too difficult.

Redefining options
The function we're looking for, getopt_long, is in the same header as its counterpart: getopt.h. The tips in the GNU manual recommend adding long options to any program that uses options at all, since it makes the things they represent more explicit and easier to understand.

First, let's take a look at the parser we constructed in the last part, using the buildin error handling:

Hidden Content
You must

[To see links please register here]

or

[To see links please register here]

to view this content.


To convert this to a working getopt_long parser, we first need to define our long options. We do this using an array of "option" structs, defined by the header. The option struct has four fields.
const char *name: the long name of the option without the prefix (--).
int has_arg: an indicator of whether or not the option takes an argument--no_argument (equivalent to no colon suffix) and required_argument (equivalent to a one colon suffix) are self-explanatory, but use optional_argument (two colons) for the GNU extension.
int *flag and int val: these control how to handle the option when it appears. If flag is a null pointer, val is used to identify the option (the great thing about the char type is that we can use them as ints here and just alias the short options). If flag isn't a null pointer, it should point to an int variable, and val should be the value assigned to the variable flag points to when this option is found.

When getopt_long sets a flag, it always returns 0. This will be important in a little bit.

Lastly for options, the array must end with a completely null option. Given these parameters, we can create a set for our previous parser.

Hidden Content
You must

[To see links please register here]

or

[To see links please register here]

to view this content.


Using the function
The getopt_long function is similar to the short version, but with a few extras. If we compare the two function's signatures, we can see that getopt_long requires our option array and an int* on top of what's needed for getopt.

Hidden Content
You must

[To see links please register here]

or

[To see links please register here]

to view this content.


The longopts argument is easy, just pass it the array we defined before. int *indexptr stores the index of any found long options. We can get the name using "longopts[*indexptr].name", utilizing the option struct, but we only have one so there's not much reason to bother. In our case, we can make it a null pointer.

When all is said and done, our new call should look something like this:

Hidden Content
You must

[To see links please register here]

or

[To see links please register here]

to view this content.


Working example
Like the last part, a full, working example to tinker with is available below.

Hidden Content
You must

[To see links please register here]

or

[To see links please register here]

to view this content.

Reply

#2
I have never used getopt(). I always push the arguments into a vector and process that. Thanks for the share.
Reply



Forum Jump:


Users browsing this thread:
1 Guest(s)

©0Day  2016 - 2023 | All Rights Reserved.  Made with    for the community. Connected through