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 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033
|
/* libnih
*
* option.c - command-line argument and option parsing
*
* Copyright © 2009 Scott James Remnant <scott@netsplit.com>.
* Copyright © 2009 Canonical Ltd.
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License version 2, as
* published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along
* with this program; if not, write to the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/
#ifdef HAVE_CONFIG_H
# include <config.h>
#endif /* HAVE_CONFIG_H */
#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <nih/macros.h>
#include <nih/alloc.h>
#include <nih/string.h>
#include <nih/main.h>
#include <nih/option.h>
#include <nih/logging.h>
/**
* NihOptionCtx:
* @parent: parent object for returned array,
* @argc: number of arguments,
* @argv: arguments array,
* @options: list of options,
* @nargs: number of non-option arguments,
* @args: non-option arguments to return,
* @arg: current argument index,
* @nonopt: index of first non-option argument,
* @optend: index of end of options.
*
* This structure is passed between the various parsing functions to avoid
* having to pass a dozen different variables around and to keep us
* re-entrant.
**/
typedef struct nih_option_ctx {
const void *parent;
int argc;
char **argv;
NihOption *options;
size_t nargs;
char **args;
int arg;
int nonopt;
int optend;
} NihOptionCtx;
/* Prototypes for static functions */
static NihOption * nih_option_get_short (NihOptionCtx *ctx, int option);
static int nih_option_short (NihOptionCtx *ctx);
static NihOption * nih_option_get_long (NihOptionCtx *ctx,
const char *option, size_t len);
static int nih_option_long (NihOptionCtx *ctx);
static int nih_option_handle (NihOptionCtx *ctx, NihOption *opt);
static int nih_option_handle_arg (NihOptionCtx *ctx, NihOption *opt,
const char *arg);
static const char *nih_option_next_nonopt (NihOptionCtx *ctx);
static void nih_option_help (NihOption *options);
static void nih_option_group_help (NihOptionGroup *group,
NihOption *options,
NihOptionGroup **groups);
/**
* default_options:
*
* These default options are appended to those defined by the user
* so they can be overriden.
**/
static const NihOption default_options[] = {
{ 'q', "quiet",
N_("reduce output to errors only"),
NULL, NULL, NULL, nih_option_quiet },
{ 'v', "verbose",
N_("increase output to include informational messages"),
NULL, NULL, NULL, nih_option_verbose },
/* Deliberately hidden, you get told about this when you file
* a bug ;-)
*/
{ 0, "debug", NULL, NULL, NULL, NULL, nih_option_debug },
{ 0, "help",
N_("display this help and exit"),
NULL, NULL, NULL, NULL },
{ 0, "version",
N_("output version information and exit"),
NULL, NULL, NULL, NULL },
NIH_OPTION_LAST
};
/**
* usage_stem:
*
* This string is prepended to the program's usage line if given.
**/
static const char *usage_stem = NULL;
/**
* usage_string:
*
* This string is appended to the program's usage line if given.
**/
static const char *usage_string = NULL;
/**
* synopsis_string:
*
* This string is output after the program's usage string if given.
**/
static const char *synopsis_string = NULL;
/**
* help_string:
*
* This string is output after the options if given.
**/
static const char *help_string = NULL;
/**
* footer_string:
*
* This string is output after the options and help if given.
**/
static const char *footer_string = NULL;
/**
* nih_option_parser:
* @parent: parent object for returned array,
* @argc: number of arguments,
* @argv: command-line arguments,
* @options: options to look for,
* @break_nonopt: stop processing options at first non-option argument.
*
* Parses the command-line arguments given in @argv looking for options
* described in @options, or those built-in. Options are handled according
* to common UNIX semantics so that short options may be grouped together
* and arguments need not immediately follow the option that requires it.
*
* Remaining non-option arguments are placed into an array for processing
* by the caller. If @break_nonopt is FALSE then the first non-option
* argument concludes option processing and all subsequent options are
* considered to be ordinary arguments; this is most useful when the
* first argument should be a command.
*
* Both the array itself, and the array items are allocated with nih_alloc();
* the items are children of the array, so it is only necessary to call
* nih_free() on the array.
*
* If @parent is not NULL, it should be a pointer to another object which
* will be used as a parent for the returned array. When all parents
* of the returned array are freed, the returned array will also be
* freed.
*
* Errors are handled by printing a message to standard error.
*
* Returns: non-option arguments array or NULL on error.
**/
char **
nih_option_parser (const void *parent,
int argc,
char *argv[],
NihOption *options,
int break_nonopt)
{
NihOptionCtx ctx;
nih_assert (argc > 0);
nih_assert (argv != NULL);
nih_assert (options != NULL);
ctx.parent = parent;
ctx.argc = argc;
ctx.argv = argv;
ctx.options = nih_option_join (NULL, options, default_options);
ctx.nargs = 0;
ctx.args = NIH_MUST (nih_str_array_new (parent));
ctx.nonopt = 0;
ctx.optend = 0;
/* Iterate the arguments looking for options */
for (ctx.arg = 1; ctx.arg < argc; ctx.arg++) {
char *arg;
arg = ctx.argv[ctx.arg];
if ((arg[0] != '-') || (arg[1] == '\0')
|| (ctx.optend && ctx.arg > ctx.optend)) {
/* Not an option */
if (ctx.arg > ctx.nonopt) {
NIH_MUST (nih_str_array_add
(&ctx.args, parent, &ctx.nargs,
ctx.argv[ctx.arg]));
if (break_nonopt)
ctx.optend = ctx.arg;
}
} else if (arg[1] != '-') {
/* Short option */
if (nih_option_short (&ctx) < 0)
goto error;
} else if (arg[2] != '\0') {
/* Long option */
if (nih_option_long (&ctx) < 0)
goto error;
} else {
/* End of options */
ctx.optend = ctx.arg;
}
}
nih_free (ctx.options);
return ctx.args;
error:
nih_free (ctx.options);
nih_free (ctx.args);
return NULL;
}
/**
* nih_option_get_short:
* @ctx: parsing context,
* @option: option to find.
*
* Find the option structure with the given short @option. If an option
* exists with the short option '-' this is used instead if no specific
* option is found.
*
* Returns; pointer to option, or NULL if not found.
**/
static NihOption *
nih_option_get_short (NihOptionCtx *ctx,
int option)
{
NihOption *opt, *catch = NULL;
for (opt = ctx->options; (opt->option || opt->long_option); opt++) {
if (opt->option == '-')
catch = opt;
if (opt->option == option)
return opt;
}
return catch;
}
/**
* nih_option_short:
* @ctx: parsing context.
*
* Process the current argument as a list of short options, handling
* each one individually.
*
* If the first option in the list expects a value, then the rest of
* the argument is taken to be the option argument rather than further
* options.
*
* Returns: zero on success, negative value on invalid option.
**/
static int
nih_option_short (NihOptionCtx *ctx)
{
char *ptr;
nih_assert (ctx != NULL);
nih_assert (program_name != NULL);
for (ptr = ctx->argv[ctx->arg] + 1; *ptr != '\0'; ptr++) {
NihOption *opt;
opt = nih_option_get_short (ctx, *ptr);
if (! opt) {
fprintf (stderr, _("%s: invalid option: -%c\n"),
program_name, *ptr);
nih_main_suggest_help ();
return -1;
}
/* If the option takes an argument, this is the first
* option in the list and there are further characters;
* treat the rest as the argument
*/
if (opt->arg_name && (ptr[-1] == '-') && (ptr[1] != '\0')) {
if (nih_option_handle_arg (ctx, opt, ptr + 1) < 0)
return -1;
break;
}
/* Otherwise it's an ordinary option */
if (nih_option_handle (ctx, opt) < 0)
return -1;
}
return 0;
}
/**
* nih_option_get_long:
* @ctx: parsing context,
* @option: option to find,
* @len: length of option.
*
* Find the option structure with the given long @option, of which only
* the first @len characters will be read. If an option named "--" exists
* then it is used if no other option could be found.
*
* Returns; pointer to option, or NULL if not found.
**/
static NihOption *
nih_option_get_long (NihOptionCtx *ctx,
const char *option,
size_t len)
{
NihOption *opt, *catch = NULL;
for (opt = ctx->options; (opt->option || opt->long_option); opt++) {
if (! opt->long_option)
continue;
if (! strcmp (opt->long_option, "--"))
catch = opt;
if (strlen (opt->long_option) > len)
continue;
if (! strncmp (option, opt->long_option, len))
return opt;
}
return catch;
}
/**
* nih_option_long:
* @ctx: parsing context.
*
* Process the current argument as a long option to be handled.
*
* If the option expects a value then it may be separated from the option
* name by an '=' sign.
*
* Returns: zero on success, negative value on invalid option.
**/
static int
nih_option_long (NihOptionCtx *ctx)
{
NihOption *opt;
char *arg, *ptr;
size_t len;
nih_assert (ctx != NULL);
nih_assert (program_name != NULL);
/* Check for an equals sign that separates the option name from
* an argument.
*/
arg = ctx->argv[ctx->arg] + 2;
ptr = strchr (arg, '=');
len = (ptr ? (size_t)(ptr - arg) : strlen (arg));
/* Find the option */
opt = nih_option_get_long (ctx, arg, len);
if (! opt) {
fprintf (stderr, _("%s: invalid option: --%s\n"),
program_name, arg);
nih_main_suggest_help ();
return -1;
}
/* Handle the case where there's an argument; either we need
* to process it, or error
*/
if (ptr != NULL) {
if (opt->arg_name) {
if (nih_option_handle_arg (ctx, opt, ptr + 1) < 0)
return -1;
return 0;
} else {
fprintf (stderr, _("%s: unexpected argument: --%s\n"),
program_name, arg);
nih_main_suggest_help ();
return -1;
}
}
/* Otherwise it's an ordinary option */
if (nih_option_handle (ctx, opt) < 0)
return -1;
return 0;
}
/**
* nih_option_handle:
* @ctx: parsing context,
* @opt: option to handle.
*
* Handle an option which either does not take an argument, or should
* take the next non-option argument from the command-line. For options
* with arguments, this calls nih_option_handle_arg(); for those without,
* this calls the setter function or treats the value member as a pointer
* to an int to store TRUE in.
*
* Returns: zero on success, negative value on invalid option.
**/
static int
nih_option_handle (NihOptionCtx *ctx,
NihOption *opt)
{
nih_assert (ctx != NULL);
nih_assert (opt != NULL);
/* Handle the special cased options first */
if (opt->long_option && (! strcmp (opt->long_option, "help"))) {
/* --help */
nih_option_help (ctx->options);
nih_free (ctx->options);
nih_free (ctx->args);
exit (0);
} else if (opt->long_option
&& (! strcmp (opt->long_option, "version"))) {
/* --version */
nih_main_version ();
nih_free (ctx->options);
nih_free (ctx->args);
exit (0);
}
if (opt->arg_name) {
const char *arg;
arg = nih_option_next_nonopt (ctx);
if (! arg) {
fprintf (stderr, _("%s: missing argument: %s\n"),
program_name, ctx->argv[ctx->arg]);
nih_main_suggest_help ();
return -1;
}
return nih_option_handle_arg (ctx, opt, arg);
} else if (opt->setter) {
return opt->setter (opt, NULL);
} else if (opt->value) {
int *value = (int *)opt->value;
*value = TRUE;
}
return 0;
}
/**
* nih_option_handle_arg:
* @ctx: parsing context,
* @opt: option to handle,
* @arg: argument.
*
* Handle an option which has the argument specified, either calling
* the setter function or treating the value member (if present) as the
* address of a char * to store the duplicated argument value in.
*
* Returns: zero on success, negative value on invalid option.
**/
static int
nih_option_handle_arg (NihOptionCtx *ctx,
NihOption *opt,
const char *arg)
{
nih_assert (ctx != NULL);
nih_assert (opt != NULL);
nih_assert (opt->arg_name != NULL);
nih_assert (arg != NULL);
if (opt->setter) {
return opt->setter (opt, arg);
} else if (opt->value) {
char **value = (char **)opt->value;
if (*value)
nih_free (*value);
*value = NIH_MUST (nih_strdup (ctx->parent, arg));
}
return 0;
}
/**
* nih_option_next_nonopt:
* @ctx: parsing context.
*
* Iterates the command-line arguments looking for the next argument
* that is not an option. Updates the nonopt member of @ctx to point
* at the option used.
*
* Returns: next non-option argument or NULL if none remain.
**/
static const char *
nih_option_next_nonopt (NihOptionCtx *ctx)
{
nih_assert (ctx != NULL);
if (ctx->nonopt < ctx->arg)
ctx->nonopt = ctx->arg;
while (++ctx->nonopt < ctx->argc) {
char *arg;
arg = ctx->argv[ctx->nonopt];
if ((arg[0] != '-')
|| (ctx->optend && ctx->nonopt > ctx->optend)) {
return arg;
} else if ((arg[1] == '-') && (arg[2] == '\0')) {
/* End of options */
ctx->optend = ctx->nonopt;
}
}
return NULL;
}
/**
* nih_option_join:
* @parent: parent object for new array,
* @a: first option array,
* @b: second option array.
*
* Joins the two option arrays together to produce a combined array containing
* the options from @a followed by the options from @b.
*
* The new list is allocated with nih_alloc(), but the members are just
* copied in from @a and @b including any pointers therein. Freeing the
* new array with nih_free() is entirely safe.
*
* If @parent is not NULL, it should be a pointer to another object which
* will be used as a parent for the returned array. When all parents
* of the returned array are freed, the returned array will also be
* freed.
*
* Returns: combined option array.
**/
NihOption *
nih_option_join (const void *parent,
const NihOption *a,
const NihOption *b)
{
const NihOption *opt;
NihOption *opts;
size_t alen = 0, blen = 0;
nih_assert (a != NULL);
nih_assert (b != NULL);
/* Count options in first list */
for (opt = a; (opt->option || opt->long_option); opt++)
alen++;
/* Count options in second list */
for (opt = b; (opt->option || opt->long_option); opt++)
blen++;
/* Allocate combined list */
opts = NIH_MUST (nih_alloc (parent,
sizeof (NihOption) * (alen + blen + 1)));
/* Copy options, making sure to copy the last option from b */
memcpy (opts, a, sizeof (NihOption) * alen);
memcpy (opts + alen, b, sizeof (NihOption) * (blen + 1));
return opts;
}
/**
* nih_option_count:
* @option: NihOption invoked,
* @arg: argument to parse.
*
* This option setter may be used to create arguments that count the number
* of times they are placed on the command line.
*
* The value member of @option must be a pointer to an integer variable,
* the arg_name member must be NULL.
*
* Returns: always returns zero.
**/
int
nih_option_count (NihOption *option,
const char *arg)
{
int *value;
nih_assert (option != NULL);
nih_assert (option->value != NULL);
nih_assert (arg == NULL);
value = (int *)option->value;
(*value)++;
return 0;
}
/**
* nih_option_int:
* @option: NihOption invoked,
* @arg: argument to parse.
*
* This option setter may be used to parse an integer from the command line
* and store it in the value member of @option, which must be a pointer to
* an integer variable.
*
* The arg_name member of @option must not be NULL.
*
* Returns: zero on success, non-zero on error.
**/
int
nih_option_int (NihOption *option,
const char *arg)
{
char *endptr;
int *value;
nih_assert (option != NULL);
nih_assert (option->value != NULL);
nih_assert (arg != NULL);
value = (int *)option->value;
*value = strtol (arg, &endptr, 10);
if (*endptr) {
fprintf (stderr, _("%s: illegal argument: %s\n"),
program_name, arg);
nih_main_suggest_help ();
return -1;
}
return 0;
}
/**
* nih_option_quiet:
* @option: NihOption invoked,
* @arg: argument to parse.
*
* This option setter is used by the built-in -q/--quiet option to set the
* default logging level to ERROR.
*
* Returns: always returns zero.
**/
int
nih_option_quiet (NihOption *option,
const char *arg)
{
nih_assert (option != NULL);
nih_assert (arg == NULL);
nih_log_set_priority (NIH_LOG_ERROR);
return 0;
}
/**
* nih_option_verbose:
* @option: NihOption invoked,
* @arg: argument to parse.
*
* This option setter is used by the built-in -v/--verbose option to set the
* default logging level to INFO.
*
* Returns: always returns zero.
**/
int
nih_option_verbose (NihOption *option,
const char *arg)
{
nih_assert (option != NULL);
nih_assert (arg == NULL);
nih_log_set_priority (NIH_LOG_INFO);
return 0;
}
/**
* nih_option_debug:
* @option: NihOption invoked,
* @arg: argument to parse.
*
* This option setter is used by the built-in --debug option to set the
* default logging level to DEBUG.
*
* Returns: always returns zero.
**/
int
nih_option_debug (NihOption *option,
const char *arg)
{
nih_assert (option != NULL);
nih_assert (arg == NULL);
nih_log_set_priority (NIH_LOG_DEBUG);
return 0;
}
/**
* nih_option_set_usage_stem:
* @usage: usage stem.
*
* Set the usage stem prepended to the program usage line in the help output,
* this should be a static translated string.
*
* The string should not be terminated with a newline.
**/
void
nih_option_set_usage_stem (const char *usage)
{
usage_stem = usage;
}
/**
* nih_option_set_usage:
* @usage: usage string.
*
* Set the usage string appended to the program usage line in the help output,
* this should be a static translated string.
*
* The string should not be terminated with a newline.
**/
void
nih_option_set_usage (const char *usage)
{
usage_string = usage;
}
/**
* nih_option_set_synopsis:
* @synopsis: synopsis string.
*
* Set the synopsis string, shown after the program usage line in the help
* output. This should be a static translated string. It will be
* automatically wrapped to the screen width.
*
* The string should not be terminated with a newline.
**/
void
nih_option_set_synopsis (const char *synopsis)
{
synopsis_string = synopsis;
}
/**
* nih_option_set_help:
* @help: help string.
*
* Set the help string, this is displayed after the options in the help
* output. This should be a static translated string. It will be
* automatically wrapped to the screen width.
*
* The string should not be terminated with a newline.
**/
void
nih_option_set_help (const char *help)
{
help_string = help;
}
/**
* nih_option_set_footer:
* @footer: footer string.
*
* Set the footer string, this is displayed after the options and help
* text in the output. This should be a static translated string.
*
* The string should not be terminated with a newline.
**/
void
nih_option_set_footer (const char *footer)
{
footer_string = footer;
}
/**
* nih_option_help:
* @options: program options list.
*
* Output a description of the program's options to standard output
* grouped by the group member of the option.
**/
static void
nih_option_help (NihOption *options)
{
NihOption *opt;
nih_local NihOptionGroup **groups = NULL;
size_t group, ngroups;
int other = FALSE;
nih_assert (program_name != NULL);
/* Count the number of option groups */
ngroups = 0;
for (opt = options; (opt->option || opt->long_option); opt++) {
if (! opt->group) {
other = TRUE;
continue;
}
for (group = 0; group < ngroups; group++) {
if (groups[group] == opt->group)
break;
}
if (group < ngroups)
continue;
groups = NIH_MUST (nih_realloc (groups, NULL,
(sizeof (NihOptionGroup *)
* (ngroups + 1))));
groups[ngroups++] = opt->group;
}
printf ("%s: %s", _("Usage"), program_name);
if (usage_stem) {
printf (" %s", usage_stem);
} else {
printf (" %s", _("[OPTION]..."));
}
if (usage_string)
printf (" %s", usage_string);
printf ("\n");
/* Wrap the synopsis to the screen width */
if (synopsis_string) {
nih_local char *str;
str = NIH_MUST (nih_str_screen_wrap (NULL, synopsis_string,
0, 0));
printf ("%s\n", str);
}
printf ("\n");
/* Iterate the option groups we found in order, and display
* only their options
*/
for (group = 0; group < ngroups; group++)
nih_option_group_help (groups[group], options, groups);
/* Display the other group */
if (other)
nih_option_group_help (NULL, options, groups);
/* Wrap the help to the screen width */
if (help_string) {
nih_local char *str;
str = NIH_MUST (nih_str_screen_wrap (NULL, help_string, 0, 0));
printf ("%s\n", str);
if (package_bugreport || footer_string)
printf ("\n");
}
/* Append the footer */
if (footer_string) {
printf ("%s\n", footer_string);
if (package_bugreport)
printf ("\n");
}
/* Append the bug report address */
if (package_bugreport) {
if (strchr (package_bugreport, '@')) {
printf (_("Report bugs to <%s>\n"), package_bugreport);
} else {
printf (_("Report bugs at <%s>\n"), package_bugreport);
}
}
}
/**
* nih_option_group_help:
* @group: group to display,
* @options: program options list,
* @groups: all groups.
*
* Output a list of the program's options in the given option group to
* standard output.
**/
static void
nih_option_group_help (NihOptionGroup *group,
NihOption *options,
NihOptionGroup **groups)
{
NihOption *opt;
size_t width;
nih_assert (options != NULL);
if (group) {
printf (_("%s options:\n"), _(group->title));
} else if (groups) {
printf (_("Other options:\n"));
} else {
printf (_("Options:\n"));
}
width = nih_max (nih_str_screen_width (), 50U) - 31;
for (opt = options; (opt->option || opt->long_option); opt++) {
nih_local char *str = NULL;
char *ptr;
size_t len = 0;
if (opt->group != group)
continue;
if (! opt->help)
continue;
/* Indent by two spaces */
printf (" ");
len += 2;
/* Display the short option */
if (opt->option) {
printf ("-%c", opt->option);
len += 2;
/* Seperate short and long option, or
* give the argument name
*/
if (opt->long_option) {
printf (", ");
len += 2;
} else if (opt->arg_name) {
printf (" %s", opt->arg_name);
len += strlen (opt->arg_name) + 1;
}
} else {
/* Make all long options the same indent
* whether or not there's a short one
*/
printf (" ");
len += 4;
}
/* Display the long option */
if (opt->long_option) {
printf ("--%s", opt->long_option);
len += strlen (opt->long_option) + 2;
/* With the argument name */
if (opt->arg_name) {
printf ("=%s", opt->arg_name);
len += strlen (opt->arg_name) + 1;
}
}
/* Format the help string to fit in the latter
* half of the screen
*/
str = NIH_MUST (nih_str_wrap (NULL, opt->help, width, 0, 2));
/* Write the description to the screen */
ptr = str;
while (ptr && *ptr) {
size_t linelen;
/* Not enough room on this line */
if (len > 28) {
printf ("\n");
len = 0;
}
/* Pad line up to the right column */
while (len < 30) {
printf (" ");
len++;
}
/* Output the line up until the next line */
linelen = strcspn (ptr, "\n");
printf ("%.*s\n", (int)linelen, ptr);
len = 0;
/* Skip to the next line */
ptr += linelen;
if (*ptr == '\n')
ptr++;
}
}
printf ("\n");
}
|