Skip to content
Browse files

[api] Support long aliases

  • Loading branch information...
1 parent b333dcc commit 44d3bcdd6f3964979b3ed4a370946f085c1e8fe3 @mmalecki committed Mar 30, 2013
Showing with 26 additions and 6 deletions.
  1. +26 −6 src/saneopt.c
View
32 src/saneopt.c
@@ -13,6 +13,28 @@ saneopt_t* saneopt_init(int argc, char** argv) {
return saneopt;
}
+int saneopt__matches(saneopt_t* opt, char* name, char* arg) {
+ int i;
+
+ if (strncmp(arg, "--", 2) == 0) {
+ arg += 2;
+
+ if (strcmp(arg, name) == 0) {
+ return 1;
+ }
+
+ for (i = 0; opt->aliases && i < opt->alias_count; i++) {
+ if (strcmp(opt->aliases[i]->option, name) == 0 &&
+ strlen(opt->aliases[i]->alias) > 1 &&
+ strcmp(arg, opt->aliases[i]->alias) == 0) {
+ return 1;
+ }
+ }
+ }
+
+ return 0;
+}
+
void saneopt_alias(saneopt_t* opt, char* option, char* alias) {
saneopt__alias_t* alias_ = malloc(sizeof(saneopt__alias_t));
alias_->option = option;
@@ -31,12 +53,10 @@ char* saneopt_get(saneopt_t* opt, char* name) {
for (i = 0; i < opt->argc; i++) {
arg = opt->argv[i];
- if (strncmp(arg, "--", 2) == 0) {
- if (strcmp(arg + 2, name) == 0) {
- return ((i + 1) < opt->argc && opt->argv[i + 1][0] != '-')
- ? opt->argv[i + 1]
- : "";
- }
+ if (saneopt__matches(opt, name, arg)) {
+ return ((i + 1) < opt->argc && opt->argv[i + 1][0] != '-')
+ ? opt->argv[i + 1]
+ : "";
}
}

0 comments on commit 44d3bcd

Please sign in to comment.
Something went wrong with that request. Please try again.