-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathppopts.h
109 lines (96 loc) · 3.31 KB
/
ppopts.h
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
105
106
107
108
109
#ifndef PPOPTS_H
#define PPOPTS_H
/** Pretty-print command line options.
*
* This module tries its best to print command-line options in a visually
* pleasing way:
*
* - Options, arguments and description texts are horizontally aligned.
* - Descriptions are automatically wrapped (at word boundaries) to not exceed
* the given width (except single words that don't fit)
* - Print descriptions either on the same line as the options or on the
* following one.
* - Option descriptions containing at least one newline ('\n') character are
* always printed literally (on the line after the options), indented by a
* few spaces before each line. This allows user-defined formatting mixed
* with automatic formatting.
* - Optionally hide all long descriptions (e.g. if getopt_long() is not
* available)
* - "Header" and "text" items to display text before or between options
* (headers are just like text, except that a blank line is printed _before_
* instead of _after_ it).
*
*
*
* Example code:
*
* struct ppopts o;
* ppopts_add(&o, 'o', "opt", "A", "Doe something with A.");
* ppopts_add(&o, 'f,, "filename", "FILE",
* "Read input from FILE. If not specified use standard input.");
* ppopts_add(&o, 'd', "debug", "", "Enable debug mode.");
* ppopts_add(&o, 'l', "literal, "",
* "An option with\n literal description text.");
* ppopts_print(&o, stderr, 75, 0);
*
*
* Output:
*
* -o A --opt A Do something with A.
*
* -f FILE --filename FILE Read input from FILE. If not specified use
* standard input.
*
* -d --debug Enable debug mode.
*
* -l --literal
* An option with
* literal description text.
*
*
* Copyright (c) 2014 Robin Martinjak <[email protected]>
*
* Copying and distribution of this file, with or without modification, are
* permitted in any medium without royalty provided the copyright notice
* and this notice are preserved.
*/
#include <stdio.h>
#include <limits.h>
/** Always print the descriptions on the next line */
#define PPOPTS_DESC_ON_NEXT_LINE (1 << 0)
#define PPOPTS_LONGOPT_MAX 30
#define PPOPTS_ARGNAME_MAX 10
#define PPOPTS_DESC_MAX 4096
#define PPOPTS_OPTS_MAX 50
enum { PPOPTS_HEADER = SCHAR_MAX + 1, PPOPTS_TEXT };
struct ppopts
{
struct ppopts_opt
{
int shortopt;
char longopt[PPOPTS_LONGOPT_MAX + 1];
char argname[PPOPTS_ARGNAME_MAX + 1];
char desc[PPOPTS_DESC_MAX + 1];
} options[PPOPTS_OPTS_MAX];
int maxlen_longopt, maxlen_argname;
int n;
};
#define PPOPTS_INITIALIZER \
{ \
{ \
{ \
0, "", "", "" \
} \
} \
, 0, 0, 0 \
}
void ppopts_init(struct ppopts *o);
void ppopts_add(struct ppopts *o, int shortopt, const char *longopt,
const char *argname, const char *desc, ...);
#define ppopts_add_header(o, ...) \
ppopts_add((o), PPOPTS_HEADER, "", "", __VA_ARGS__)
#define ppopts_add_text(o, ...) \
ppopts_add((o), PPOPTS_TEXT, "", "", __VA_ARGS__)
void ppopts_print(struct ppopts *o, FILE *stream, int wrap, int flags);
int ppopts_getopt(struct ppopts *o, int argc, char *const argv[]);
#endif