2021-02-07 22:31:04 +03:00
|
|
|
### Option
|
2020-12-28 02:52:22 +03:00
|
|
|
|
2021-02-06 10:19:31 +03:00
|
|
|
- A basic alternative to getopt but quite limited compared to it.
|
2021-02-03 08:09:50 +03:00
|
|
|
- Long or short version of options are supported. Values are accepted only with
|
|
|
|
'=' sign.
|
|
|
|
- --address=127.0.0.1
|
|
|
|
- -a=127.0.0.1
|
2020-12-28 02:52:22 +03:00
|
|
|
|
|
|
|
```c
|
|
|
|
|
|
|
|
#include "sc_option.h"
|
|
|
|
|
|
|
|
static struct sc_option_item options[] = {{.letter = 'm', .name = NULL},
|
|
|
|
{.letter = 'k', .name = "key"},
|
|
|
|
{.letter = 'h', .name = "help"}};
|
|
|
|
|
|
|
|
int main(int argc, char *argv[])
|
|
|
|
{
|
|
|
|
char *value;
|
|
|
|
|
|
|
|
struct sc_option opt = {.argv = argv,
|
|
|
|
.count = sizeof(options) / sizeof(options[0]),
|
|
|
|
.options = options};
|
|
|
|
|
|
|
|
for (int i = 1; i < argc; i++) {
|
|
|
|
char c = sc_option_at(&opt, i, &value);
|
|
|
|
switch (c) {
|
|
|
|
case 'm':
|
|
|
|
// If value does not exist, it will point to '\0' character.
|
|
|
|
printf("Option 'm', value : %s \n", value);
|
|
|
|
break;
|
|
|
|
case 'k':
|
|
|
|
printf("Option 'k', value : %s \n", value);
|
|
|
|
break;
|
|
|
|
case 'h':
|
|
|
|
printf("Option 'h', value : %s \n", value);
|
|
|
|
break;
|
|
|
|
case '?':
|
|
|
|
printf("Unknown option : %s \n", argv[i]);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}```
|
|
|
|
|