help should be automatic

This commit is contained in:
nathants 2020-07-12 12:27:35 -07:00
parent d92b42f49c
commit d32113e538
3 changed files with 22 additions and 19 deletions

22
argh.h
View File

@ -5,15 +5,19 @@
#include <stdlib.h>
#include <string.h>
#define ARGH_PARSE \
bool argh_used = true; \
bool argh_repeat = false; \
bool argh_val = false; \
char *ARGH_ARGV[argc]; \
char *argh_name; \
int ARGH_ARGC = 0; \
int argh_diff = 0; \
int argh_offset = 0; \
#define ARGH_PARSE \
if (strcmp(argv[argc - 1], "-h") == 0 || strcmp(argv[argc - 1], "--help") == 0) { \
fprintf(stderr, "usage: %s\n", USAGE); \
exit(1); \
} \
bool argh_used = true; \
bool argh_repeat = false; \
bool argh_val = false; \
char *ARGH_ARGV[argc]; \
char *argh_name; \
int ARGH_ARGC = 0; \
int argh_diff = 0; \
int argh_offset = 0; \
while(argh_offset < argc)
// check for a flag which is either present or not

View File

@ -2,14 +2,11 @@
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "argh.h"
#define USAGE "example [-l|--lz4] [-h N|--head N] [-p|--prefix] POS1 ... POSN"
int main(int argc, char **argv) {
if (strcmp(argv[argc - 1], "-h") == 0 || strcmp(argv[argc - 1], "--help") == 0) {
fprintf(stderr, "usage: example [-l|--lz4] [-h N|--head N] [-p|--prefix] POS1 ... POSN\n");
exit(1);
}
bool prefix = false;
bool lz4 = false;
int head = 0;

View File

@ -9,13 +9,15 @@ header only argument parsing for c inspired by the simplicity of [argh](https://
## example
```c
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "argh.h"
#define USAGE "example [-l|--lz4] [-h N|--head N] [-p|--prefix] POS1 ... POSN"
int main(int argc, char **argv) {
if (strcmp(argv[argc - 1], "-h") == 0 || strcmp(argv[argc - 1], "--help") == 0) {
fprintf(stderr, "usage: example [-l|--lz4] [-h N|--head N] [-p|--prefix] POS1 ... POSN\n");
exit(1);
}
bool prefix = false;
bool lz4 = false;
int head = 0;
@ -27,7 +29,7 @@ int main(int argc, char **argv) {
}
printf("head: %d, prefix: %d, lz4: %d\n", head, prefix, lz4);
for (int i = 0; i < ARGH_ARGC; i++)
printf("pos arg %d: %s\n", i, ARGH_ARGV[i]);
printf("positional arg %d: %s\n", i, ARGH_ARGV[i]);
}
```