-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathargs.c
104 lines (85 loc) · 1.79 KB
/
args.c
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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
#include "args.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
const char* prog_name = NULL;
const char* repo_path = ".";
int timeout = 30; // s
void print_usage()
{
printf("Usage: %s [-r path/to/git/repo] [-t timeout_in_s]\n", prog_name);
}
bool parse_pair(char* argv[], int offset)
{
static bool timeout_set = false;
static bool repo_set = false;
if (!repo_set && strcmp(argv[offset], "-r") == 0)
{
repo_path = argv[offset+1];
repo_set = true;
return true;
}
else if (!timeout_set && strcmp(argv[offset], "-t") == 0)
{
long int t = strtol(argv[offset+1], NULL, 10);
if (t >= 1 && t <= 100000)
{
timeout = (int)t;
timeout_set = true;
}
else
{
printf("Timeout value must be between 1s and 100000s\n");
return false;
}
return true;
}
return false;
}
bool parse_args_impl(int argc, char* argv[])
{
prog_name = argv[0];
char* last_slash = NULL;
#ifdef WIN32
int delimiter = '\\';
#else
int delimiter = '/';
#endif
last_slash = strrchr(prog_name, delimiter);
if (last_slash)
prog_name = last_slash + 1;
if (argc == 2 || argc == 4 || argc > 5)
return false;
if (argc == 3)
return parse_pair(argv, 1);
if (argc == 5)
{
if (!parse_pair(argv, 1))
{
return false;
}
return parse_pair(argv, 3);
}
return true;
}
bool parse_args(int argc, char* argv[])
{
if (!parse_args_impl(argc, argv))
{
print_usage();
return false;
}
return true;
}
const char* get_prog_name()
{
return prog_name;
}
const char* get_repo_path()
{
return repo_path;
}
int get_timeout()
{
return timeout;
}