-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathargs_parser.c
More file actions
72 lines (60 loc) · 1.86 KB
/
args_parser.c
File metadata and controls
72 lines (60 loc) · 1.86 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
// SPDX-License-Identifier: MIT
// Copyright (C) 2026 p1k0chu
#include "args_parser.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
static void print_help(char *program_name);
app_arguments parse_args(int argc, char **argv) {
app_arguments args = {0};
enum {
NOTHING,
PATH,
} parser_state = NOTHING;
int i;
for (i = 1; i < argc; ++i) {
char *arg = argv[i];
switch (parser_state) {
case NOTHING:
if (strcmp(arg, "--help") == 0 || strcmp(arg, "-h") == 0)
print_help(argv[0]);
if (strcmp(arg, "-p") == 0 || strcmp(arg, "--path") == 0) {
parser_state = PATH;
} else if (arg[0] == '^') {
args.rev_hide = arg + 1;
} else if (strstr(arg, "..")) {
// if revision to hide is not present then
// strtok will return the second token immediately
if (arg[0] != '.') {
args.rev_hide = strtok(arg, "..");
arg = NULL;
}
args.rev_push = strtok(arg, "..");
if (strtok(NULL, "..")) {
fputs("git revision range can only have two revisions", stderr);
exit(1);
}
} else {
args.rev_push = arg;
}
break;
case PATH:
args.path = arg;
parser_state = NOTHING;
break;
}
}
if (args.path == NULL)
args.path = getenv("PWD");
return args;
}
static void print_help(char *s) {
printf("Usage: %s [options] [revision-range]\n\n", s);
puts(
"Options:\n"
"\t-h, --help:\n"
"\t\tShow help\n"
"\t-p, --path:\n"
"\t\tpath to the git repository. uses current directory if omitted");
exit(0);
}