-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathhelp.c
51 lines (40 loc) · 1.51 KB
/
help.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
/* File: help.c
Copyright (C) 2013 David Hauweele <[email protected]>
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>. */
#include <stdio.h>
#include <string.h>
#include "help.h"
void help(const char *name, const char *usage, const struct opt_help opts[])
{
const struct opt_help *opt;
int size;
int max = 0;
fprintf(stderr, "usage: %s %s\n", name, usage);
/* maximum option names size for padding */
for(opt = opts ; opt->name_long ; opt++) {
size = strlen(opt->name_long);
if(size > max)
max = size;
}
/* print options and help messages */
for(opt = opts ; opt->name_long ; opt++) {
if(opt->name_short != '\0')
fprintf(stderr, " -%c, --%s", opt->name_short, opt->name_long);
else
fprintf(stderr, " --%s", opt->name_long);
/* padding */
size = strlen(opt->name_long);
for(; size <= max ; size++)
fputc(' ', stderr);
fprintf(stderr, "%s\n", opt->help);
}
}