Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Small improve #20

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 8 additions & 3 deletions optparse.h
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,7 @@ char *optparse_arg(struct optparse *options);
#define OPTPARSE_MSG_INVALID "invalid option"
#define OPTPARSE_MSG_MISSING "option requires an argument"
#define OPTPARSE_MSG_TOOMANY "option takes no arguments"
#define OPTPARSE_MSG_UNKNOWN "unknown error"

static int
optparse_error(struct optparse *options, const char *msg, const char *data)
Expand Down Expand Up @@ -259,8 +260,12 @@ optparse(struct optparse *options, const char *optstring)
else
options->optarg = 0;
return option[0];
default: {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it's not possible for optparse_argtype() to return anything other than -1 .. 2, so this case will never execute.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Logically yes, but I encountered one case that returned 0 from line 263. I will come back to you if I can reproduce the problem.

By the way, isn't it so that optparse should return one of follwing values?

  • Option character, when an option is successfully parsed
  • -1: indicate all options have been parsed
  • '?': for invalid option

The return 0 in line 263 doesn't make that much sense?

char str[2] = {0, 0};
str[0] = option[0];
return optparse_error(options, OPTPARSE_MSG_UNKNOWN, str);
}
}
return 0;
}

OPTPARSE_API
Expand Down Expand Up @@ -390,8 +395,8 @@ optparse_long(struct optparse *options,
options->optarg = options->argv[options->optind];
if (options->optarg == 0)
return optparse_error(options, OPTPARSE_MSG_MISSING, name);
else
options->optind++;

options->optind++;
}
return options->optopt;
}
Expand Down