| 245 | } |
| 246 | |
| 247 | void |
| 248 | argparse_usage(struct argparse *this_) |
| 249 | { |
| 250 | fprintf(stdout, "Usage: %s\n", this_->usage); |
| 251 | fputc('\n', stdout); |
| 252 | |
| 253 | const struct argparse_option *options; |
| 254 | |
| 255 | // figure out best width |
| 256 | size_t usage_opts_width = 0; |
| 257 | size_t len; |
| 258 | options = this_->options; |
| 259 | for (; options->type != ARGPARSE_OPT_END; options++) { |
| 260 | len = 0; |
| 261 | if ((options)->short_name) { |
| 262 | len += 2; |
| 263 | } |
| 264 | if ((options)->short_name && (options)->long_name) { |
| 265 | len += 2; // separator ", " |
| 266 | } |
| 267 | if ((options)->long_name) { |
| 268 | len += strlen((options)->long_name) + 2; |
| 269 | } |
| 270 | if (options->type == ARGPARSE_OPT_INTEGER) { |
| 271 | len += strlen("=<int>"); |
| 272 | } else if (options->type == ARGPARSE_OPT_STRING) { |
| 273 | len += strlen("=<str>"); |
| 274 | } |
| 275 | len = ceil((float)len / 4) * 4; |
| 276 | if (usage_opts_width < len) { |
| 277 | usage_opts_width = len; |
| 278 | } |
| 279 | } |
| 280 | usage_opts_width += 4; // 4 spaces prefix |
| 281 | |
| 282 | options = this_->options; |
| 283 | for (; options->type != ARGPARSE_OPT_END; options++) { |
| 284 | size_t pos; |
| 285 | int pad; |
| 286 | pos = fprintf(stdout, " "); |
| 287 | if (options->short_name) { |
| 288 | pos += fprintf(stdout, "-%c", options->short_name); |
| 289 | } |
| 290 | if (options->long_name && options->short_name) { |
| 291 | pos += fprintf(stdout, ", "); |
| 292 | } |
| 293 | if (options->long_name) { |
| 294 | pos += fprintf(stdout, "--%s", options->long_name); |
| 295 | } |
| 296 | if (options->type == ARGPARSE_OPT_INTEGER) { |
| 297 | pos += fprintf(stdout, "=<int>"); |
| 298 | } else if (options->type == ARGPARSE_OPT_STRING) { |
| 299 | pos += fprintf(stdout, "=<str>"); |
| 300 | } |
| 301 | if (pos <= usage_opts_width) { |
| 302 | pad = usage_opts_width - pos; |
| 303 | } else { |
| 304 | fputc('\n', stdout); |
no outgoing calls
no test coverage detected