-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtl_flag.h
More file actions
143 lines (130 loc) · 4.01 KB
/
tl_flag.h
File metadata and controls
143 lines (130 loc) · 4.01 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
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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
// See LICENSE.txt and CONTRIBUTING.md for details.
#ifndef TL_FLAG_H
#define TL_FLAG_H
#include <stdbool.h>
#include <stddef.h>
/**
* @brief Represents a parsed flag.
*
* `name` points into argv (or the tokenizer buffer) at the first '-' of
* the flag. `name_len` is the length up to '\0' or '='. For the '=' form
* `name` is NOT a NUL-terminated C string at name_len — name[name_len] is
* '=', so comparisons must use memcmp with name_len, never strcmp.
*
* argv entry: "--foo=bar"
* - - f o o = b a r \0
* ^ ^
* name value
* name_len = 5
* value = "bar"
*
* argv entry: "--foo" "bar"
* - - f o o \0 b a r \0
* ^ ^
* name value
* name_len = 5
* value = "bar"
*
* argv entry: "--foo" (boolean, no value)
* - - f o o \0
* ^
* name
* name_len = 5
* value = NULL
*/
typedef struct {
const char *name; // points at the first '-' of the flag in argv
size_t name_len; // length of the flag name up to '\0' or '='
const char *value; // value after first '=', or NULL if none
} tl_flag_t;
/**
* @brief Parses the given command line arguments.
*
* Parses argv into flags. A flag is anything starting with "--". It can carry
* a value written as --name=value, or as --name value in the next entry.
* A bare "--" ends flag parsing; everything after it is a positional, even if
* it starts with dashes. Any previously parsed state is thrown away first.
*
* @param argc The number of command line arguments.
* @param argv The command line arguments.
*
* @return void
*/
void tl_parse_args(int argc, char *argv[]);
/**
* @brief Parses a raw command line string.
*
* Splits the line into tokens. Double quotes group text with spaces into
* one token, and a backslash keeps the next character as-is. The first
* token is the program name and is skipped, like argv[0].
*
* @param line The command line string to parse.
*
* @return true on success, false otherwise.
*/
bool tl_parse_line(const char *line);
/**
* @brief Releases memory held by the argument parser.
*
* Safe to call when nothing has been parsed. Called implicitly by
* tl_parse_args and tl_parse_line.
*
* @return void
*/
void tl_free_args(void);
/**
* @brief Looks up a specific flag.
*
* @param flag The flag to look up.
*
* @return true if the flag is found, false otherwise.
*/
bool tl_lookup_flag(const char *flag);
/**
* @brief Returns the value of a specific flag.
*
* Returns the value of the first occurrence of flag. For repeated flags
* use tl_count_flag and tl_get_flag_at.
*
* @param flag The flag to get.
*
* @return The value of the flag, or NULL if not found or no value.
*/
const char *tl_get_flag(const char *flag);
/**
* @brief Returns the number of times a flag was given.
*
* @param flag The flag to count.
*
* @return The occurrence count (0 if not given).
*/
size_t tl_count_flag(const char *flag);
/**
* @brief Returns the value of a repeated flag at a given index.
*
* Occurrences are indexed in the order they appeared on the command line.
*
* @param flag The flag to get.
* @param index The occurrence index (0-based).
*
* @return The value, or NULL if out of range or no value at that index.
*/
const char *tl_get_flag_at(const char *flag, size_t index);
/**
* @brief Returns the number of positional arguments.
*
* Positionals are bare arguments (not starting with `--`) and everything
* after a bare `--` terminator, in the order they appeared.
*
* @return The positional argument count.
*/
size_t tl_count_positional(void);
/**
* @brief Returns the positional argument at the given index.
*
* @param index The positional index (0-based).
*
* @return The positional value, or NULL if out of range.
*/
const char *tl_get_positional(size_t index);
#endif // TL_FLAG_H