Skip to content

Commit

Permalink
Handling optparse long option
Browse files Browse the repository at this point in the history
  • Loading branch information
gsnw-sebast committed May 19, 2024
1 parent 90ad83f commit 46ee105
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 0 deletions.
10 changes: 10 additions & 0 deletions src/fping.c
Original file line number Diff line number Diff line change
Expand Up @@ -561,6 +561,16 @@ int main(int argc, char **argv)
float opt_value_float;
while ((c = optparse_long(&optparse_state, longopts, NULL)) != EOF) {
switch (c) {
case '0':
/*
* Use long-option example
* Define "struct optparse_long longopts": { "long-option-name", '0', OPTPARSE_NONE }
*
* if(strstr(optparse_state.optlongname, "long-option-name") != NULL) {
* long_option_flag = 1;
* }
*/
break;
case '4':
#ifdef IPV6
if (hints_ai_family != AF_UNSPEC && hints_ai_family != AF_INET) {
Expand Down
2 changes: 2 additions & 0 deletions src/optparse.c
Original file line number Diff line number Diff line change
Expand Up @@ -238,12 +238,14 @@ optparse_long(struct optparse *options,
/* Parse as long option. */
options->errmsg[0] = '\0';
options->optopt = 0;
options->optlongname = 0;
options->optarg = 0;
option += 2; /* skip "--" */
options->optind++;
for (int i = 0; !longopts_end(longopts, i); i++) {
const char *name = longopts[i].longname;
if (longopts_match(name, option)) {
options->optlongname = option;
if (longindex)
*longindex = i;
options->optopt = longopts[i].shortname;
Expand Down
1 change: 1 addition & 0 deletions src/optparse.h
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ struct optparse {
int permute;
int optind;
int optopt;
char *optlongname;
char *optarg;
char errmsg[64];
int subopt;
Expand Down

0 comments on commit 46ee105

Please sign in to comment.