1 #include "git-compat-util.h"
2 #include "parse-options.h"
13 static inline const char *get_arg(struct optparse_t
*p
)
16 const char *res
= p
->opt
;
24 static inline const char *skip_prefix(const char *str
, const char *prefix
)
26 size_t len
= strlen(prefix
);
27 return strncmp(str
, prefix
, len
) ? NULL
: str
+ len
;
30 static int opterror(const struct option
*opt
, const char *reason
, int flags
)
32 if (flags
& OPT_SHORT
)
33 return error("switch `%c' %s", opt
->short_name
, reason
);
34 if (flags
& OPT_UNSET
)
35 return error("option `no-%s' %s", opt
->long_name
, reason
);
36 return error("option `%s' %s", opt
->long_name
, reason
);
39 static int get_value(struct optparse_t
*p
,
40 const struct option
*opt
, int flags
)
43 arg
= p
->opt
? p
->opt
: (p
->argc
> 1 ? p
->argv
[1] : NULL
);
45 if (p
->opt
&& (flags
& OPT_UNSET
))
46 return opterror(opt
, "takes no value", flags
);
50 if (!(flags
& OPT_SHORT
) && p
->opt
)
51 return opterror(opt
, "takes no value", flags
);
52 if (flags
& OPT_UNSET
)
53 *(int *)opt
->value
= 0;
55 (*(int *)opt
->value
)++;
59 if (flags
& OPT_UNSET
) {
60 *(const char **)opt
->value
= (const char *)NULL
;
63 if (opt
->flags
& PARSE_OPT_OPTARG
&& (!arg
|| *arg
== '-')) {
64 *(const char **)opt
->value
= (const char *)opt
->defval
;
68 return opterror(opt
, "requires a value", flags
);
69 *(const char **)opt
->value
= get_arg(p
);
73 if (flags
& OPT_UNSET
)
74 return (*opt
->callback
)(opt
, NULL
, 1);
75 if (opt
->flags
& PARSE_OPT_OPTARG
&& (!arg
|| *arg
== '-'))
76 return (*opt
->callback
)(opt
, NULL
, 0);
78 return opterror(opt
, "requires a value", flags
);
79 return (*opt
->callback
)(opt
, get_arg(p
), 0);
82 if (flags
& OPT_UNSET
) {
83 *(int *)opt
->value
= 0;
86 if (opt
->flags
& PARSE_OPT_OPTARG
&& (!arg
|| !isdigit(*arg
))) {
87 *(int *)opt
->value
= opt
->defval
;
91 return opterror(opt
, "requires a value", flags
);
92 *(int *)opt
->value
= strtol(get_arg(p
), (char **)&s
, 10);
94 return opterror(opt
, "expects a numerical value", flags
);
98 die("should not happen, someone must be hit on the forehead");
102 static int parse_short_opt(struct optparse_t
*p
, const struct option
*options
)
104 for (; options
->type
!= OPTION_END
; options
++) {
105 if (options
->short_name
== *p
->opt
) {
106 p
->opt
= p
->opt
[1] ? p
->opt
+ 1 : NULL
;
107 return get_value(p
, options
, OPT_SHORT
);
110 return error("unknown switch `%c'", *p
->opt
);
113 static int parse_long_opt(struct optparse_t
*p
, const char *arg
,
114 const struct option
*options
)
116 for (; options
->type
!= OPTION_END
; options
++) {
120 if (!options
->long_name
)
123 rest
= skip_prefix(arg
, options
->long_name
);
125 if (strncmp(arg
, "no-", 3))
128 rest
= skip_prefix(arg
+ 3, options
->long_name
);
137 return get_value(p
, options
, flags
);
139 return error("unknown option `%s'", arg
);
142 int parse_options(int argc
, const char **argv
, const struct option
*options
,
143 const char * const usagestr
[], int flags
)
145 struct optparse_t args
= { argv
+ 1, argc
- 1, NULL
};
148 for (; args
.argc
; args
.argc
--, args
.argv
++) {
149 const char *arg
= args
.argv
[0];
151 if (*arg
!= '-' || !arg
[1]) {
152 argv
[j
++] = args
.argv
[0];
159 if (*args
.opt
== 'h')
160 usage_with_options(usagestr
, options
);
161 if (parse_short_opt(&args
, options
) < 0)
162 usage_with_options(usagestr
, options
);
167 if (!arg
[2]) { /* "--" */
168 if (!(flags
& PARSE_OPT_KEEP_DASHDASH
)) {
175 if (!strcmp(arg
+ 2, "help"))
176 usage_with_options(usagestr
, options
);
177 if (parse_long_opt(&args
, arg
+ 2, options
))
178 usage_with_options(usagestr
, options
);
181 memmove(argv
+ j
, args
.argv
, args
.argc
* sizeof(*argv
));
182 argv
[j
+ args
.argc
] = NULL
;
183 return j
+ args
.argc
;
186 #define USAGE_OPTS_WIDTH 24
189 void usage_with_options(const char * const *usagestr
,
190 const struct option
*opts
)
192 fprintf(stderr
, "usage: %s\n", *usagestr
++);
193 while (*usagestr
&& **usagestr
)
194 fprintf(stderr
, " or: %s\n", *usagestr
++);
196 fprintf(stderr
, " %s\n", *usagestr
++);
198 if (opts
->type
!= OPTION_GROUP
)
201 for (; opts
->type
!= OPTION_END
; opts
++) {
205 if (opts
->type
== OPTION_GROUP
) {
208 fprintf(stderr
, "%s\n", opts
->help
);
212 pos
= fprintf(stderr
, " ");
213 if (opts
->short_name
)
214 pos
+= fprintf(stderr
, "-%c", opts
->short_name
);
215 if (opts
->long_name
&& opts
->short_name
)
216 pos
+= fprintf(stderr
, ", ");
218 pos
+= fprintf(stderr
, "--%s", opts
->long_name
);
220 switch (opts
->type
) {
222 if (opts
->flags
& PARSE_OPT_OPTARG
)
223 pos
+= fprintf(stderr
, " [<n>]");
225 pos
+= fprintf(stderr
, " <n>");
228 case OPTION_CALLBACK
:
230 if (opts
->flags
& PARSE_OPT_OPTARG
)
231 pos
+= fprintf(stderr
, " [<%s>]", opts
->argh
);
233 pos
+= fprintf(stderr
, " <%s>", opts
->argh
);
235 if (opts
->flags
& PARSE_OPT_OPTARG
)
236 pos
+= fprintf(stderr
, " [...]");
238 pos
+= fprintf(stderr
, " ...");
245 if (pos
<= USAGE_OPTS_WIDTH
)
246 pad
= USAGE_OPTS_WIDTH
- pos
;
249 pad
= USAGE_OPTS_WIDTH
;
251 fprintf(stderr
, "%*s%s\n", pad
+ USAGE_GAP
, "", opts
->help
);
258 /*----- some often used options -----*/
260 int parse_opt_abbrev_cb(const struct option
*opt
, const char *arg
, int unset
)
265 v
= unset
? 0 : DEFAULT_ABBREV
;
267 v
= strtol(arg
, (char **)&arg
, 10);
269 return opterror(opt
, "expects a numerical value", 0);
270 if (v
&& v
< MINIMUM_ABBREV
)
275 *(int *)(opt
->value
) = v
;