diff options
author | Christian Grothoff <christian@grothoff.org> | 2019-06-22 23:28:58 +0200 |
---|---|---|
committer | Christian Grothoff <christian@grothoff.org> | 2019-06-22 23:28:58 +0200 |
commit | 988ac7f121b646cba3ca44567f54b1315398505d (patch) | |
tree | 96a6e224c13bd6c692b4bf68eb0f97c632095c46 /src/util/getopt_helpers.c | |
parent | ea20a69462912d46cf4f9dc47e6c7945c9da5f97 (diff) | |
download | gnunet-988ac7f121b646cba3ca44567f54b1315398505d.tar.gz gnunet-988ac7f121b646cba3ca44567f54b1315398505d.zip |
finish #4623
Diffstat (limited to 'src/util/getopt_helpers.c')
-rw-r--r-- | src/util/getopt_helpers.c | 441 |
1 files changed, 208 insertions, 233 deletions
diff --git a/src/util/getopt_helpers.c b/src/util/getopt_helpers.c index d6ba85733..05856beee 100644 --- a/src/util/getopt_helpers.c +++ b/src/util/getopt_helpers.c | |||
@@ -26,7 +26,7 @@ | |||
26 | #include "platform.h" | 26 | #include "platform.h" |
27 | #include "gnunet_util_lib.h" | 27 | #include "gnunet_util_lib.h" |
28 | 28 | ||
29 | #define LOG(kind,...) GNUNET_log_from (kind, "util-getopt", __VA_ARGS__) | 29 | #define LOG(kind, ...) GNUNET_log_from (kind, "util-getopt", __VA_ARGS__) |
30 | 30 | ||
31 | 31 | ||
32 | /** | 32 | /** |
@@ -48,9 +48,7 @@ print_version (struct GNUNET_GETOPT_CommandLineProcessorContext *ctx, | |||
48 | 48 | ||
49 | (void) option; | 49 | (void) option; |
50 | (void) value; | 50 | (void) value; |
51 | printf ("%s v%s\n", | 51 | printf ("%s v%s\n", ctx->binaryName, version); |
52 | ctx->binaryName, | ||
53 | version); | ||
54 | return GNUNET_NO; | 52 | return GNUNET_NO; |
55 | } | 53 | } |
56 | 54 | ||
@@ -64,14 +62,13 @@ print_version (struct GNUNET_GETOPT_CommandLineProcessorContext *ctx, | |||
64 | struct GNUNET_GETOPT_CommandLineOption | 62 | struct GNUNET_GETOPT_CommandLineOption |
65 | GNUNET_GETOPT_option_version (const char *version) | 63 | GNUNET_GETOPT_option_version (const char *version) |
66 | { | 64 | { |
67 | struct GNUNET_GETOPT_CommandLineOption clo = { | 65 | struct GNUNET_GETOPT_CommandLineOption clo = {.shortName = 'v', |
68 | .shortName = 'v', | 66 | .name = "version", |
69 | .name = "version", | 67 | .description = gettext_noop ( |
70 | .description = gettext_noop("print the version number"), | 68 | "print the version number"), |
71 | .option_exclusive = 1, | 69 | .option_exclusive = 1, |
72 | .processor = &print_version, | 70 | .processor = &print_version, |
73 | .scls = (void *) version | 71 | .scls = (void *) version}; |
74 | }; | ||
75 | return clo; | 72 | return clo; |
76 | } | 73 | } |
77 | 74 | ||
@@ -111,10 +108,9 @@ format_help (struct GNUNET_GETOPT_CommandLineProcessorContext *ctx, | |||
111 | (void) value; | 108 | (void) value; |
112 | if (NULL != about) | 109 | if (NULL != about) |
113 | { | 110 | { |
114 | printf ("%s\n%s\n", | 111 | printf ("%s\n%s\n", ctx->binaryOptions, gettext (about)); |
115 | ctx->binaryOptions, | 112 | printf (_ ( |
116 | gettext (about)); | 113 | "Arguments mandatory for long options are also mandatory for short options.\n")); |
117 | printf (_("Arguments mandatory for long options are also mandatory for short options.\n")); | ||
118 | } | 114 | } |
119 | i = 0; | 115 | i = 0; |
120 | opt = ctx->allOptions; | 116 | opt = ctx->allOptions; |
@@ -147,7 +143,7 @@ format_help (struct GNUNET_GETOPT_CommandLineProcessorContext *ctx, | |||
147 | trans = ""; | 143 | trans = ""; |
148 | ml = strlen (trans); | 144 | ml = strlen (trans); |
149 | p = 0; | 145 | p = 0; |
150 | OUTER: | 146 | OUTER: |
151 | while (ml - p > 78 - slen) | 147 | while (ml - p > 78 - slen) |
152 | { | 148 | { |
153 | for (j = p + 78 - slen; j > (int) p; j--) | 149 | for (j = p + 78 - slen; j > (int) p; j--) |
@@ -199,13 +195,13 @@ OUTER: | |||
199 | struct GNUNET_GETOPT_CommandLineOption | 195 | struct GNUNET_GETOPT_CommandLineOption |
200 | GNUNET_GETOPT_option_help (const char *about) | 196 | GNUNET_GETOPT_option_help (const char *about) |
201 | { | 197 | { |
202 | struct GNUNET_GETOPT_CommandLineOption clo = { | 198 | struct GNUNET_GETOPT_CommandLineOption clo = {.shortName = 'h', |
203 | .shortName = 'h', | 199 | .name = "help", |
204 | .name = "help", | 200 | .description = gettext_noop ( |
205 | .description = gettext_noop("print this help"), | 201 | "print this help"), |
206 | .processor = format_help, | 202 | .option_exclusive = 1, |
207 | .scls = (void *) about | 203 | .processor = format_help, |
208 | }; | 204 | .scls = (void *) about}; |
209 | 205 | ||
210 | return clo; | 206 | return clo; |
211 | } | 207 | } |
@@ -252,17 +248,15 @@ increment_value (struct GNUNET_GETOPT_CommandLineProcessorContext *ctx, | |||
252 | */ | 248 | */ |
253 | struct GNUNET_GETOPT_CommandLineOption | 249 | struct GNUNET_GETOPT_CommandLineOption |
254 | GNUNET_GETOPT_option_increment_uint (char shortName, | 250 | GNUNET_GETOPT_option_increment_uint (char shortName, |
255 | const char *name, | 251 | const char *name, |
256 | const char *description, | 252 | const char *description, |
257 | unsigned int *val) | 253 | unsigned int *val) |
258 | { | 254 | { |
259 | struct GNUNET_GETOPT_CommandLineOption clo = { | 255 | struct GNUNET_GETOPT_CommandLineOption clo = {.shortName = shortName, |
260 | .shortName = shortName, | 256 | .name = name, |
261 | .name = name, | 257 | .description = description, |
262 | .description = description, | 258 | .processor = &increment_value, |
263 | .processor = &increment_value, | 259 | .scls = (void *) val}; |
264 | .scls = (void *) val | ||
265 | }; | ||
266 | 260 | ||
267 | return clo; | 261 | return clo; |
268 | } | 262 | } |
@@ -277,13 +271,12 @@ GNUNET_GETOPT_option_increment_uint (char shortName, | |||
277 | struct GNUNET_GETOPT_CommandLineOption | 271 | struct GNUNET_GETOPT_CommandLineOption |
278 | GNUNET_GETOPT_option_verbose (unsigned int *level) | 272 | GNUNET_GETOPT_option_verbose (unsigned int *level) |
279 | { | 273 | { |
280 | struct GNUNET_GETOPT_CommandLineOption clo = { | 274 | struct GNUNET_GETOPT_CommandLineOption clo = {.shortName = 'V', |
281 | .shortName = 'V', | 275 | .name = "verbose", |
282 | .name = "verbose", | 276 | .description = |
283 | .description = gettext_noop("be verbose"), | 277 | gettext_noop ("be verbose"), |
284 | .processor = &increment_value, | 278 | .processor = &increment_value, |
285 | .scls = (void *) level | 279 | .scls = (void *) level}; |
286 | }; | ||
287 | 280 | ||
288 | return clo; | 281 | return clo; |
289 | } | 282 | } |
@@ -331,17 +324,15 @@ set_one (struct GNUNET_GETOPT_CommandLineProcessorContext *ctx, | |||
331 | */ | 324 | */ |
332 | struct GNUNET_GETOPT_CommandLineOption | 325 | struct GNUNET_GETOPT_CommandLineOption |
333 | GNUNET_GETOPT_option_flag (char shortName, | 326 | GNUNET_GETOPT_option_flag (char shortName, |
334 | const char *name, | 327 | const char *name, |
335 | const char *description, | 328 | const char *description, |
336 | int *val) | 329 | int *val) |
337 | { | 330 | { |
338 | struct GNUNET_GETOPT_CommandLineOption clo = { | 331 | struct GNUNET_GETOPT_CommandLineOption clo = {.shortName = shortName, |
339 | .shortName = shortName, | 332 | .name = name, |
340 | .name = name, | 333 | .description = description, |
341 | .description = description, | 334 | .processor = &set_one, |
342 | .processor = &set_one, | 335 | .scls = (void *) val}; |
343 | .scls = (void *) val | ||
344 | }; | ||
345 | 336 | ||
346 | return clo; | 337 | return clo; |
347 | } | 338 | } |
@@ -394,15 +385,13 @@ GNUNET_GETOPT_option_string (char shortName, | |||
394 | const char *description, | 385 | const char *description, |
395 | char **str) | 386 | char **str) |
396 | { | 387 | { |
397 | struct GNUNET_GETOPT_CommandLineOption clo = { | 388 | struct GNUNET_GETOPT_CommandLineOption clo = {.shortName = shortName, |
398 | .shortName = shortName, | 389 | .name = name, |
399 | .name = name, | 390 | .argumentHelp = argumentHelp, |
400 | .argumentHelp = argumentHelp, | 391 | .description = description, |
401 | .description = description, | 392 | .require_argument = 1, |
402 | .require_argument = 1, | 393 | .processor = &set_string, |
403 | .processor = &set_string, | 394 | .scls = (void *) str}; |
404 | .scls = (void *) str | ||
405 | }; | ||
406 | 395 | ||
407 | return clo; | 396 | return clo; |
408 | } | 397 | } |
@@ -417,15 +406,14 @@ GNUNET_GETOPT_option_string (char shortName, | |||
417 | struct GNUNET_GETOPT_CommandLineOption | 406 | struct GNUNET_GETOPT_CommandLineOption |
418 | GNUNET_GETOPT_option_loglevel (char **level) | 407 | GNUNET_GETOPT_option_loglevel (char **level) |
419 | { | 408 | { |
420 | struct GNUNET_GETOPT_CommandLineOption clo = { | 409 | struct GNUNET_GETOPT_CommandLineOption clo = |
421 | .shortName = 'L', | 410 | {.shortName = 'L', |
422 | .name = "log", | 411 | .name = "log", |
423 | .argumentHelp = "LOGLEVEL", | 412 | .argumentHelp = "LOGLEVEL", |
424 | .description = gettext_noop("configure logging to use LOGLEVEL"), | 413 | .description = gettext_noop ("configure logging to use LOGLEVEL"), |
425 | .require_argument = 1, | 414 | .require_argument = 1, |
426 | .processor = &set_string, | 415 | .processor = &set_string, |
427 | .scls = (void *) level | 416 | .scls = (void *) level}; |
428 | }; | ||
429 | 417 | ||
430 | return clo; | 418 | return clo; |
431 | } | 419 | } |
@@ -470,20 +458,18 @@ set_filename (struct GNUNET_GETOPT_CommandLineProcessorContext *ctx, | |||
470 | */ | 458 | */ |
471 | struct GNUNET_GETOPT_CommandLineOption | 459 | struct GNUNET_GETOPT_CommandLineOption |
472 | GNUNET_GETOPT_option_filename (char shortName, | 460 | GNUNET_GETOPT_option_filename (char shortName, |
473 | const char *name, | 461 | const char *name, |
474 | const char *argumentHelp, | 462 | const char *argumentHelp, |
475 | const char *description, | 463 | const char *description, |
476 | char **str) | 464 | char **str) |
477 | { | 465 | { |
478 | struct GNUNET_GETOPT_CommandLineOption clo = { | 466 | struct GNUNET_GETOPT_CommandLineOption clo = {.shortName = shortName, |
479 | .shortName = shortName, | 467 | .name = name, |
480 | .name = name, | 468 | .argumentHelp = argumentHelp, |
481 | .argumentHelp = argumentHelp, | 469 | .description = description, |
482 | .description = description, | 470 | .require_argument = 1, |
483 | .require_argument = 1, | 471 | .processor = &set_filename, |
484 | .processor = &set_filename, | 472 | .scls = (void *) str}; |
485 | .scls = (void *) str | ||
486 | }; | ||
487 | 473 | ||
488 | return clo; | 474 | return clo; |
489 | } | 475 | } |
@@ -497,15 +483,15 @@ GNUNET_GETOPT_option_filename (char shortName, | |||
497 | struct GNUNET_GETOPT_CommandLineOption | 483 | struct GNUNET_GETOPT_CommandLineOption |
498 | GNUNET_GETOPT_option_logfile (char **logfn) | 484 | GNUNET_GETOPT_option_logfile (char **logfn) |
499 | { | 485 | { |
500 | struct GNUNET_GETOPT_CommandLineOption clo = { | 486 | struct GNUNET_GETOPT_CommandLineOption clo = |
501 | .shortName = 'l', | 487 | {.shortName = 'l', |
502 | .name = "logfile", | 488 | .name = "logfile", |
503 | .argumentHelp = "FILENAME", | 489 | .argumentHelp = "FILENAME", |
504 | .description = gettext_noop ("configure logging to write logs to FILENAME"), | 490 | .description = |
505 | .require_argument = 1, | 491 | gettext_noop ("configure logging to write logs to FILENAME"), |
506 | .processor = &set_filename, | 492 | .require_argument = 1, |
507 | .scls = (void *) logfn | 493 | .processor = &set_filename, |
508 | }; | 494 | .scls = (void *) logfn}; |
509 | 495 | ||
510 | return clo; | 496 | return clo; |
511 | } | 497 | } |
@@ -519,15 +505,14 @@ GNUNET_GETOPT_option_logfile (char **logfn) | |||
519 | struct GNUNET_GETOPT_CommandLineOption | 505 | struct GNUNET_GETOPT_CommandLineOption |
520 | GNUNET_GETOPT_option_cfgfile (char **fn) | 506 | GNUNET_GETOPT_option_cfgfile (char **fn) |
521 | { | 507 | { |
522 | struct GNUNET_GETOPT_CommandLineOption clo = { | 508 | struct GNUNET_GETOPT_CommandLineOption clo = |
523 | .shortName = 'c', | 509 | {.shortName = 'c', |
524 | .name = "config", | 510 | .name = "config", |
525 | .argumentHelp = "FILENAME", | 511 | .argumentHelp = "FILENAME", |
526 | .description = gettext_noop("use configuration file FILENAME"), | 512 | .description = gettext_noop ("use configuration file FILENAME"), |
527 | .require_argument = 1, | 513 | .require_argument = 1, |
528 | .processor = &set_filename, | 514 | .processor = &set_filename, |
529 | .scls = (void *) fn | 515 | .scls = (void *) fn}; |
530 | }; | ||
531 | 516 | ||
532 | return clo; | 517 | return clo; |
533 | } | 518 | } |
@@ -556,13 +541,10 @@ set_ulong (struct GNUNET_GETOPT_CommandLineProcessorContext *ctx, | |||
556 | char dummy[2]; | 541 | char dummy[2]; |
557 | 542 | ||
558 | (void) ctx; | 543 | (void) ctx; |
559 | if (1 != SSCANF (value, | 544 | if (1 != SSCANF (value, "%llu%1s", val, dummy)) |
560 | "%llu%1s", | ||
561 | val, | ||
562 | dummy)) | ||
563 | { | 545 | { |
564 | FPRINTF (stderr, | 546 | FPRINTF (stderr, |
565 | _("You must pass a number to the `%s' option.\n"), | 547 | _ ("You must pass a number to the `%s' option.\n"), |
566 | option); | 548 | option); |
567 | return GNUNET_SYSERR; | 549 | return GNUNET_SYSERR; |
568 | } | 550 | } |
@@ -581,20 +563,18 @@ set_ulong (struct GNUNET_GETOPT_CommandLineProcessorContext *ctx, | |||
581 | */ | 563 | */ |
582 | struct GNUNET_GETOPT_CommandLineOption | 564 | struct GNUNET_GETOPT_CommandLineOption |
583 | GNUNET_GETOPT_option_ulong (char shortName, | 565 | GNUNET_GETOPT_option_ulong (char shortName, |
584 | const char *name, | 566 | const char *name, |
585 | const char *argumentHelp, | 567 | const char *argumentHelp, |
586 | const char *description, | 568 | const char *description, |
587 | unsigned long long *val) | 569 | unsigned long long *val) |
588 | { | 570 | { |
589 | struct GNUNET_GETOPT_CommandLineOption clo = { | 571 | struct GNUNET_GETOPT_CommandLineOption clo = {.shortName = shortName, |
590 | .shortName = shortName, | 572 | .name = name, |
591 | .name = name, | 573 | .argumentHelp = argumentHelp, |
592 | .argumentHelp = argumentHelp, | 574 | .description = description, |
593 | .description = description, | 575 | .require_argument = 1, |
594 | .require_argument = 1, | 576 | .processor = &set_ulong, |
595 | .processor = &set_ulong, | 577 | .scls = (void *) val}; |
596 | .scls = (void *) val | ||
597 | }; | ||
598 | 578 | ||
599 | return clo; | 579 | return clo; |
600 | } | 580 | } |
@@ -620,14 +600,12 @@ set_relative_time (struct GNUNET_GETOPT_CommandLineProcessorContext *ctx, | |||
620 | const char *value) | 600 | const char *value) |
621 | { | 601 | { |
622 | struct GNUNET_TIME_Relative *val = scls; | 602 | struct GNUNET_TIME_Relative *val = scls; |
623 | 603 | ||
624 | (void) ctx; | 604 | (void) ctx; |
625 | if (GNUNET_OK != | 605 | if (GNUNET_OK != GNUNET_STRINGS_fancy_time_to_relative (value, val)) |
626 | GNUNET_STRINGS_fancy_time_to_relative (value, | ||
627 | val)) | ||
628 | { | 606 | { |
629 | FPRINTF (stderr, | 607 | FPRINTF (stderr, |
630 | _("You must pass relative time to the `%s' option.\n"), | 608 | _ ("You must pass relative time to the `%s' option.\n"), |
631 | option); | 609 | option); |
632 | return GNUNET_SYSERR; | 610 | return GNUNET_SYSERR; |
633 | } | 611 | } |
@@ -647,20 +625,18 @@ set_relative_time (struct GNUNET_GETOPT_CommandLineProcessorContext *ctx, | |||
647 | */ | 625 | */ |
648 | struct GNUNET_GETOPT_CommandLineOption | 626 | struct GNUNET_GETOPT_CommandLineOption |
649 | GNUNET_GETOPT_option_relative_time (char shortName, | 627 | GNUNET_GETOPT_option_relative_time (char shortName, |
650 | const char *name, | 628 | const char *name, |
651 | const char *argumentHelp, | 629 | const char *argumentHelp, |
652 | const char *description, | 630 | const char *description, |
653 | struct GNUNET_TIME_Relative *val) | 631 | struct GNUNET_TIME_Relative *val) |
654 | { | 632 | { |
655 | struct GNUNET_GETOPT_CommandLineOption clo = { | 633 | struct GNUNET_GETOPT_CommandLineOption clo = {.shortName = shortName, |
656 | .shortName = shortName, | 634 | .name = name, |
657 | .name = name, | 635 | .argumentHelp = argumentHelp, |
658 | .argumentHelp = argumentHelp, | 636 | .description = description, |
659 | .description = description, | 637 | .require_argument = 1, |
660 | .require_argument = 1, | 638 | .processor = &set_relative_time, |
661 | .processor = &set_relative_time, | 639 | .scls = (void *) val}; |
662 | .scls = (void *) val | ||
663 | }; | ||
664 | 640 | ||
665 | return clo; | 641 | return clo; |
666 | } | 642 | } |
@@ -688,12 +664,10 @@ set_absolute_time (struct GNUNET_GETOPT_CommandLineProcessorContext *ctx, | |||
688 | struct GNUNET_TIME_Absolute *val = scls; | 664 | struct GNUNET_TIME_Absolute *val = scls; |
689 | 665 | ||
690 | (void) ctx; | 666 | (void) ctx; |
691 | if (GNUNET_OK != | 667 | if (GNUNET_OK != GNUNET_STRINGS_fancy_time_to_absolute (value, val)) |
692 | GNUNET_STRINGS_fancy_time_to_absolute (value, | ||
693 | val)) | ||
694 | { | 668 | { |
695 | FPRINTF (stderr, | 669 | FPRINTF (stderr, |
696 | _("You must pass absolute time to the `%s' option.\n"), | 670 | _ ("You must pass absolute time to the `%s' option.\n"), |
697 | option); | 671 | option); |
698 | return GNUNET_SYSERR; | 672 | return GNUNET_SYSERR; |
699 | } | 673 | } |
@@ -713,20 +687,18 @@ set_absolute_time (struct GNUNET_GETOPT_CommandLineProcessorContext *ctx, | |||
713 | */ | 687 | */ |
714 | struct GNUNET_GETOPT_CommandLineOption | 688 | struct GNUNET_GETOPT_CommandLineOption |
715 | GNUNET_GETOPT_option_absolute_time (char shortName, | 689 | GNUNET_GETOPT_option_absolute_time (char shortName, |
716 | const char *name, | 690 | const char *name, |
717 | const char *argumentHelp, | 691 | const char *argumentHelp, |
718 | const char *description, | 692 | const char *description, |
719 | struct GNUNET_TIME_Absolute *val) | 693 | struct GNUNET_TIME_Absolute *val) |
720 | { | 694 | { |
721 | struct GNUNET_GETOPT_CommandLineOption clo = { | 695 | struct GNUNET_GETOPT_CommandLineOption clo = {.shortName = shortName, |
722 | .shortName = shortName, | 696 | .name = name, |
723 | .name = name, | 697 | .argumentHelp = argumentHelp, |
724 | .argumentHelp = argumentHelp, | 698 | .description = description, |
725 | .description = description, | 699 | .require_argument = 1, |
726 | .require_argument = 1, | 700 | .processor = &set_absolute_time, |
727 | .processor = &set_absolute_time, | 701 | .scls = (void *) val}; |
728 | .scls = (void *) val | ||
729 | }; | ||
730 | 702 | ||
731 | return clo; | 703 | return clo; |
732 | } | 704 | } |
@@ -755,20 +727,18 @@ set_uint (struct GNUNET_GETOPT_CommandLineProcessorContext *ctx, | |||
755 | char dummy[2]; | 727 | char dummy[2]; |
756 | 728 | ||
757 | (void) ctx; | 729 | (void) ctx; |
758 | if('-' == *value) | 730 | if ('-' == *value) |
759 | { | 731 | { |
760 | FPRINTF (stderr, | 732 | FPRINTF (stderr, |
761 | _("Your input for the '%s' option has to be a non negative number \n"), | 733 | _ ( |
762 | option); | 734 | "Your input for the '%s' option has to be a non negative number \n"), |
763 | return GNUNET_SYSERR; | 735 | option); |
736 | return GNUNET_SYSERR; | ||
764 | } | 737 | } |
765 | if (1 != SSCANF (value, | 738 | if (1 != SSCANF (value, "%u%1s", val, dummy)) |
766 | "%u%1s", | ||
767 | val, | ||
768 | dummy)) | ||
769 | { | 739 | { |
770 | FPRINTF (stderr, | 740 | FPRINTF (stderr, |
771 | _("You must pass a number to the `%s' option.\n"), | 741 | _ ("You must pass a number to the `%s' option.\n"), |
772 | option); | 742 | option); |
773 | return GNUNET_SYSERR; | 743 | return GNUNET_SYSERR; |
774 | } | 744 | } |
@@ -787,26 +757,23 @@ set_uint (struct GNUNET_GETOPT_CommandLineProcessorContext *ctx, | |||
787 | */ | 757 | */ |
788 | struct GNUNET_GETOPT_CommandLineOption | 758 | struct GNUNET_GETOPT_CommandLineOption |
789 | GNUNET_GETOPT_option_uint (char shortName, | 759 | GNUNET_GETOPT_option_uint (char shortName, |
790 | const char *name, | 760 | const char *name, |
791 | const char *argumentHelp, | 761 | const char *argumentHelp, |
792 | const char *description, | 762 | const char *description, |
793 | unsigned int *val) | 763 | unsigned int *val) |
794 | { | 764 | { |
795 | struct GNUNET_GETOPT_CommandLineOption clo = { | 765 | struct GNUNET_GETOPT_CommandLineOption clo = {.shortName = shortName, |
796 | .shortName = shortName, | 766 | .name = name, |
797 | .name = name, | 767 | .argumentHelp = argumentHelp, |
798 | .argumentHelp = argumentHelp, | 768 | .description = description, |
799 | .description = description, | 769 | .require_argument = 1, |
800 | .require_argument = 1, | 770 | .processor = &set_uint, |
801 | .processor = &set_uint, | 771 | .scls = (void *) val}; |
802 | .scls = (void *) val | ||
803 | }; | ||
804 | 772 | ||
805 | return clo; | 773 | return clo; |
806 | } | 774 | } |
807 | 775 | ||
808 | 776 | ||
809 | |||
810 | /** | 777 | /** |
811 | * Set an option of type 'uint16_t' from the command line. | 778 | * Set an option of type 'uint16_t' from the command line. |
812 | * A pointer to this function should be passed as part of the | 779 | * A pointer to this function should be passed as part of the |
@@ -822,30 +789,27 @@ GNUNET_GETOPT_option_uint (char shortName, | |||
822 | */ | 789 | */ |
823 | static int | 790 | static int |
824 | set_uint16 (struct GNUNET_GETOPT_CommandLineProcessorContext *ctx, | 791 | set_uint16 (struct GNUNET_GETOPT_CommandLineProcessorContext *ctx, |
825 | void *scls, | 792 | void *scls, |
826 | const char *option, | 793 | const char *option, |
827 | const char *value) | 794 | const char *value) |
828 | { | 795 | { |
829 | uint16_t *val = scls; | 796 | uint16_t *val = scls; |
830 | unsigned int v; | 797 | unsigned int v; |
831 | char dummy[2]; | 798 | char dummy[2]; |
832 | 799 | ||
833 | (void) ctx; | 800 | (void) ctx; |
834 | if (1 != SSCANF (value, | 801 | if (1 != SSCANF (value, "%u%1s", &v, dummy)) |
835 | "%u%1s", | ||
836 | &v, | ||
837 | dummy)) | ||
838 | { | 802 | { |
839 | FPRINTF (stderr, | 803 | FPRINTF (stderr, |
840 | _("You must pass a number to the `%s' option.\n"), | 804 | _ ("You must pass a number to the `%s' option.\n"), |
841 | option); | 805 | option); |
842 | return GNUNET_SYSERR; | 806 | return GNUNET_SYSERR; |
843 | } | 807 | } |
844 | if (v > UINT16_MAX) | 808 | if (v > UINT16_MAX) |
845 | { | 809 | { |
846 | FPRINTF (stderr, | 810 | FPRINTF (stderr, |
847 | _("You must pass a number below %u to the `%s' option.\n"), | 811 | _ ("You must pass a number below %u to the `%s' option.\n"), |
848 | (unsigned int) UINT16_MAX, | 812 | (unsigned int) UINT16_MAX, |
849 | option); | 813 | option); |
850 | return GNUNET_SYSERR; | 814 | return GNUNET_SYSERR; |
851 | } | 815 | } |
@@ -865,20 +829,18 @@ set_uint16 (struct GNUNET_GETOPT_CommandLineProcessorContext *ctx, | |||
865 | */ | 829 | */ |
866 | struct GNUNET_GETOPT_CommandLineOption | 830 | struct GNUNET_GETOPT_CommandLineOption |
867 | GNUNET_GETOPT_option_uint16 (char shortName, | 831 | GNUNET_GETOPT_option_uint16 (char shortName, |
868 | const char *name, | 832 | const char *name, |
869 | const char *argumentHelp, | 833 | const char *argumentHelp, |
870 | const char *description, | 834 | const char *description, |
871 | uint16_t *val) | 835 | uint16_t *val) |
872 | { | 836 | { |
873 | struct GNUNET_GETOPT_CommandLineOption clo = { | 837 | struct GNUNET_GETOPT_CommandLineOption clo = {.shortName = shortName, |
874 | .shortName = shortName, | 838 | .name = name, |
875 | .name = name, | 839 | .argumentHelp = argumentHelp, |
876 | .argumentHelp = argumentHelp, | 840 | .description = description, |
877 | .description = description, | 841 | .require_argument = 1, |
878 | .require_argument = 1, | 842 | .processor = &set_uint16, |
879 | .processor = &set_uint16, | 843 | .scls = (void *) val}; |
880 | .scls = (void *) val | ||
881 | }; | ||
882 | 844 | ||
883 | return clo; | 845 | return clo; |
884 | } | 846 | } |
@@ -923,15 +885,16 @@ set_base32 (struct GNUNET_GETOPT_CommandLineProcessorContext *ctx, | |||
923 | struct Base32Context *bc = scls; | 885 | struct Base32Context *bc = scls; |
924 | 886 | ||
925 | (void) ctx; | 887 | (void) ctx; |
926 | if (GNUNET_OK != | 888 | if (GNUNET_OK != GNUNET_STRINGS_string_to_data (value, |
927 | GNUNET_STRINGS_string_to_data (value, | 889 | strlen (value), |
928 | strlen (value), | 890 | bc->val, |
929 | bc->val, | 891 | bc->val_size)) |
930 | bc->val_size)) | ||
931 | { | 892 | { |
932 | fprintf (stderr, | 893 | fprintf ( |
933 | _("Argument `%s' malformed. Expected base32 (Crockford) encoded value.\n"), | 894 | stderr, |
934 | option); | 895 | _ ( |
896 | "Argument `%s' malformed. Expected base32 (Crockford) encoded value.\n"), | ||
897 | option); | ||
935 | return GNUNET_SYSERR; | 898 | return GNUNET_SYSERR; |
936 | } | 899 | } |
937 | return GNUNET_OK; | 900 | return GNUNET_OK; |
@@ -964,23 +927,21 @@ free_bc (void *cls) | |||
964 | */ | 927 | */ |
965 | struct GNUNET_GETOPT_CommandLineOption | 928 | struct GNUNET_GETOPT_CommandLineOption |
966 | GNUNET_GETOPT_option_base32_fixed_size (char shortName, | 929 | GNUNET_GETOPT_option_base32_fixed_size (char shortName, |
967 | const char *name, | 930 | const char *name, |
968 | const char *argumentHelp, | 931 | const char *argumentHelp, |
969 | const char *description, | 932 | const char *description, |
970 | void *val, | 933 | void *val, |
971 | size_t val_size) | 934 | size_t val_size) |
972 | { | 935 | { |
973 | struct Base32Context *bc = GNUNET_new (struct Base32Context); | 936 | struct Base32Context *bc = GNUNET_new (struct Base32Context); |
974 | struct GNUNET_GETOPT_CommandLineOption clo = { | 937 | struct GNUNET_GETOPT_CommandLineOption clo = {.shortName = shortName, |
975 | .shortName = shortName, | 938 | .name = name, |
976 | .name = name, | 939 | .argumentHelp = argumentHelp, |
977 | .argumentHelp = argumentHelp, | 940 | .description = description, |
978 | .description = description, | 941 | .require_argument = 1, |
979 | .require_argument = 1, | 942 | .processor = &set_base32, |
980 | .processor = &set_base32, | 943 | .cleaner = &free_bc, |
981 | .cleaner = &free_bc, | 944 | .scls = (void *) bc}; |
982 | .scls = (void *) bc | ||
983 | }; | ||
984 | 945 | ||
985 | bc->val = val; | 946 | bc->val = val; |
986 | bc->val_size = val_size; | 947 | bc->val_size = val_size; |
@@ -1002,4 +963,18 @@ GNUNET_GETOPT_option_mandatory (struct GNUNET_GETOPT_CommandLineOption opt) | |||
1002 | } | 963 | } |
1003 | 964 | ||
1004 | 965 | ||
966 | /** | ||
967 | * Make the given option mutually exclusive with other options. | ||
968 | * | ||
969 | * @param opt option to modify | ||
970 | * @return @a opt with the exclusive flag set. | ||
971 | */ | ||
972 | struct GNUNET_GETOPT_CommandLineOption | ||
973 | GNUNET_GETOPT_option_exclusive (struct GNUNET_GETOPT_CommandLineOption opt) | ||
974 | { | ||
975 | opt.option_exclusive = 1; | ||
976 | return opt; | ||
977 | } | ||
978 | |||
979 | |||
1005 | /* end of getopt_helpers.c */ | 980 | /* end of getopt_helpers.c */ |