2019-02-14 19:17:02 -08:00
|
|
|
/*
|
|
|
|
* arg - A simple C program by Bruce Hill for parsing command line arguments
|
|
|
|
* Licensed under the MIT license (see LICENSE for details).
|
|
|
|
*
|
|
|
|
* Example: arg --foo a b --foo=blah --baz -xyz
|
|
|
|
* - Prints "blah" and exits with success
|
|
|
|
* Example: arg --nope a b --foo=blah --baz -xyz
|
|
|
|
* - Prints nothing and exits with failure
|
|
|
|
* Example: arg -y a b --foo=blah --baz -xyz
|
|
|
|
* - Prints nothing and exits with success
|
|
|
|
*/
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <string.h>
|
|
|
|
|
2019-02-15 01:57:15 -08:00
|
|
|
#define EXIT_SUCCESS 0
|
|
|
|
#define EXIT_NO_MATCH 1
|
|
|
|
#define EXIT_BAD_USAGE -1
|
|
|
|
|
2019-02-14 19:17:02 -08:00
|
|
|
int main(int argc, char **argv)
|
|
|
|
{
|
|
|
|
char *flag = argv[1];
|
2019-02-15 01:57:15 -08:00
|
|
|
if (!flag) {
|
|
|
|
fprintf(stderr, "Usage: arg <flag> ...\n");
|
|
|
|
return EXIT_BAD_USAGE;
|
2019-02-14 19:17:02 -08:00
|
|
|
}
|
2019-02-15 01:57:15 -08:00
|
|
|
size_t flaglen = strlen(flag);
|
2019-02-14 19:17:02 -08:00
|
|
|
// Look for: --flag=val, --flag val, --flag, -f=val, -f val, or -f
|
|
|
|
for (int i = 2; i < argc; i++) {
|
|
|
|
char *arg = argv[i];
|
2019-02-15 01:57:15 -08:00
|
|
|
if (strncmp(arg, flag, flaglen) != 0) continue;
|
|
|
|
if (arg[flaglen] == '\0') { // --flag ...
|
|
|
|
if (argv[i+1] && argv[i+1][0] != '-') // --flag <value>
|
|
|
|
puts(argv[i+1]); // value of the flag
|
|
|
|
return EXIT_SUCCESS;
|
|
|
|
} else if (arg[flaglen] == '=') { // --flag=...
|
|
|
|
puts(&arg[flaglen+1]);
|
|
|
|
return EXIT_SUCCESS;
|
2019-02-14 19:17:02 -08:00
|
|
|
}
|
|
|
|
}
|
2019-02-15 01:57:15 -08:00
|
|
|
// If flag is single-character without a value, e.g. -f, look for it
|
|
|
|
// among other single character flags like -xfy.
|
|
|
|
if (flag[0] == '-' && flaglen == 2) {
|
2019-02-14 19:17:02 -08:00
|
|
|
for (int i = 2; i < argc; i++) {
|
|
|
|
char *arg = argv[i];
|
|
|
|
if (arg[0] != '-' || arg[1] == '-')
|
|
|
|
continue; // skip foo and --foo
|
|
|
|
for (char *c = &arg[1]; *c; c++) {
|
2019-02-15 01:57:15 -08:00
|
|
|
if (*c == flag[1])
|
|
|
|
return EXIT_SUCCESS;
|
2019-02-14 19:17:02 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2019-02-15 01:57:15 -08:00
|
|
|
return EXIT_NO_MATCH;
|
2019-02-14 19:17:02 -08:00
|
|
|
}
|