This repository has been archived by the owner on Jan 3, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathoptions.c
87 lines (83 loc) · 2.16 KB
/
options.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
// SPDX-License-Identifier: BSD-3-Clause
// Copyright (C) 2003-2014 Brian Harring <ferringb@gmail.com>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <errno.h>
#include <diffball/defs.h>
#include "options.h"
void print_usage(const char *prog, const char *usage_portion, struct usage_options *text, int exit_code)
{
struct usage_options *u_ptr;
print_version(prog);
u_ptr = text;
unsigned int long_len = 0;
while (1)
{
if (u_ptr->long_arg == NULL && u_ptr->description == NULL)
break;
if (u_ptr->long_arg != NULL)
{
long_len = MAX(strlen(u_ptr->long_arg), long_len);
}
u_ptr++;
}
long_len = MIN(15, long_len);
fprintf(stdout, " usage: %s [flags] ", prog);
if (usage_portion)
fprintf(stdout, "%s", usage_portion);
fprintf(stdout, "\n\n");
u_ptr = text;
while (1)
{
if (u_ptr->long_arg == NULL && u_ptr->description == NULL)
break;
if (0 != u_ptr->short_arg)
{
fprintf(stdout, " -%c ", u_ptr->short_arg);
if (NULL != u_ptr->long_arg)
fprintf(stdout, "--%-*s", long_len, u_ptr->long_arg);
else
fprintf(stdout, " %*s", long_len, "");
if (NULL != u_ptr->description)
fprintf(stdout, " %s\n", u_ptr->description);
else
fprintf(stdout, "\n");
}
else if (NULL != u_ptr->long_arg)
{
fprintf(stdout, " --%-*s", long_len, u_ptr->long_arg);
if (NULL != u_ptr->description)
fprintf(stdout, " %s\n", u_ptr->description);
else
fprintf(stdout, "\n");
}
else if (u_ptr->description != NULL)
{
// description fluff
fprintf(stdout, "\n%s\n", u_ptr->description);
}
else
{
// all opts exhausted. end of usage struct aparently (else someone screwed up)
break;
}
u_ptr++;
}
fprintf(stdout, "\n");
exit(exit_code);
}
void print_version(const char *prog)
{
fprintf(stdout, "diffball version %s, program %s (C) 2003-2021 Brian Harring\n", VERSION, prog);
fprintf(stdout, "https://github.com/ferringb/diffball\n");
fprintf(stdout, "THIS SOFTWARE COMES WITH ABSOLUTELY NO WARRANTY! USE AT YOUR OWN RISK!\n");
fprintf(stdout, "Report bugs to <ferringb@gmail.com>\n\n");
}
char *
get_next_arg(int argc, char **argv)
{
if (argc > optind)
return argv[optind++];
return NULL;
}