/
optparse.c
883 lines (731 loc) · 30.6 KB
/
optparse.c
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
#include "src/common/libtap/tap.h"
#include "src/common/liboptparse/optparse.h"
#include "src/common/libutil/sds.h"
static void *myfatal_h = NULL;
int myfatal (void *h, int exit_code)
{
myfatal_h = h;
return (0);
}
sds usage_out = NULL;
void output_f (const char *fmt, ...)
{
va_list ap;
sds s = usage_out ? usage_out : sdsempty();
va_start (ap, fmt);
usage_out = sdscatvprintf (s, fmt, ap);
va_end (ap);
}
void usage_output_is (const char *expected, const char *msg)
{
ok (usage_out != NULL, "optparse_print_usage");
is (usage_out, expected, msg);
sdsfree (usage_out);
usage_out = NULL;
}
void usage_ok (optparse_t *p, const char *expected, const char *msg)
{
optparse_print_usage (p);
usage_output_is (expected, msg);
}
void test_usage_output (void)
{
optparse_err_t e;
optparse_t *p = optparse_create ("prog-foo");
struct optparse_option opt;
ok (p != NULL, "optparse_create");
// Ensure we use default term columns:
unsetenv ("COLUMNS");
opt = ((struct optparse_option) {
.name = "test", .key = 't', .has_arg = 0,
.usage = "Enable a test option."
});
e = optparse_add_option (p, &opt);
ok (e == OPTPARSE_SUCCESS, "optparse_add_option");
opt = ((struct optparse_option) {
.name = "test2", .key = 'T', .has_arg = 1,
.arginfo = "N",
.usage = "Enable a test option N."
});
e = optparse_add_option (p, &opt);
ok (e == OPTPARSE_SUCCESS, "optparse_add_option");
e = optparse_set (p, OPTPARSE_USAGE, "[MOAR OPTIONS]");
ok (e == OPTPARSE_SUCCESS, "optparse_set (USAGE)");
// Reset usage works:
e = optparse_set (p, OPTPARSE_USAGE, "[OPTIONS]");
ok (e == OPTPARSE_SUCCESS, "optparse_set (USAGE)");
e = optparse_set (p, OPTPARSE_LOG_FN, output_f);
ok (e == OPTPARSE_SUCCESS, "optparse_set (LOG_FN)");
usage_ok (p, "\
Usage: prog-foo [OPTIONS]\n\
-T, --test2=N Enable a test option N.\n\
-h, --help Display this message.\n\
-t, --test Enable a test option.\n",
"Usage output as expected");
// Add a hidden (undocumented) option
opt = ((struct optparse_option) {
.name = "hidden", .key = 'H', .has_arg = 1,
.flags = OPTPARSE_OPT_HIDDEN,
.arginfo = "ARGINFO",
.usage = "This option should not be displayed"
});
e = optparse_add_option (p, &opt);
ok (e == OPTPARSE_SUCCESS, "optparse_add_option. group 1.");
usage_ok (p, "\
Usage: prog-foo [OPTIONS]\n\
-T, --test2=N Enable a test option N.\n\
-h, --help Display this message.\n\
-t, --test Enable a test option.\n",
"Usage output as expected");
// Adjust left margin
e = optparse_set (p, OPTPARSE_LEFT_MARGIN, 0);
ok (e == OPTPARSE_SUCCESS, "optparse_set (LEFT_MARGIN)");
usage_ok (p, "\
Usage: prog-foo [OPTIONS]\n\
-T, --test2=N Enable a test option N.\n\
-h, --help Display this message.\n\
-t, --test Enable a test option.\n",
"Usage output as expected w/ left margin");
e = optparse_set (p, OPTPARSE_LEFT_MARGIN, 2);
ok (e == OPTPARSE_SUCCESS, "optparse_set (LEFT_MARGIN)");
// Remove options
e = optparse_remove_option (p, "test");
ok (e == OPTPARSE_SUCCESS, "optparse_remove_option (\"test\")");
usage_ok (p, "\
Usage: prog-foo [OPTIONS]\n\
-T, --test2=N Enable a test option N.\n\
-h, --help Display this message.\n",
"Usage output as expected after option removal");
// Add doc sections
e = optparse_add_doc (p, "This is some doc in header", 0);
ok (e == OPTPARSE_SUCCESS, "optparse_add_doc (group=0)");
usage_ok (p, "\
Usage: prog-foo [OPTIONS]\n\
This is some doc in header\n\
-T, --test2=N Enable a test option N.\n\
-h, --help Display this message.\n",
"Usage output as with doc");
// Add a longer option in group 1:
opt = ((struct optparse_option) {
.name = "long-option", .key = 'A', .has_arg = 1, .group = 1,
.arginfo = "ARGINFO",
.usage = "Enable a long option with argument info ARGINFO."
});
e = optparse_add_option (p, &opt);
ok (e == OPTPARSE_SUCCESS, "optparse_add_option. group 1.");
usage_ok (p, "\
Usage: prog-foo [OPTIONS]\n\
This is some doc in header\n\
-T, --test2=N Enable a test option N.\n\
-h, --help Display this message.\n\
-A, --long-option=ARGINFO\n\
Enable a long option with argument info ARGINFO.\n",
"Usage output with option in group 1");
// Add doc for group 1.
e = optparse_add_doc (p, "This is some doc for group 1", 1);
ok (e == OPTPARSE_SUCCESS, "optparse_add_doc (group = 1)");
usage_ok (p, "\
Usage: prog-foo [OPTIONS]\n\
This is some doc in header\n\
-T, --test2=N Enable a test option N.\n\
-h, --help Display this message.\n\
This is some doc for group 1\n\
-A, --long-option=ARGINFO\n\
Enable a long option with argument info ARGINFO.\n",
"Usage output with option in group 1");
// Increase option width:
e = optparse_set (p, OPTPARSE_OPTION_WIDTH, 30);
ok (e == OPTPARSE_SUCCESS, "optparse_set (OPTION_WIDTH)");
usage_ok (p, "\
Usage: prog-foo [OPTIONS]\n\
This is some doc in header\n\
-T, --test2=N Enable a test option N.\n\
-h, --help Display this message.\n\
This is some doc for group 1\n\
-A, --long-option=ARGINFO Enable a long option with argument info ARGINFO.\n",
"Usage output with increased option width");
// Add an option with very long description in group 1:
opt = ((struct optparse_option) {
.name = "option-B", .key = 'B', .group = 1,
.usage = "This option has a very long description. It should be split across lines nicely."
});
e = optparse_add_option (p, &opt);
ok (e == OPTPARSE_SUCCESS, "optparse_add_option. group 1.");
usage_ok (p, "\
Usage: prog-foo [OPTIONS]\n\
This is some doc in header\n\
-T, --test2=N Enable a test option N.\n\
-h, --help Display this message.\n\
This is some doc for group 1\n\
-A, --long-option=ARGINFO Enable a long option with argument info ARGINFO.\n\
-B, --option-B This option has a very long description. It should\n\
be split across lines nicely.\n",
"Usage output with message autosplit across lines");
// Add an option whose description will break up a word
opt = ((struct optparse_option) {
.name = "option-C", .key = 'C', .group = 1,
.usage = "ThisOptionHasAVeryLongWordInTheDescriptionThatShouldBeBrokenAcrossLines."
});
e = optparse_add_option (p, &opt);
ok (e == OPTPARSE_SUCCESS, "optparse_add_option. group 1.");
usage_ok (p, "\
Usage: prog-foo [OPTIONS]\n\
This is some doc in header\n\
-T, --test2=N Enable a test option N.\n\
-h, --help Display this message.\n\
This is some doc for group 1\n\
-A, --long-option=ARGINFO Enable a long option with argument info ARGINFO.\n\
-B, --option-B This option has a very long description. It should\n\
be split across lines nicely.\n\
-C, --option-C ThisOptionHasAVeryLongWordInTheDescriptionThatSho-\n\
uldBeBrokenAcrossLines.\n",
"Usage output with message autosplit across lines");
ok (setenv ("COLUMNS", "120", 1) >= 0, "Set COLUMNS=120");
usage_ok (p, "\
Usage: prog-foo [OPTIONS]\n\
This is some doc in header\n\
-T, --test2=N Enable a test option N.\n\
-h, --help Display this message.\n\
This is some doc for group 1\n\
-A, --long-option=ARGINFO Enable a long option with argument info ARGINFO.\n\
-B, --option-B This option has a very long description. It should be split across lines nicely.\n\
-C, --option-C ThisOptionHasAVeryLongWordInTheDescriptionThatShouldBeBrokenAcrossLines.\n",
"Usage output with COLUMNS=120 not split across lines");
/* Unset COLUMNS again */
unsetenv ("COLUMNS");
// Add an option with no short option key
opt = ((struct optparse_option) {
.name = "long-only", .group = 1,
.usage = "This option is long only"
});
e = optparse_add_option (p, &opt);
ok (e == OPTPARSE_SUCCESS, "optparse_add_option. long only, group 1.");
usage_ok (p, "\
Usage: prog-foo [OPTIONS]\n\
This is some doc in header\n\
-T, --test2=N Enable a test option N.\n\
-h, --help Display this message.\n\
This is some doc for group 1\n\
--long-only This option is long only\n\
-A, --long-option=ARGINFO Enable a long option with argument info ARGINFO.\n\
-B, --option-B This option has a very long description. It should\n\
be split across lines nicely.\n\
-C, --option-C ThisOptionHasAVeryLongWordInTheDescriptionThatSho-\n\
uldBeBrokenAcrossLines.\n",
"Usage output with long only option");
optparse_destroy (p);
}
void test_convenience_accessors (void)
{
struct optparse_option opts [] = {
{ .name = "foo", .key = 1, .has_arg = 0, .usage = "" },
{ .name = "bar", .key = 2, .has_arg = 1, .arginfo = "", .usage = "" },
{ .name = "baz", .key = 3, .has_arg = 1, .arginfo = "", .usage = "" },
{ .name = "mnf", .key = 4, .has_arg = 1, .arginfo = "", .usage = "" },
{ .name = "oop", .key = 5, .has_arg = 1, .arginfo = "", .usage = "" },
{ .name = "neg", .key = 6, .has_arg = 1, .arginfo = "", .usage = "" },
{ .name = "dub", .key = 7, .has_arg = 1, .arginfo = "", .usage = "" },
{ .name = "ndb", .key = 8, .has_arg = 1, .arginfo = "", .usage = "" },
OPTPARSE_TABLE_END,
};
char *av[] = { "test", "--foo", "--baz=hello", "--mnf=7", "--neg=-4",
"--dub=5.7", "--ndb=-3.2", NULL };
int ac = sizeof (av) / sizeof (av[0]) - 1;
int rc, optindex;
optparse_t *p = optparse_create ("test");
ok (p != NULL, "create object");
rc = optparse_add_option_table (p, opts);
ok (rc == OPTPARSE_SUCCESS, "register options");
ok (optparse_option_index (p) == -1, "optparse_option_index returns -1 before parse");
optindex = optparse_parse_args (p, ac, av);
ok (optindex == ac, "parse options, verify optindex");
ok (optparse_option_index (p) == optindex, "optparse_option_index works after parse");
/* hasopt
*/
dies_ok ({ optparse_hasopt (p, "no-exist"); },
"hasopt exits on unknown arg");
lives_ok ({ optparse_hasopt (p, "foo"); },
"hasopt lives on known arg");
ok (optparse_hasopt (p, "foo"), "hasopt finds present option");
ok (!optparse_hasopt (p, "bar"), "hasopt doesn't find missing option");
ok (optparse_hasopt (p, "baz"), "hasopt finds option with argument");
/* get_int
*/
dies_ok ({optparse_get_int (p, "no-exist", 0); },
"get_int exits on unknown arg");
dies_ok ({optparse_get_int (p, "foo", 0); },
"get_int exits on option with no argument");
dies_ok ({optparse_get_int (p, "baz", 0); },
"get_int exits on option with wrong type argument (string)");
dies_ok ({optparse_get_int (p, "dub", 0); },
"get_int exits on option with wrong type argument (float)");
lives_ok ({optparse_get_int (p, "bar", 0); },
"get_int lives on known arg");
ok (optparse_get_int (p, "bar", 42) == 42,
"get_int returns default argument when arg not present");
ok (optparse_get_int (p, "mnf", 42) == 7,
"get_int returns arg when present");
ok (optparse_get_int (p, "neg", 42) == -4,
"get_int returns negative arg when present");
/* get_double
*/
dies_ok ({optparse_get_double (p, "no-exist", 0); },
"get_double exits on unknown arg");
dies_ok ({optparse_get_double (p, "foo", 0); },
"get_double exits on option with no argument");
dies_ok ({optparse_get_double (p, "baz", 0); },
"get_int exits on option with wrong type argument (string)");
lives_ok ({optparse_get_double (p, "bar", 0); },
"get_double lives on known arg");
ok (optparse_get_double (p, "bar", 42.0) == 42.0,
"get_double returns default argument when arg not present");
ok (optparse_get_double (p, "mnf", 42) == 7.0,
"get_double returns arg when present");
ok (optparse_get_double (p, "neg", 42) == -4.0,
"get_double returns negative arg when present");
ok (optparse_get_double (p, "dub", 42) == 5.7,
"get_double returns arg when present");
ok (optparse_get_double (p, "ndb", 42) == -3.2,
"get_double returns negative arg when present");
/* get_str
*/
dies_ok ({optparse_get_str (p, "no-exist", NULL); },
"get_str exits on unknown arg");
ok (optparse_get_str (p, "foo", "xyz") == NULL,
"get_str returns NULL on option with no argument configured");
lives_ok ({optparse_get_str (p, "bar", NULL); },
"get_str lives on known arg");
ok (optparse_get_str (p, "bar", NULL) == NULL,
"get_str returns default argument when arg not present");
like (optparse_get_str (p, "baz", NULL), "^hello$",
"get_str returns arg when present");
/* fatalerr
*/
dies_ok ({ optparse_hasopt (p, "no-exist"); },
"hasopt exits on unknown arg");
rc = optparse_set (p, OPTPARSE_FATALERR_FN, myfatal);
ok (rc == OPTPARSE_SUCCESS, "optparse_set FATALERR_FN");
rc = optparse_set (p, OPTPARSE_FATALERR_HANDLE, stderr);
ok (rc == OPTPARSE_SUCCESS, "optparse_set FATALERR_HANDLE");
lives_ok ({optparse_get_int (p, "no-exist", 0); },
"get_int now survives unknown arg");
ok (myfatal_h == stderr, "handle successfully passed to fatalerr");
optparse_destroy (p);
}
void test_errors (void)
{
optparse_err_t e;
struct optparse_option opt;
optparse_t *p = optparse_create ("errors-test");
ok (p != NULL, "optparse_create");
opt = ((struct optparse_option) {
.name = "help", .key = 'h',
.usage = "Conflicting option"
});
e = optparse_add_option (p, &opt);
ok (e == OPTPARSE_EEXIST, "optparse_add_option: Errror with EEXIST");
e = optparse_add_option (NULL, &opt);
ok (e == OPTPARSE_BAD_ARG, "optparse_add_option: BAD_ARG with invalid optparse_t");
e = optparse_remove_option (p, "foo");
ok (e == OPTPARSE_FAILURE, "optparse_remove_option: FAILURE if option not found");
// optparse_set error cases:
e = optparse_set (p, 1000, 1);
ok (e == OPTPARSE_BAD_ARG, "optparse_set (invalid item) returns BAD_ARG");
e = optparse_set (p, OPTPARSE_LEFT_MARGIN, 2000);
ok (e == OPTPARSE_BAD_ARG, "optparse_set (LEFT_MARGIN, 2000) returns BAD_ARG");
e = optparse_set (p, OPTPARSE_LEFT_MARGIN, -1);
ok (e == OPTPARSE_BAD_ARG, "optparse_set (LEFT_MARGIN, -1) returns BAD_ARG");
e = optparse_set (p, OPTPARSE_OPTION_WIDTH, 2000);
ok (e == OPTPARSE_BAD_ARG, "optparse_set (OPTION_WIDTH, 2000) returns BAD_ARG");
e = optparse_set (p, OPTPARSE_OPTION_WIDTH, -1);
ok (e == OPTPARSE_BAD_ARG, "optparse_set (OPTION_WIDTH, -1) returns BAD_ARG");
optparse_destroy (p);
}
void test_multiret (void)
{
int rc;
const char *optarg;
optparse_err_t e;
optparse_t *p = optparse_create ("multret-test");
struct optparse_option opts [] = {
{ .name = "required-arg", .key = 'r', .has_arg = 1,
.arginfo = "", .usage = "" },
{ .name = "optional-arg", .key = 'o', .has_arg = 2,
.arginfo = "", .usage = "" },
{ .name = "multi-ret", .key = 'm', .has_arg = 1,
.flags = OPTPARSE_OPT_AUTOSPLIT,
.arginfo = "", .usage = "" },
OPTPARSE_TABLE_END,
};
char *av[] = { "multret-test",
"-r", "one", "-mone", "-m", "two",
"-o", "-rtwo", "--multi-ret=a,b,c",
NULL };
int ac = sizeof (av) / sizeof (av[0]) - 1;
int optindex;
ok (p != NULL, "optparse_create");
e = optparse_add_option_table (p, opts);
ok (e == OPTPARSE_SUCCESS, "register options");
optindex = optparse_parse_args (p, ac, av);
ok (optindex == ac, "parse options, verify optindex");
rc = optparse_getopt (p, "required-arg", &optarg);
ok (rc == 2, "-r used twice");
is (optarg, "two", "last usage wins");
optarg = NULL;
rc = optparse_getopt (p, "optional-arg", &optarg);
ok (rc == 1, "-o used once");
ok (optarg == NULL, "with no arg");
optarg = NULL;
rc = optparse_getopt (p, "multi-ret", &optarg);
ok (rc == 3, "-m used three times");
is (optarg, "c", "last usage wins");
// iterate over arguments
int i = 0;
char *expected[] = { "one", "two", "BAD INDEX" };
const char *s;
while ((s = optparse_getopt_next (p, "required-arg"))) {
is (expected [i], s, "%d: argument matches", i);
i++;
}
ok (optparse_getopt_next (p, "required-arg") == NULL,
"getopt_next returns Null repeatedly after iteration");
rc = optparse_getopt_iterator_reset (p, "required-arg");
ok (rc == 2, "Iterator reset indicates 2 options to iterate");
// multi-ret
char *expected2[] = { "one", "two", "a", "b", "c" };
i = 0;
while ((s = optparse_getopt_next (p, "multi-ret"))) {
is (expected2 [i], s, "%d: argument matches", i);
i++;
}
rc = optparse_getopt_iterator_reset (p, "multi-ret");
ok (rc == 5, "Iterator reset indicates 2 options to iterate");
optparse_destroy (p);
}
void test_long_only (void)
{
int rc;
const char *optarg;
optparse_err_t e;
optparse_t *p = optparse_create ("long-only-test");
struct optparse_option opts [] = {
{ .name = "basic", .key = 'b', .has_arg = 1,
.arginfo = "B", .usage = "This is a basic argument" },
{ .name = "long-only", .has_arg = 1,
.arginfo = "L", .usage = "This is a long-only option" },
{ .name = "again-long-only", .has_arg = 0,
.usage = "Another long-only" },
OPTPARSE_TABLE_END,
};
char *av[] = { "long-only-test",
"-b", "one", "--again-long-only",
NULL };
int ac = sizeof (av) / sizeof (av[0]) - 1;
int optindex;
ok (p != NULL, "optparse_create");
e = optparse_add_option_table (p, opts);
ok (e == OPTPARSE_SUCCESS, "register options");
optindex = optparse_parse_args (p, ac, av);
ok (optindex == ac, "parse options, verify optindex");
rc = optparse_getopt (p, "basic", &optarg);
ok (rc == 1, "got -b");
is (optarg, "one", "got correct argument to --basic option");
optarg = NULL;
// Ensure we got correct long-only option
ok (optparse_hasopt (p, "again-long-only"), "Got --again-long-only");
ok (!optparse_hasopt (p, "long-only"), "And didn't get --long-only");
char *av2[] = { "long-only-test", "--again-long-only", "-bxxx",
"--long-only=foo", NULL };
ac = sizeof (av2) / sizeof(av2[0]) - 1;
optindex = optparse_parse_args (p, ac, av2);
ok (optindex == ac, "parse options, verify optindex");
optarg = NULL;
rc = optparse_getopt (p, "basic", &optarg);
ok (rc == 2, "got -b", rc); // second use of --basic
is (optarg, "xxx", "got correct argument to --basic option");
// Ensure we got correct long-only option
ok (optparse_hasopt (p, "again-long-only"), "Got --again-long-only");
rc = optparse_getopt (p, "long-only", &optarg);
ok (rc == 1, "got --long-only");
is (optarg, "foo", "got correct argument to --long-only option");
optparse_destroy (p);
}
void test_optional_argument (void)
{
int rc;
const char *optarg;
optparse_err_t e;
optparse_t *p = optparse_create ("optarg");
struct optparse_option opts [] = {
{ .name = "basic", .key = 'b', .has_arg = 1,
.arginfo = "B", .usage = "This is a basic argument" },
{ .name = "optional-arg", .has_arg = 2, .key = 'o',
.arginfo = "OPTIONAL", .usage = "This has an optional argument" },
OPTPARSE_TABLE_END,
};
char *av[] = { "optarg",
"--optional-arg", "extra-args",
NULL };
int ac = sizeof (av) / sizeof (av[0]) - 1;
int optindex;
e = optparse_add_option_table (p, opts);
ok (e == OPTPARSE_SUCCESS, "register options");
optindex = optparse_parse_args (p, ac, av);
ok (optindex == (ac - 1), "parse options, verify optindex");
ok (optparse_hasopt (p, "optional-arg"),
"found optional-arg option with no args");
optarg = NULL;
rc = optparse_getopt (p, "optional-arg", &optarg);
ok (rc == 1, "saw --optional-arg once", rc);
is (optarg, NULL, "no argument to --optional-arg");
char *av2[] = { "optarg",
"--optional-arg=foo", "extra-args",
NULL };
ac = sizeof (av2) / sizeof (av2[0]) - 1;
optindex = optparse_parse_args (p, ac, av2);
ok (optindex == (ac - 1), "parse options, verify optindex");
ok (optparse_hasopt (p, "optional-arg"),
"found optional-arg option with args");
rc = optparse_getopt (p, "optional-arg", &optarg);
ok (rc == 2, "saw --optional-arg again", rc);
is (optarg, "foo", "got argument to --optional-arg");
}
int subcmd (optparse_t *p, int ac, char **av)
{
return (0);
}
void test_data (void)
{
const char haha[] = "haha";
const char hehe[] = "hehe";
const char *s;
optparse_t *c;
optparse_t *p = optparse_create ("data-test");
ok (p != NULL, "optparse_create");
optparse_set_data (p, "foo", (void *)haha);
s = optparse_get_data (p, "foo");
ok (s == haha, "got back correct data");
is (s, haha, "got back correct string");
c = optparse_add_subcommand (p, "test", subcmd);
ok (c != NULL, "optparse_add_subcommand");
s = optparse_get_data (c, "foo");
ok (s == haha, "optparse_get_data recursive lookup in parent works");
is (s, haha, "got back correct string");
optparse_set_data (c, "foo", (void *)hehe);
s = optparse_get_data (c, "foo");
ok (s == hehe, "child data overrides parent");
is (s, hehe, "got back correct string");
optparse_destroy (p);
}
int subcmd_one (optparse_t *p, int ac, char **av)
{
int *ip = NULL;
ok (p != NULL, "subcmd_one: got valid optparse structure");
ip = optparse_get_data (p, "called");
ok (ip != NULL, "subcmd_one: got data pointer");
*ip = 1;
return (0);
}
int subcmd_two (optparse_t *p, int ac, char **av)
{
int *ip = NULL;
ok (p != NULL, "subcmd_two: got valid optparse structure");
ip = optparse_get_data (p, "called");
ok (ip != NULL, "subcmd_two: got data pointer");
*ip = optparse_get_int (p, "test-opt", 2);
return (0);
}
int subcmd_three (optparse_t *p, int ac, char **av)
{
int *acptr = NULL;
ok (p != NULL, "subcmd_three: got valid optparse structure");
acptr = optparse_get_data (p, "argc");
ok (acptr != NULL, "subcmd_three: got argc ptr");
*acptr = ac;
is (av[0], "three", "subcmd_three: av[0] == %s (expected 'three')", av[0]);
return (0);
}
int subcmd_hidden (optparse_t *p, int ac, char **av)
{
ok (p != NULL, "subcmd_hidden: valid optparse structure");
return (0);
}
int do_nothing (void *h, int code)
{
return -code;
}
void test_subcommand (void)
{
optparse_err_t e;
int called = 0;
int n;
optparse_t *b;
optparse_t *a = optparse_create ("test");
ok (a != NULL, "optparse_create");
b = optparse_add_subcommand (a, "one", subcmd_one);
ok (b != NULL, "optparse_add_subcommand (subcmd_one)");
optparse_set_data (b, "called", &called);
ok (optparse_get_data (b, "called") == &called, "optparse_set_data ()");
// Ensure optparse_get_parent/subcommand work:
ok (optparse_get_parent (b) == a, "optparse_get_parent works");
ok (optparse_get_subcommand (a, "one") == b, "optparse_get_subcommand");
b = optparse_add_subcommand (a, "two", subcmd_two);
ok (b != NULL, "optparse_add_subcommand (subcmd_two)");
optparse_set_data (b, "called", &called);
ok (optparse_get_data (b, "called") == &called, "optparse_set_data ()");
e = optparse_set (a, OPTPARSE_LOG_FN, output_f);
ok (e == OPTPARSE_SUCCESS, "optparse_set (LOG_FN)");
usage_ok (a, "\
Usage: test one [OPTIONS]\n\
or: test two [OPTIONS]\n\
-h, --help Display this message.\n",
"Usage output as expected with subcommands");
// Set OPTPARSE_PRINT_SUBCMDS false:
e = optparse_set (a, OPTPARSE_PRINT_SUBCMDS, 0);
ok (e == OPTPARSE_SUCCESS, "optparse_set (PRINT_SUBCMDS, 0)");
usage_ok (a, "\
Usage: test [OPTIONS]...\n\
-h, --help Display this message.\n",
"Usage output as expected with no print subcmds");
e = optparse_set (b, OPTPARSE_LOG_FN, output_f);
ok (e == OPTPARSE_SUCCESS, "optparse_set (subcmd, LOG_FN)");
// Add option to subcommand
e = optparse_add_option (b, &(struct optparse_option) {
.name = "test-opt", .key = 't', .has_arg = 1,
.arginfo = "N",
.usage = "Test option with numeric argument N",
});
ok (e == OPTPARSE_SUCCESS, "optparse_add_option");
usage_ok (b, "\
Usage: test two [OPTIONS]...\n\
-h, --help Display this message.\n\
-t, --test-opt=N Test option with numeric argument N\n",
"Usage output as expected with subcommands");
char *av[] = { "test", "one", NULL };
int ac = sizeof (av) / sizeof (av[0]) - 1;
n = optparse_parse_args (a, ac, av);
ok (n == 1, "optparse_parse_args");
n = optparse_run_subcommand (a, ac, av);
ok (n >= 0, "optparse_run_subcommand");
ok (called == 1, "optparse_run_subcommand: called subcmd_one()");
char *av2[] = { "test", "two", NULL };
ac = sizeof (av2) / sizeof (av2[0]) - 1;
n = optparse_parse_args (a, ac, av2);
ok (n == 1, "optparse_parse_args");
n = optparse_run_subcommand (a, ac, av2);
ok (n >= 0, "optparse_run_subcommand");
ok (called == 2, "optparse_run_subcommand: called subcmd_two()");
char *av3[] = { "test", "two", "--test-opt", "3", NULL };
ac = sizeof (av3) / sizeof (av3[0]) - 1;
// Run subcommand before parse also runs subcommand:
//
e = optparse_run_subcommand (a, ac, av3);
ok (e == OPTPARSE_SUCCESS, "optparse_run_subcommand before parse succeeds");
ok (called = 3, "optparse_run_subcmomand: called subcmd_two with correct args");
// Test unknown option prints expected error:
char *av4[] = { "test", "two", "--unknown", NULL };
ac = sizeof (av4) / sizeof (av4[0]) - 1;
e = optparse_set (b, OPTPARSE_FATALERR_FN, do_nothing);
n = optparse_run_subcommand (a, ac, av4);
ok (n == -1, "optparse_run_subcommand with bad args returns error");
usage_output_is ("\
test two: unrecognized option '--unknown'\n\
Try `test two --help' for more information.\n",
"bad argument error message is expected");
// Test no subcommand (and subcommand required) prints error
char *av5[] = { "test", NULL };
ac = sizeof (av5) / sizeof (av5[0]) - 1;
// Set OPTPARSE_PRINT_SUBCMDS true:
e = optparse_set (a, OPTPARSE_PRINT_SUBCMDS, 1);
ok (e == OPTPARSE_SUCCESS, "optparse_set (PRINT_SUBCMDS, 0)");
// Don't exit on fatal error:
e = optparse_set (a, OPTPARSE_FATALERR_FN, do_nothing);
ok (e == OPTPARSE_SUCCESS, "optparse_set (FATALERR_FN, do_nothing)");
n = optparse_run_subcommand (a, ac, av5);
ok (n == -1, "optparse_run_subcommand with no subcommand");
usage_output_is ("\
test: missing subcommand\n\
Usage: test one [OPTIONS]\n\
or: test two [OPTIONS]\n\
-h, --help Display this message.\n",
"missing subcommand error message is expected");
// Add a hidden subcommand
e = optparse_reg_subcommand (a, "hidden",
subcmd_hidden,
NULL,
"This is a hidden subcmd",
OPTPARSE_SUBCMD_HIDDEN,
NULL);
ok (e == OPTPARSE_SUCCESS, "optparse_reg_subcommand()");
usage_ok (a, "\
Usage: test one [OPTIONS]\n\
or: test two [OPTIONS]\n\
-h, --help Display this message.\n",
"Hidden subcommand doesn't appear in usage output");
// Unhide subcommand
e = optparse_set (optparse_get_subcommand (a, "hidden"),
OPTPARSE_SUBCMD_HIDE, 0);
ok (e == OPTPARSE_SUCCESS, "optparse_set (OPTPARSE_SUBCMD_HIDE, 0)");
usage_ok (a, "\
Usage: test hidden [OPTIONS]\n\
or: test one [OPTIONS]\n\
or: test two [OPTIONS]\n\
-h, --help Display this message.\n",
"Unhidden subcommand now displayed in usage output");
// Hide again with optparse_set
e = optparse_set (optparse_get_subcommand (a, "hidden"),
OPTPARSE_SUBCMD_HIDE, 1);
ok (e == OPTPARSE_SUCCESS, "optparse_set (OPTPARSE_SUBCMD_HIDE, 1)");
usage_ok (a, "\
Usage: test one [OPTIONS]\n\
or: test two [OPTIONS]\n\
-h, --help Display this message.\n",
"Unhidden subcommand now displayed in usage output");
// Test Subcommand without option processing:
optparse_t *d = optparse_add_subcommand (a, "three", subcmd_three);
ok (d != NULL, "optparse_create()");
e = optparse_set (d, OPTPARSE_SUBCMD_NOOPTS, 1);
ok (e == OPTPARSE_SUCCESS, "optparse_set (OPTPARSE_SUBCMD_NOOPTS)");
int value = 0;
optparse_set_data (d, "argc", &value);
char *av6[] = { "test", "three", "--help", NULL };
ac = sizeof (av6) / sizeof (av6[0]) - 1;
n = optparse_run_subcommand (a, ac, av6);
ok (n == 0, "optparse_run_subcommand with OPTPARSE_SUBCMD_NOOPTS");
ok (value == 2, "optparse_run_subcommand() run with argc = %d (expected 2)", value);
ok (optparse_option_index (d) == -1, "optparse_run_subcommand: skipped parse_args");
optparse_destroy (a);
}
void test_corner_case (void)
{
optparse_err_t e;
optparse_t *p = optparse_create ("optarg");
char *av[] = { "cornercase", NULL };
int ac = sizeof (av) / sizeof (av[0]) - 1;
int optindex;
ok (p != NULL, "optparse_create");
/* Test empty options do not segfault */
e = optparse_remove_option (p, "help");
ok (e == OPTPARSE_SUCCESS, "optparse_remove_option");
optindex = optparse_parse_args (p, ac, av);
ok (optindex == ac, "parse options, verify optindex");
optparse_destroy (p);
}
int main (int argc, char *argv[])
{
plan (196);
test_convenience_accessors (); /* 35 tests */
test_usage_output (); /* 42 tests */
test_errors (); /* 9 tests */
test_multiret (); /* 19 tests */
test_data (); /* 8 tests */
test_subcommand (); /* 56 tests */
test_long_only (); /* 13 tests */
test_optional_argument (); /* 9 tests */
test_corner_case (); /* 3 tests */
done_testing ();
return (0);
}
/*
* vi: ts=4 sw=4 expandtab
*/