Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

[api] Use `char*` for option value

When option is not passed, return value is `NULL`. When option has no
value, return value is `""`. When it has a value, it's returned.

Fixes #1.
  • Loading branch information...
commit 5493c1c6d1064458320851771f702abf701a5755 1 parent adf92bf
@mmalecki authored
Showing with 9 additions and 28 deletions.
  1. +1 −10 include/saneopt.h
  2. +8 −18 src/saneopt.c
View
11 include/saneopt.h
@@ -14,10 +14,6 @@ struct saneopt {
unsigned int alias_count;
} typedef saneopt_t;
-struct saneopt_arg {
- char* value;
-} typedef saneopt_arg_t;
-
/*
* Create a new `saneopt` parser.
*/
@@ -29,13 +25,8 @@ saneopt_t* saneopt_new(int argc, char** argv);
void saneopt_alias(saneopt_t* opt, char* old, char* new_);
/*
- * Check if argument is empty (wasn't passed or has no value).
- */
-int saneopt_empty(saneopt_arg_t* arg);
-
-/*
* Get option called `option`.
*/
-saneopt_arg_t* saneopt_get(saneopt_t* opt, char* name);
+char* saneopt_get(saneopt_t* opt, char* name);
#endif
View
26 src/saneopt.c
@@ -25,28 +25,18 @@ void saneopt_alias(saneopt_t* opt, char* option, char* alias) {
opt->aliases[opt->alias_count - 1] = alias_;
}
-int saneopt_empty(saneopt_arg_t* arg) {
- return arg == NULL || arg->value == NULL;
-}
-
-saneopt_arg_t* saneopt_get(saneopt_t* opt, char* name) {
- int i, argc;
+char* saneopt_get(saneopt_t* opt, char* name) {
+ int i;
char* arg;
- char** argv;
- saneopt_arg_t* result;
-
- argc = opt->argc;
- argv = opt->argv;
+ char* result;
- for (i = 0; i < argc; i++) {
- arg = argv[i];
+ for (i = 0; i < opt->argc; i++) {
+ arg = opt->argv[i];
if (strncmp(arg, "--", 2) == 0) {
if (strcmp(arg + 2, name) == 0) {
- result = malloc(sizeof(saneopt_arg_t));
- if ((i + 1) < argc && argv[i + 1][0] != '-') {
- result->value = argv[i + 1];
- return result;
- }
+ return ((i + 1) < opt->argc && opt->argv[i + 1][0] != '-')
+ ? opt->argv[i + 1]
+ : "";
}
}
}
Please sign in to comment.
Something went wrong with that request. Please try again.