]>
Commit | Line | Data |
---|---|---|
0c9c969a UG |
1 | #ifndef INCLUDE_examples_args_h__ |
2 | #define INCLUDE_examples_args_h__ | |
3 | ||
4 | /** | |
5 | * Argument-processing helper structure | |
6 | */ | |
7 | struct args_info { | |
8 | int argc; | |
9 | char **argv; | |
10 | int pos; | |
11 | int opts_done : 1; /**< Did we see a -- separator */ | |
12 | }; | |
13 | #define ARGS_INFO_INIT { argc, argv, 0, 0 } | |
14 | #define ARGS_CURRENT(args) args->argv[args->pos] | |
15 | ||
16 | /** | |
17 | * Check if a string has the given prefix. Returns 0 if not prefixed | |
18 | * or the length of the prefix if it is. | |
19 | */ | |
20 | extern size_t is_prefixed(const char *str, const char *pfx); | |
21 | ||
22 | /** | |
23 | * Match an integer string, returning 1 if matched, 0 if not. | |
24 | */ | |
25 | extern int is_integer(int *out, const char *str, int allow_negative); | |
26 | ||
27 | /** | |
28 | * Check current `args` entry against `opt` string. If it matches | |
29 | * exactly, take the next arg as a string; if it matches as a prefix with | |
30 | * an equal sign, take the remainder as a string; if value not supplied, | |
31 | * default value `def` will be given. otherwise return 0. | |
32 | */ | |
33 | extern int optional_str_arg( | |
34 | const char **out, struct args_info *args, const char *opt, const char *def); | |
35 | ||
36 | /** | |
37 | * Check current `args` entry against `opt` string. If it matches | |
38 | * exactly, take the next arg as a string; if it matches as a prefix with | |
39 | * an equal sign, take the remainder as a string; otherwise return 0. | |
40 | */ | |
41 | extern int match_str_arg( | |
42 | const char **out, struct args_info *args, const char *opt); | |
43 | ||
44 | /** | |
45 | * Check current `args` entry against `opt` string parsing as uint16. If | |
46 | * `opt` matches exactly, take the next arg as a uint16_t value; if `opt` | |
47 | * is a prefix (equal sign optional), take the remainder of the arg as a | |
48 | * uint16_t value; otherwise return 0. | |
49 | */ | |
50 | extern int match_uint16_arg( | |
51 | uint16_t *out, struct args_info *args, const char *opt); | |
52 | ||
53 | /** | |
54 | * Check current `args` entry against `opt` string parsing as uint32. If | |
55 | * `opt` matches exactly, take the next arg as a uint16_t value; if `opt` | |
56 | * is a prefix (equal sign optional), take the remainder of the arg as a | |
57 | * uint32_t value; otherwise return 0. | |
58 | */ | |
59 | extern int match_uint32_arg( | |
60 | uint32_t *out, struct args_info *args, const char *opt); | |
61 | ||
62 | /** | |
63 | * Check current `args` entry against `opt` string parsing as int. If | |
64 | * `opt` matches exactly, take the next arg as an int value; if it matches | |
65 | * as a prefix (equal sign optional), take the remainder of the arg as a | |
66 | * int value; otherwise return 0. | |
67 | */ | |
68 | extern int match_int_arg( | |
69 | int *out, struct args_info *args, const char *opt, int allow_negative); | |
70 | ||
71 | /** | |
72 | * Check current `args` entry against a "bool" `opt` (ie. --[no-]progress). | |
73 | * If `opt` matches positively, out will be set to 1, or if `opt` matches | |
74 | * negatively, out will be set to 0, and in both cases 1 will be returned. | |
75 | * If neither the positive or the negative form of opt matched, out will be -1, | |
76 | * and 0 will be returned. | |
77 | */ | |
78 | extern int match_bool_arg(int *out, struct args_info *args, const char *opt); | |
79 | ||
80 | /** | |
81 | * Check if we're processing past the single -- separator | |
82 | */ | |
83 | extern int match_arg_separator(struct args_info *args); | |
84 | ||
85 | /** | |
86 | * Consume all remaining arguments in a git_strarray | |
87 | */ | |
88 | extern void strarray_from_args(git_strarray *array, struct args_info *args); | |
89 | ||
90 | #endif |