CommandOptionParser_T.cpp
Go to the documentation of this file.
1 //==============================================================================
2 //
3 // This file is part of GNSSTk, the ARL:UT GNSS Toolkit.
4 //
5 // The GNSSTk is free software; you can redistribute it and/or modify
6 // it under the terms of the GNU Lesser General Public License as published
7 // by the Free Software Foundation; either version 3.0 of the License, or
8 // any later version.
9 //
10 // The GNSSTk is distributed in the hope that it will be useful,
11 // but WITHOUT ANY WARRANTY; without even the implied warranty of
12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 // GNU Lesser General Public License for more details.
14 //
15 // You should have received a copy of the GNU Lesser General Public
16 // License along with GNSSTk; if not, write to the Free Software Foundation,
17 // Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110, USA
18 //
19 // This software was developed by Applied Research Laboratories at the
20 // University of Texas at Austin.
21 // Copyright 2004-2022, The Board of Regents of The University of Texas System
22 //
23 //==============================================================================
24 
25 //==============================================================================
26 //
27 // This software was developed by Applied Research Laboratories at the
28 // University of Texas at Austin, under contract to an agency or agencies
29 // within the U.S. Department of Defense. The U.S. Government retains all
30 // rights to use, duplicate, distribute, disclose, or release this software.
31 //
32 // Pursuant to DoD Directive 523024
33 //
34 // DISTRIBUTION STATEMENT A: This software has been approved for public
35 // release, distribution is unlimited.
36 //
37 //==============================================================================
38 
39 #include "CommandOptionParser.hpp"
41 #include "YDSTime.hpp"
42 #include "TestUtil.hpp"
43 #include <iostream>
44 
45 using namespace std;
46 using namespace gnsstk;
47 
48 // Macro to assert that there are no errors and add diagnostics to
49 // test output if there are
50 #define COPANOERR(COP) \
51  { \
52  std::ostringstream oss; \
53  oss << "CommandOptionParser has errors:" << endl; \
54  COP.dumpErrors(oss); \
55  testFramework.assert(!COP.hasErrors(), oss.str(), __LINE__); \
56  }
57 
59 {
60 public:
63 
64  int testInitialization();
65  int testAddOption();
66  int testParseOptions();
67  int testOptionPresence();
68  int testNOfWhich();
69  //int testDisplayUsage();
70 
71 };
72 
73 
74 /*************************************************************************
75  */
77 {
78  TUDEF("CommandOptionParser", "Initialization");
79 
80  try
81  {
82  CommandOptionParser cop("");
83  TUASSERT(!cop.hasErrors());
84  TUPASS("CommandOptionParser was created successfully.");
85  }
86  catch ( ... )
87  {
88  TUFAIL("CommandOptionParser() threw an exception but should not have.");
89  }
90 
91  try
92  {
93  CommandOptionParser cop("Program description");
94  TUASSERT(!cop.hasErrors());
95  TUPASS("CommandOptionParser was created successfully.");
96  }
97  catch ( ... )
98  {
99  TUFAIL("CommandOptionParser() threw an exception but should not have.");
100  }
101 
102  try
103  {
104  CommandOptionVec testCmdOptVec;
105  CommandOptionParser cop("Program description", testCmdOptVec);
106  TUASSERT(!cop.hasErrors());
107  TUPASS("CommandOptionParser was created successfully.");
108  }
109  catch ( ... )
110  {
111  TUFAIL("CommandOptionParser() threw an exception but should not have.");
112  }
113 
114  try
115  {
116  CommandOptionVec testCmdOptVec;
117  CommandOption cmdOpt1(CommandOption::noArgument,
118  CommandOption::stdType, 'f', "foo", "Foo", false,
119  testCmdOptVec);
120  CommandOption cmdOpt2(CommandOption::noArgument,
121  CommandOption::stdType, 'b', "bar", "Boo", false,
122  testCmdOptVec);
123  CommandOptionParser cop("Program description", testCmdOptVec);
124  TUASSERT(!cop.hasErrors());
125  TUPASS("CommandOptions were added successfully.");
126  }
127  catch ( ... )
128  {
129  TUFAIL("CommandOptionParser() threw an exception but should not have.");
130  }
131 
132  try // Disallow multiple CommandOption's with identical short options
133  {
134  CommandOptionVec testCmdOptVec;
135  CommandOption cmdOpt1(CommandOption::noArgument, CommandOption::stdType,
136  'f', "foo", "Foo", false, testCmdOptVec);
137  CommandOption cmdOpt2(CommandOption::noArgument, CommandOption::stdType,
138  'f', "far", "Far", false, testCmdOptVec);
139  CommandOptionParser cop("Program description", testCmdOptVec);
140  TUFAIL("CommandOptionParser should have disallowed conflicting short"
141  " options.");
142  }
143  catch ( ... )
144  {
145  TUPASS("CommandOptionParser correctly threw an exception to disallow"
146  " conflicting short options.");
147  }
148 
149  try // Disallow multiple CommandOption's with identical long options
150  {
151  CommandOptionVec testCmdOptVec;
152  CommandOption cmdOpt1(CommandOption::noArgument, CommandOption::stdType,
153  'f', "foo", "Foo1", false, testCmdOptVec);
154  CommandOption cmdOpt2(CommandOption::noArgument, CommandOption::stdType,
155  'F', "foo", "Foo2", false, testCmdOptVec);
156  CommandOptionParser cop("Program description", testCmdOptVec);
157  TUFAIL("CommandOptionParser should have disallowed conflicting long"
158  " options.");
159  }
160  catch ( ... )
161  {
162  TUPASS("CommandOptionParser correctly threw an exception to disallow"
163  " conflicting long options.");
164  }
165 
166  try // Disallow multiple CommandOptionRest instances
167  {
168  CommandOptionVec testCmdOptVec;
169  CommandOption cmdOpt1(CommandOption::noArgument,
170  CommandOption::trailingType, 0, "", "Foo1", false,
171  testCmdOptVec);
172  CommandOption cmdOpt2(CommandOption::noArgument,
173  CommandOption::trailingType, 0, "", "Foo2", false,
174  testCmdOptVec);
175  CommandOptionParser cop("Program description", testCmdOptVec);
176  TUFAIL("CommandOptionParser should have disallowed multiple"
177  " CommandOptionRest instances.");
178  }
179  catch ( ... )
180  {
181  TUPASS("CommandOptionParser correctly threw an exception to multiple"
182  " CommandOptionRest instances.");
183  }
184 
185  TURETURN();
186 }
187 
188 
189 /*************************************************************************
190  */
192 {
193  TUDEF("CommandOptionParser", "AddOption");
194 
195  try
196  {
197  CommandOptionVec testCmdOptVec;
198  CommandOption cmdOpt1(CommandOption::noArgument, CommandOption::stdType,
199  'f', "foo", "Foo", false, testCmdOptVec);
200  CommandOption cmdOpt2(CommandOption::noArgument, CommandOption::stdType,
201  'b', "bar", "Boo", false, testCmdOptVec);
202  CommandOptionParser cop("Program description");
203  cop.addOption(cmdOpt1);
204  cop.addOption(cmdOpt2);
205  TUPASS("CommandOptions were added successfully.");
206  }
207  catch ( ... )
208  {
209  TUFAIL("CommandOptionParser() threw an exception but should not have.");
210  }
211 
212  try // Disallow multiple CommandOption's with identical short options
213  {
214  CommandOptionVec testCmdOptVec;
215  CommandOption cmdOpt1(CommandOption::noArgument, CommandOption::stdType,
216  'f', "foo", "Foo", false, testCmdOptVec);
217  CommandOption cmdOpt2(CommandOption::noArgument, CommandOption::stdType,
218  'f', "far", "Far", false, testCmdOptVec);
219  CommandOptionParser cop("Program description");
220  cop.addOption(cmdOpt1);
221  cop.addOption(cmdOpt2);
222  TUFAIL("CommandOptionParser should have disallowed conflicting short"
223  " options.");
224  }
225  catch ( ... )
226  {
227  TUPASS("CommandOptionParser correctly threw an exception to disallow"
228  " conflicting short options.");
229  }
230 
231  try // Disallow multiple CommandOption's with identical long options
232  {
233  CommandOptionVec testCmdOptVec;
234  CommandOption cmdOpt1(CommandOption::noArgument, CommandOption::stdType,
235  'f', "foo", "Foo1", false, testCmdOptVec);
236  CommandOption cmdOpt2(CommandOption::noArgument, CommandOption::stdType,
237  'F', "foo", "Foo2", false, testCmdOptVec);
238  CommandOptionParser cop("Program description");
239  cop.addOption(cmdOpt1);
240  cop.addOption(cmdOpt2);
241  TUFAIL("CommandOptionParser should have disallowed conflicting long"
242  " options.");
243  }
244  catch ( ... )
245  {
246  TUPASS("CommandOptionParser correctly threw an exception to disallow"
247  " conflicting long options.");
248  }
249 
250  try // Disallow multiple CommandOptionRest instances
251  {
252  CommandOptionVec testCmdOptVec;
253  CommandOption cmdOpt1(CommandOption::noArgument,
254  CommandOption::trailingType, 0, "", "Foo1", false,
255  testCmdOptVec);
256  CommandOption cmdOpt2(CommandOption::noArgument,
257  CommandOption::trailingType, 0, "", "Foo2", false,
258  testCmdOptVec);
259  CommandOptionParser cop("Program description");
260  cop.addOption(cmdOpt1);
261  cop.addOption(cmdOpt2);
262  TUFAIL("CommandOptionParser should have disallowed multiple"
263  " CommandOptionRest instances.");
264  }
265  catch ( ... )
266  {
267  TUPASS("CommandOptionParser correctly threw an exception to multiple"
268  " CommandOptionRest instances.");
269  }
270 
271  TURETURN();
272 }
273 
274 
275 /*************************************************************************
276  */
278 {
279  TUDEF("CommandOptionParser", "ParseOptions");
280 
281  try // Parse with no CommandOptions
282  {
283  CommandOptionParser cop("Description");
284  int argc = 1;
285  char* argv[] = { const_cast<char*>("program") };
286  cop.parseOptions(argc, argv);
287  TUPASS("CommandOptionParser parsed the options without throwing an"
288  " exception.");
289  if (cop.hasErrors() )
290  {
291  std::ostringstream oss;
292  oss << "CommandOptionParser encountered unexpected errors while"
293  << " parsing: ";
294  cop.dumpErrors(oss);
295  TUFAIL(oss.str());
296  }
297  else
298  {
299  TUPASS("CommandOptionParser parsed without errors.");
300  }
301  }
302  catch ( ... )
303  {
304  TUFAIL("CommandOptionParser() threw an exception while parsing but"
305  " should not have.");
306  }
307 
308  try // Parse with a single CommandOption with no value
309  {
310  CommandOptionVec testCmdOptVec;
311  CommandOption cmdOpt(CommandOption::noArgument, CommandOption::stdType,
312  'f', "foo", "Foo", false, testCmdOptVec);
313  CommandOptionParser cop("Description", testCmdOptVec);
314  int argc = 2;
315  char* argv[] = { const_cast<char*>("program"), const_cast<char*>("-f")};
316  cop.parseOptions(argc, argv);
317  TUPASS("CommandOptionParser parsed the options without throwing an"
318  " exception.");
319  if (cop.hasErrors())
320  {
321  std::ostringstream oss;
322  oss << "CommandOptionParser encountered unexpected errors while"
323  << " parsing: ";
324  cop.dumpErrors(oss);
325  TUFAIL(oss.str());
326  }
327  else
328  {
329  TUPASS("CommandOptionParser parsed without errors.");
330  std::ostringstream countOss;
331  countOss << cmdOpt.getCount();
332  TUASSERTE(unsigned long,1,cmdOpt.getCount());
333  std::ostringstream orderOss;
334  orderOss << cmdOpt.getOrder();
335  TUASSERTE(unsigned long,1,cmdOpt.getOrder());
336  }
337  }
338  catch ( ... )
339  {
340  TUFAIL("CommandOptionParser() threw an unexpected exception.");
341  }
342 
343  try // Parse with a single CommandOption with a value
344  {
345  CommandOptionVec testCmdOptVec;
346  CommandOption cmdOpt(CommandOption::hasArgument, CommandOption::stdType,
347  'f', "foo", "Foo", false, testCmdOptVec);
348  CommandOptionParser cop("Description", testCmdOptVec);
349  int argc = 3;
350  char* argv[] = { const_cast<char*>("program"), const_cast<char*>("-f"),
351  const_cast<char*>("value") };
352  cop.parseOptions(argc, argv);
353  TUPASS("CommandOptionParser parsed the options without throwing an"
354  " exception.");
355  if (cop.hasErrors() )
356  {
357  std::ostringstream oss;
358  oss << "CommandOptionParser encountered unexpected errors while"
359  << " parsing: ";
360  cop.dumpErrors(oss);
361  TUFAIL(oss.str());
362  }
363  else
364  {
365  TUPASS("CommandOptionParser parsed without errors.");
366  std::ostringstream countOss;
367  countOss << cmdOpt.getCount();
368  TUASSERTE(unsigned long,1,cmdOpt.getCount());
369  std::ostringstream orderOss;
370  orderOss << cmdOpt.getOrder();
371  TUASSERTE(unsigned long,1,cmdOpt.getOrder());
372  std::vector<std::string> values = cmdOpt.getValue();
373  TUASSERTE(unsigned long,1,values.size());
374  if (values.size() == 1)
375  {
376  TUASSERTE(unsigned long,0,values[0].compare("value"));
377  }
378  }
379  }
380  catch ( ... )
381  {
382  TUFAIL("CommandOptionParser() threw an unexpected exception.");
383  }
384 
385  try // Parse with an unexpected standard CommandOption
386  {
387  CommandOptionVec testCmdOptVec;
388  CommandOption cmdOpt(CommandOption::noArgument, CommandOption::stdType,
389  'f', "foo", "Foo", false, testCmdOptVec);
390  CommandOptionParser cop("Description", testCmdOptVec);
391  int argc = 2;
392  char* argv[] = { const_cast<char*>("program"), const_cast<char*>("-g")};
393  cop.parseOptions(argc, argv);
394  TUPASS("CommandOptionParser parsed the options without throwing an"
395  " exception.");
396  TUASSERT(cop.hasErrors());
397  }
398  catch ( ... )
399  {
400  TUFAIL("CommandOptionParser() threw an exception while parsing but"
401  " should not have.");
402  }
403 
404  try // Parse with an unexpected trailing CommandOption
405  {
406  CommandOptionVec testCmdOptVec;
407  CommandOption cmdOpt(CommandOption::noArgument, CommandOption::stdType,
408  'f', "foo", "Foo", false, testCmdOptVec);
409  CommandOptionParser cop("Description", testCmdOptVec);
410  int argc = 3;
411  char* argv[] = { const_cast<char*>("program"), const_cast<char*>("-f"),
412  const_cast<char*>("trailing") };
413  cop.parseOptions(argc, argv);
414  TUPASS("CommandOptionParser parsed the options without throwing an"
415  " exception.");
416  TUASSERT(cop.hasErrors());
417  }
418  catch ( ... )
419  {
420  TUFAIL("CommandOptionParser() threw an exception while parsing but"
421  " should not have.");
422  }
423 
424  try // Parse with a missing required CommandOption
425  {
426  CommandOptionVec testCmdOptVec;
427  CommandOption cmdOpt(CommandOption::noArgument, CommandOption::stdType,
428  'f', "foo", "Foo", true, testCmdOptVec);
429  CommandOptionParser cop("Description", testCmdOptVec);
430  int argc = 2;
431  char* argv[] = { const_cast<char*>("program"),
432  const_cast<char*>("trailing") };
433  cop.parseOptions(argc, argv);
434  TUPASS("CommandOptionParser parsed the options without throwing an"
435  " exception.");
436  TUASSERT(cop.hasErrors());
437  }
438  catch ( ... )
439  {
440  TUFAIL("CommandOptionParser() threw an exception while parsing but"
441  " should not have.");
442  }
443 
444  try // Parse with a violated CommandOption max count
445  {
446  CommandOptionVec testCmdOptVec;
447  CommandOption cmdOpt(CommandOption::noArgument, CommandOption::stdType,
448  'f', "foo", "Foo", false, testCmdOptVec);
449  cmdOpt.setMaxCount(1);
450  CommandOptionParser cop("Description", testCmdOptVec);
451  int argc = 3;
452  char* argv[] = { const_cast<char*>("program"), const_cast<char*>("-f"),
453  const_cast<char*>("-f") };
454  cop.parseOptions(argc, argv);
455  TUPASS("CommandOptionParser parsed the options without throwing an"
456  " exception.");
457  TUASSERT(cop.hasErrors());
458  }
459  catch ( ... )
460  {
461  TUFAIL("CommandOptionParser() threw an exception while parsing but"
462  " should not have.");
463  }
464 
465  try // Parse with multiple CommandOptions
466  {
467  CommandOptionVec testCmdOptVec;
468  CommandOption cmdOpt1(CommandOption::noArgument, CommandOption::stdType,
469  'f', "foo", "Foo", false, testCmdOptVec);
470  CommandOption cmdOpt2(CommandOption::noArgument, CommandOption::stdType,
471  'g', "goo", "Goo", false, testCmdOptVec);
472  CommandOptionParser cop("Description", testCmdOptVec);
473  int argc = 3;
474  char* argv[] = { const_cast<char*>("program"), const_cast<char*>("-g"),
475  const_cast<char*>("-f") };
476  cop.parseOptions(argc, argv);
477  TUPASS("CommandOptionParser parsed the options without throwing an"
478  " exception.");
479  if (cop.hasErrors() )
480  {
481  std::ostringstream oss;
482  oss << "CommandOptionParser encountered unexpected errors while"
483  << " parsing: ";
484  cop.dumpErrors(oss);
485  TUFAIL(oss.str());
486  }
487  else
488  {
489  TUPASS("CommandOptionParser parsed without errors.");
490  std::ostringstream count1Oss;
491  count1Oss << cmdOpt1.getCount();
492  TUASSERTE(unsigned long,1,cmdOpt1.getCount());
493  std::ostringstream count2Oss;
494  count2Oss << cmdOpt1.getCount();
495  TUASSERTE(unsigned long,1,cmdOpt2.getCount());
496  std::ostringstream order1Oss;
497  order1Oss << cmdOpt2.getOrder();
498  TUASSERTE(unsigned long,2,cmdOpt1.getOrder());
499  std::ostringstream order2Oss;
500  order2Oss << cmdOpt2.getOrder();
501  TUASSERTE(unsigned long,1,cmdOpt2.getOrder());
502  }
503  }
504  catch ( ... )
505  {
506  TUFAIL("CommandOptionParser() threw an exception while parsing but"
507  " should not have.");
508  }
509 
510  try // Parse a CommandOption with no short option
511  {
512  CommandOptionVec testCmdOptVec;
513  CommandOption cmdOpt(CommandOption::noArgument, CommandOption::stdType,
514  0, "foo", "Foo", false, testCmdOptVec);
515  CommandOptionParser cop("Description", testCmdOptVec);
516  int argc = 2;
517  char* argv[] = { const_cast<char*>("program"),
518  const_cast<char*>("--foo") };
519  cop.parseOptions(argc, argv);
520  TUPASS("CommandOptionParser parsed the options without throwing an"
521  " exception.");
522  if (cop.hasErrors() )
523  {
524  std::ostringstream oss;
525  oss << "CommandOptionParser encountered unexpected errors while"
526  << " parsing: ";
527  cop.dumpErrors(oss);
528  TUFAIL(oss.str());
529  }
530  else
531  {
532  TUPASS("CommandOptionParser parsed without errors.");
533  std::ostringstream countOss;
534  countOss << cmdOpt.getCount();
535  TUASSERTE(unsigned long,1,cmdOpt.getCount());
536  std::ostringstream orderOss;
537  orderOss << cmdOpt.getOrder();
538  TUASSERTE(unsigned long,1,cmdOpt.getOrder());
539  }
540  }
541  catch ( ... )
542  {
543  TUFAIL("CommandOptionParser() threw an exception while parsing but"
544  " should not have.");
545  }
546 
547  try // Parse a CommandOption with no long option
548  {
549  CommandOptionVec testCmdOptVec;
550  CommandOption cmdOpt(CommandOption::noArgument, CommandOption::stdType,
551  'f', "", "Foo", false, testCmdOptVec);
552  CommandOptionParser cop("Description", testCmdOptVec);
553  int argc = 2;
554  char* argv[] = { const_cast<char*>("program"), const_cast<char*>("-f")};
555  cop.parseOptions(argc, argv);
556  TUPASS("CommandOptionParser parsed the options without throwing an"
557  " exception.");
558  if (cop.hasErrors() )
559  {
560  std::ostringstream oss;
561  oss << "CommandOptionParser encountered unexpected errors while"
562  << " parsing: ";
563  cop.dumpErrors(oss);
564  TUFAIL(oss.str());
565  }
566  else
567  {
568  TUPASS("CommandOptionParser parsed without errors.");
569  std::ostringstream countOss;
570  countOss << cmdOpt.getCount();
571  TUASSERTE(unsigned long,1,cmdOpt.getCount());
572  std::ostringstream orderOss;
573  orderOss << cmdOpt.getOrder();
574  TUASSERTE(unsigned long,1,cmdOpt.getOrder());
575  }
576  }
577  catch ( ... )
578  {
579  TUFAIL("CommandOptionParser() threw an exception while parsing but"
580  " should not have.");
581  }
582 
583  defaultCommandOptionList.clear();
584 
585  try // Parse a CommandOptionNoArg
586  {
587  CommandOptionNoArg cmdOpt('f', "foo", "Foo", false);
588  CommandOptionParser cop("Description");
589  int argc = 3;
590  char* argv[] = { const_cast<char*>("program"), const_cast<char*>("-f"),
591  const_cast<char*>("--foo") };
592  cop.parseOptions(argc, argv);
593  TUPASS("CommandOptionParser parsed the options without throwing an"
594  " exception.");
595  if (cop.hasErrors() )
596  {
597  std::ostringstream oss;
598  oss << "CommandOptionParser encountered unexpected errors while"
599  << " parsing: ";
600  cop.dumpErrors(oss);
601  TUFAIL(oss.str());
602  }
603  else
604  {
605  TUPASS("CommandOptionParser parsed without errors.");
606  std::ostringstream countOss;
607  countOss << cmdOpt.getCount();
608  TUASSERTE(unsigned long,2,cmdOpt.getCount());
609  std::ostringstream orderOss;
610  orderOss << cmdOpt.getOrder();
611  TUASSERTE(unsigned long,2,cmdOpt.getOrder());
612  }
613  }
614  catch ( ... )
615  {
616  TUFAIL("CommandOptionParser() threw an exception while parsing but"
617  " should not have.");
618  }
619 
620  defaultCommandOptionList.clear();
621 
622  try // Parse a CommandOptionWithArg
623  {
624  CommandOptionWithArg cmdOpt(CommandOption::stdType, 'f', "foo", "Foo",
625  false);
626  CommandOptionParser cop("Description");
627  int argc = 5;
628  char* argv[] = { const_cast<char*>("program"), const_cast<char*>("-f"),
629  const_cast<char*>("value1"),
630  const_cast<char*>("--foo"),
631  const_cast<char*>("value2") };
632  cop.parseOptions(argc, argv);
633  TUPASS("CommandOptionParser parsed the options without throwing an"
634  " exception.");
635  if (cop.hasErrors() )
636  {
637  std::ostringstream oss;
638  oss << "CommandOptionParser encountered unexpected errors while"
639  << " parsing: ";
640  cop.dumpErrors(oss);
641  TUFAIL(oss.str());
642  }
643  else
644  {
645  TUPASS("CommandOptionParser parsed without errors.");
646  std::ostringstream countOss;
647  countOss << cmdOpt.getCount();
648  TUASSERTE(unsigned long,2,cmdOpt.getCount());
649  std::ostringstream orderOss;
650  orderOss << cmdOpt.getOrder();
651  TUASSERTE(unsigned long,2,cmdOpt.getOrder());
652  std::vector<std::string> values = cmdOpt.getValue();
653  TUASSERTE(unsigned long,2,values.size());
654  if (values.size() == 2)
655  {
656  TUASSERTE(std::string,"value1",values[0]);
657  TUASSERTE(std::string,"value2",values[1]);
658  }
659  }
660  }
661  catch ( ... )
662  {
663  TUFAIL("CommandOptionParser() threw an exception while parsing but"
664  " should not have.");
665  }
666 
667  defaultCommandOptionList.clear();
668 
669  try // Parse a CommandOptionWithAnyArg
670  {
671  CommandOptionWithAnyArg cmdOpt('f', "foo", "Foo", false);
672  CommandOptionParser cop("Description");
673  int argc = 5;
674  char* argv[] = { const_cast<char*>("program"), const_cast<char*>("-f"),
675  const_cast<char*>("value1"),
676  const_cast<char*>("--foo"),
677  const_cast<char*>("value2") };
678  cop.parseOptions(argc, argv);
679  TUPASS("CommandOptionParser parsed the options without throwing an"
680  " exception.");
681  if (cop.hasErrors() )
682  {
683  std::ostringstream oss;
684  oss << "CommandOptionParser encountered unexpected errors while"
685  << " parsing: ";
686  cop.dumpErrors(oss);
687  TUFAIL(oss.str());
688  }
689  else
690  {
691  TUPASS("CommandOptionParser parsed without errors.");
692  std::ostringstream countOss;
693  countOss << cmdOpt.getCount();
694  TUASSERTE(unsigned long,2,cmdOpt.getCount());
695  std::ostringstream orderOss;
696  orderOss << cmdOpt.getOrder();
697  TUASSERTE(unsigned long,2,cmdOpt.getOrder());
698  std::vector<std::string> values = cmdOpt.getValue();
699  TUASSERTE(unsigned long,2,values.size());
700  if (values.size() == 2)
701  {
702  TUASSERTE(std::string,"value1",values[0]);
703  TUASSERTE(std::string,"value2",values[1]);
704  }
705  }
706  }
707  catch ( ... )
708  {
709  TUFAIL("CommandOptionParser() threw an exception while parsing but"
710  " should not have.");
711  }
712 
713  defaultCommandOptionList.clear();
714 
715  try // Parse a CommandOptionWithStringArg (invalid)
716  {
717  CommandOptionWithStringArg cmdOpt('f', "foo", "Foo", false);
718  CommandOptionParser cop("Description");
719  int argc = 5;
720  char* argv[] = { const_cast<char*>("program"),
721  const_cast<char*>("-f"),
722  const_cast<char*>("value1"),
723  const_cast<char*>("--foo"),
724  const_cast<char*>("value2") };
725  cop.parseOptions(argc, argv);
726  TUPASS("CommandOptionParser parsed the options without throwing an"
727  " exception.");
728  if (cop.hasErrors() )
729  {
730  std::ostringstream oss;
731  oss << "CommandOptionParser encountered expected errors while"
732  << " parsing: ";
733  cop.dumpErrors(oss);
734  TUPASS(oss.str());
735  }
736  else
737  {
738  TUFAIL("CommandOptionParser parsed without errors but should have"
739  " rejected the argument value due to its format");
740  }
741  }
742  catch ( ... )
743  {
744  TUFAIL("CommandOptionParser() threw an exception while parsing but"
745  " should not have.");
746  }
747 
748  defaultCommandOptionList.clear();
749 
750  try // Parse a CommandOptionWithStringArg (valid)
751  {
752  CommandOptionWithStringArg cmdOpt('f', "foo", "Foo", false);
753  CommandOptionParser cop("Description");
754  int argc = 5;
755  char* argv[] = { const_cast<char*>("program"), const_cast<char*>("-f"),
756  const_cast<char*>("valueOne"),
757  const_cast<char*>("--foo"),
758  const_cast<char*>("valueTwo") };
759  cop.parseOptions(argc, argv);
760  TUPASS("CommandOptionParser parsed the options without throwing an"
761  " exception.");
762  if (cop.hasErrors() )
763  {
764  std::ostringstream oss;
765  oss << "CommandOptionParser encountered unexpected errors while"
766  << " parsing: ";
767  cop.dumpErrors(oss);
768  TUFAIL(oss.str());
769  }
770  else
771  {
772  TUPASS("CommandOptionParser parsed without errors.");
773  std::ostringstream countOss;
774  countOss << cmdOpt.getCount();
775  TUASSERTE(unsigned long,2,cmdOpt.getCount());
776  std::ostringstream orderOss;
777  orderOss << cmdOpt.getOrder();
778  TUASSERTE(unsigned long,2,cmdOpt.getOrder());
779  std::vector<std::string> values = cmdOpt.getValue();
780  TUASSERTE(unsigned long,2,values.size());
781  if (values.size() == 2)
782  {
783  TUASSERTE(std::string,"valueOne",values[0]);
784  TUASSERTE(std::string,"valueTwo",values[1]);
785  }
786  }
787  }
788  catch ( ... )
789  {
790  TUFAIL("CommandOptionParser() threw an exception while parsing but"
791  " should not have.");
792  }
793 
794  defaultCommandOptionList.clear();
795 
796  try // Parse a CommandOptionWithNumberArg (invalid)
797  {
798  CommandOptionWithNumberArg cmdOpt('f', "foo", "Foo", false);
799  CommandOptionParser cop("Description");
800  int argc = 5;
801  char* argv[] = { const_cast<char*>("program"),
802  const_cast<char*>("-f"),
803  const_cast<char*>("value"),
804  const_cast<char*>("--foo"),
805  const_cast<char*>("12.45") };
806  cop.parseOptions(argc, argv);
807  TUPASS("CommandOptionParser parsed the options without throwing an"
808  " exception.");
809  if (cop.hasErrors() )
810  {
811  std::ostringstream oss;
812  oss << "CommandOptionParser encountered expected errors while"
813  << " parsing: ";
814  cop.dumpErrors(oss);
815  TUPASS(oss.str());
816  }
817  else
818  {
819  TUFAIL("CommandOptionParser parsed without errors but should have"
820  " rejected the argument value due to its format");
821  }
822  }
823  catch ( ... )
824  {
825  TUFAIL("CommandOptionParser() threw an exception while parsing but"
826  " should not have.");
827  }
828 
829  defaultCommandOptionList.clear();
830 
831  try // Parse a CommandOptionWithNumberArg (valid)
832  {
833  CommandOptionWithNumberArg cmdOpt('f', "foo", "Foo", false);
834  CommandOptionParser cop("Description");
835  int argc = 5;
836  char* argv[] = { const_cast<char*>("program"),
837  const_cast<char*>("-f"),
838  const_cast<char*>("0"),
839  const_cast<char*>("--foo"),
840  const_cast<char*>("12345") };
841  cop.parseOptions(argc, argv);
842  TUPASS("CommandOptionParser parsed the options without throwing an"
843  " exception.");
844  if (cop.hasErrors() )
845  {
846  std::ostringstream oss;
847  oss << "CommandOptionParser encountered unexpected errors while"
848  << " parsing: ";
849  cop.dumpErrors(oss);
850  TUFAIL(oss.str());
851  }
852  else
853  {
854  TUPASS("CommandOptionParser parsed without errors.");
855  std::ostringstream countOss;
856  countOss << cmdOpt.getCount();
857  TUASSERTE(unsigned long,2,cmdOpt.getCount());
858  std::ostringstream orderOss;
859  orderOss << cmdOpt.getOrder();
860  TUASSERTE(unsigned long,2,cmdOpt.getOrder());
861  std::vector<std::string> values = cmdOpt.getValue();
862  TUASSERTE(unsigned long,2,values.size());
863  if (values.size() == 2)
864  {
865  TUASSERTE(std::string,"0",values[0]);
866  TUASSERTE(std::string,"12345",values[1]);
867  }
868  }
869  }
870  catch ( ... )
871  {
872  TUFAIL("CommandOptionParser() threw an exception while parsing but"
873  " should not have.");
874  }
875 
876  defaultCommandOptionList.clear();
877 
878  try // Parse a CommandOptionWithDecimalArg (invalid)
879  {
880  CommandOptionWithDecimalArg cmdOpt('f', "foo", "Foo", false);
881  CommandOptionParser cop("Description");
882  int argc = 5;
883  char* argv[] = { const_cast<char*>("program"),
884  const_cast<char*>("-f"),
885  const_cast<char*>("value"),
886  const_cast<char*>("--foo"),
887  const_cast<char*>("1.2e34") };
888  cop.parseOptions(argc, argv);
889  TUPASS("CommandOptionParser parsed the options without throwing an"
890  " exception.");
891  if (cop.hasErrors() )
892  {
893  std::ostringstream oss;
894  oss << "CommandOptionParser encountered expected errors while"
895  << " parsing: ";
896  cop.dumpErrors(oss);
897  TUPASS(oss.str());
898  }
899  else
900  {
901  TUFAIL("CommandOptionParser parsed without errors but should have"
902  " rejected the argument value due to its format");
903  }
904  }
905  catch ( ... )
906  {
907  TUFAIL("CommandOptionParser() threw an exception while parsing but"
908  " should not have.");
909  }
910 
911  defaultCommandOptionList.clear();
912 
913  try // Parse a CommandOptionWithDecimalArg (valid)
914  {
915  CommandOptionWithDecimalArg cmdOpt('f', "foo", "Foo", false);
916  CommandOptionParser cop("Description");
917  int argc = 5;
918  char* argv[] = { const_cast<char*>("program"),
919  const_cast<char*>("-f"),
920  const_cast<char*>("0"),
921  const_cast<char*>("--foo"),
922  const_cast<char*>("123.45") };
923  cop.parseOptions(argc, argv);
924  TUPASS("CommandOptionParser parsed the options without throwing an"
925  " exception.");
926  if (cop.hasErrors() )
927  {
928  std::ostringstream oss;
929  oss << "CommandOptionParser encountered unexpected errors while"
930  << " parsing: ";
931  cop.dumpErrors(oss);
932  TUFAIL(oss.str());
933  }
934  else
935  {
936  TUPASS("CommandOptionParser parsed without errors.");
937  std::ostringstream countOss;
938  countOss << cmdOpt.getCount();
939  TUASSERTE(unsigned long,2,cmdOpt.getCount());
940  std::ostringstream orderOss;
941  orderOss << cmdOpt.getOrder();
942  TUASSERTE(unsigned long,2,cmdOpt.getOrder());
943  std::vector<std::string> values = cmdOpt.getValue();
944  TUASSERTE(unsigned long,2,values.size());
945  if (values.size() == 2)
946  {
947  TUASSERTE(std::string,"0",values[0]);
948  TUASSERTE(std::string,"123.45",values[1]);
949  }
950  }
951  }
952  catch ( ... )
953  {
954  TUFAIL("CommandOptionParser() threw an exception while parsing but"
955  " should not have.");
956  }
957 
958  defaultCommandOptionList.clear();
959 
960  try // Parse a CommandOptionWithCommonTimeArg (invalid)
961  {
962  CommandOptionWithCommonTimeArg cmdOpt('t', "time", "%Y %j %s", "Time",
963  false);
964  CommandOptionParser cop("Description");
965  int argc = 5;
966  char* argv[] = { const_cast<char*>("program"),
967  const_cast<char*>("-t"),
968  const_cast<char*>("value"),
969  const_cast<char*>("--time"),
970  const_cast<char*>("1234") };
971  cop.parseOptions(argc, argv);
972  TUPASS("CommandOptionParser parsed the options without throwing an"
973  " exception.");
974  if (cop.hasErrors() )
975  {
976  std::ostringstream oss;
977  oss << "CommandOptionParser encountered expected errors while"
978  << " parsing: ";
979  cop.dumpErrors(oss);
980  TUPASS(oss.str());
981  }
982  else
983  {
984  TUFAIL("CommandOptionParser parsed without errors but should have"
985  " rejected the argument value due to its format.");
986  }
987  }
988  catch ( ... )
989  {
990  TUFAIL("CommandOptionParser() threw an exception while parsing but"
991  " should not have.");
992  }
993 
994  defaultCommandOptionList.clear();
995 
996  try // Parse a CommandOptionWithCommonTimeArg (valid YDS)
997  {
998  CommandOptionWithCommonTimeArg cmdOpt('t', "time", "%Y %j %s", "Time",
999  false);
1000  CommandOptionParser cop("Description");
1001  int argc = 5;
1002  char* argv[] = { const_cast<char*>("program"),
1003  const_cast<char*>("-t"),
1004  const_cast<char*>("2015 123 45678.0"),
1005  const_cast<char*>("--time"),
1006  const_cast<char*>("2015 234 56789.0") };
1007  cop.parseOptions(argc, argv);
1008  TUPASS("CommandOptionParser parsed the options without throwing an"
1009  " exception.");
1010  if (cop.hasErrors() )
1011  {
1012  std::ostringstream oss;
1013  oss << "CommandOptionParser encountered unexpected errors while"
1014  << " parsing: ";
1015  cop.dumpErrors(oss);
1016  TUFAIL(oss.str());
1017  }
1018  else
1019  {
1020  TUPASS("CommandOptionParser parsed without errors.");
1021  std::ostringstream countOss;
1022  countOss << cmdOpt.getCount();
1023  TUASSERTE(unsigned long,2,cmdOpt.getCount());
1024  std::ostringstream orderOss;
1025  orderOss << cmdOpt.getOrder();
1026  TUASSERTE(unsigned long,2,cmdOpt.getOrder());
1027  std::vector<std::string> values = cmdOpt.getValue();
1028  TUASSERTE(unsigned long,2,values.size());
1029  if (values.size() == 2)
1030  {
1031  TUASSERTE(std::string,"2015 123 45678.0",values[0]);
1032  TUASSERTE(std::string,"2015 234 56789.0",values[1]);
1033  }
1034  std::vector<gnsstk::CommonTime> times = cmdOpt.getTime();
1035  TUASSERTE(unsigned long,2,times.size());
1036  if (times.size() == 2)
1037  {
1038  gnsstk::CommonTime t1 =
1039  gnsstk::YDSTime(2015, 123, 45678.0).convertToCommonTime();
1040  gnsstk::CommonTime t2 =
1041  gnsstk::YDSTime(2015, 234, 56789.0).convertToCommonTime();
1042  TUASSERTE(gnsstk::CommonTime,t1,times[0]);
1043  TUASSERTE(gnsstk::CommonTime,t2,times[1]);
1044  }
1045  }
1046  }
1047  catch ( ... )
1048  {
1049  TUFAIL("CommandOptionParser() threw an unexpected exception.");
1050  }
1051 
1052  defaultCommandOptionList.clear();
1053 
1054  try // Parse with a missing required CommandOptionRest
1055  {
1056  CommandOptionRest cmdOpt("Description", true);
1057  CommandOptionParser cop("Description");
1058  int argc = 1;
1059  char* argv[] = { const_cast<char*>("program") };
1060  cop.parseOptions(argc, argv);
1061  TUPASS("CommandOptionParser parsed the options without throwing an"
1062  " exception.");
1063  TUASSERT(cop.hasErrors());
1064  }
1065  catch ( ... )
1066  {
1067  TUFAIL("CommandOptionParser() threw an exception while parsing but"
1068  " should not have.");
1069  }
1070 
1071  defaultCommandOptionList.clear();
1072 
1073  try // Parse a CommandOptionRest
1074  {
1075  CommandOptionRest cmdOpt("Description", false);
1076  CommandOptionParser cop("Description");
1077  int argc = 3;
1078  char* argv[] = { const_cast<char*>("program"),
1079  const_cast<char*>("trailing1"),
1080  const_cast<char*>("trailing2") };
1081  cop.parseOptions(argc, argv);
1082  TUPASS("CommandOptionParser parsed the options without throwing an"
1083  " exception.");
1084  if (cop.hasErrors() )
1085  {
1086  std::ostringstream oss;
1087  oss << "CommandOptionParser encountered unexpected errors while"
1088  << " parsing: ";
1089  cop.dumpErrors(oss);
1090  TUFAIL(oss.str());
1091  }
1092  else
1093  {
1094  TUPASS("CommandOptionParser parsed without errors.");
1095  std::ostringstream countOss;
1096  countOss << cmdOpt.getCount();
1097  TUASSERTE(unsigned long,2,cmdOpt.getCount());
1098  std::ostringstream orderOss;
1099  orderOss << cmdOpt.getOrder();
1100  TUASSERTE(unsigned long,2,cmdOpt.getOrder());
1101  std::vector<std::string> values = cmdOpt.getValue();
1102  TUASSERTE(unsigned long,2,values.size());
1103  if (values.size() == 2)
1104  {
1105  TUASSERTE(std::string,"trailing1",values[0]);
1106  TUASSERTE(std::string,"trailing2",values[1]);
1107  }
1108  }
1109  }
1110  catch ( ... )
1111  {
1112  TUFAIL("CommandOptionParser() threw an exception while parsing but"
1113  " should not have.");
1114  }
1115 
1116  defaultCommandOptionList.clear();
1117 
1118  TURETURN();
1119 }
1120 
1121 
1122 /*************************************************************************
1123  */
1125 {
1126  TUDEF("CommandOptionParser", "OptionPresence");
1127 
1128  try // Parse with a satisfied CommandOptionMutex
1129  {
1130  CommandOptionWithAnyArg cmdOptF('F', "foo", "Foo", false);
1131  CommandOptionWithAnyArg cmdOptB('B', "bar", "Bar", false);
1132  CommandOptionRest cmdOptRest("Rest", false);
1133  CommandOptionMutex com;
1134  com.addOption(&cmdOptF);
1135  com.addOption(&cmdOptB);
1136  CommandOptionParser cop("Description");
1137  int argc = 6;
1138  char* argv[] = { const_cast<char*>("program"),
1139  const_cast<char*>("-F"),
1140  const_cast<char*>("value1"),
1141  const_cast<char*>("--foo"),
1142  const_cast<char*>("value2"),
1143  const_cast<char*>("trailing") };
1144  cop.parseOptions(argc, argv);
1145  TUPASS("CommandOptionParser parsed the options without throwing an"
1146  " exception.");
1147  if (cop.hasErrors() )
1148  {
1149  std::ostringstream oss;
1150  oss << "CommandOptionParser encountered unexpected errors while"
1151  << " parsing: ";
1152  cop.dumpErrors(oss);
1153  TUFAIL(oss.str());
1154  }
1155  else
1156  {
1157  TUPASS("CommandOptionParser parsed without errors.");
1158  TUASSERTE(CommandOption*,&cmdOptF,com.whichOne());
1159  }
1160  }
1161  catch ( ... )
1162  {
1163  TUFAIL("CommandOptionParser() threw an exception while parsing but"
1164  " should not have.");
1165  }
1166 
1167  defaultCommandOptionList.clear();
1168 
1169  try // Parse with a violated CommandOptionMutex
1170  {
1171  CommandOptionWithAnyArg cmdOptF('F', "foo", "Foo", false);
1172  CommandOptionWithAnyArg cmdOptB('B', "bar", "Bar", false);
1173  CommandOptionRest cmdOptRest("Rest", false);
1174  CommandOptionMutex com;
1175  com.addOption(&cmdOptF);
1176  com.addOption(&cmdOptB);
1177  CommandOptionParser cop("Description");
1178  int argc = 6;
1179  char* argv[] = { const_cast<char*>("program"),
1180  const_cast<char*>("-F"),
1181  const_cast<char*>("value1"),
1182  const_cast<char*>("-B"),
1183  const_cast<char*>("value2"),
1184  const_cast<char*>("trailing") };
1185  cop.parseOptions(argc, argv);
1186  TUPASS("CommandOptionParser parsed the options without throwing an"
1187  " exception.");
1188  TUASSERT(cop.hasErrors());
1189  }
1190  catch ( ... )
1191  {
1192  TUFAIL("CommandOptionParser() threw an exception while parsing but"
1193  " should not have.");
1194  }
1195 
1196  defaultCommandOptionList.clear();
1197 
1198  try // Parse with a violated CommandOptionMutex
1199  {
1200  CommandOptionWithAnyArg cmdOptF('F', "foo", "Foo", false);
1201  CommandOptionWithAnyArg cmdOptB('B', "bar", "Bar", false);
1202  CommandOptionRest cmdOptRest("Rest", false);
1203  CommandOptionMutex com;
1204  com.addOption(&cmdOptF);
1205  com.addOption(&cmdOptRest);
1206  CommandOptionParser cop("Description");
1207  int argc = 6;
1208  char* argv[] = { const_cast<char*>("program"),
1209  const_cast<char*>("-F"),
1210  const_cast<char*>("value1"),
1211  const_cast<char*>("-B"),
1212  const_cast<char*>("value2"),
1213  const_cast<char*>("trailing") };
1214  cop.parseOptions(argc, argv);
1215  TUPASS("CommandOptionParser parsed the options without throwing an"
1216  " exception.");
1217  TUASSERT(cop.hasErrors());
1218  }
1219  catch ( ... )
1220  {
1221  TUFAIL("CommandOptionParser() threw an exception while parsing but"
1222  " should not have.");
1223  }
1224 
1225  defaultCommandOptionList.clear();
1226 
1227  try // Parse with a satisfied CommandOptionDependent
1228  {
1229  CommandOptionWithAnyArg cmdOptF('F', "foo", "Foo", false);
1230  CommandOptionWithAnyArg cmdOptB('B', "bar", "Bar", false);
1231  CommandOptionRest cmdOptRest("Rest", false);
1232  CommandOptionDependent cod(&cmdOptF, &cmdOptB);
1233  CommandOptionParser cop("Description");
1234  int argc = 6;
1235  char* argv[] = { const_cast<char*>("program"),
1236  const_cast<char*>("-F"),
1237  const_cast<char*>("value1"),
1238  const_cast<char*>("-B"),
1239  const_cast<char*>("value2"),
1240  const_cast<char*>("trailing") };
1241  cop.parseOptions(argc, argv);
1242  TUPASS("CommandOptionParser parsed the options without throwing an"
1243  " exception.");
1244  if (cop.hasErrors() )
1245  {
1246  std::ostringstream oss;
1247  oss << "CommandOptionParser encountered unexpected errors while"
1248  << " parsing: ";
1249  cop.dumpErrors(oss);
1250  TUFAIL(oss.str());
1251  }
1252  else
1253  {
1254  TUPASS("CommandOptionParser parsed without errors.");
1255  }
1256  }
1257  catch ( ... )
1258  {
1259  TUFAIL("CommandOptionParser() threw an exception while parsing but"
1260  " should not have.");
1261  }
1262 
1263  defaultCommandOptionList.clear();
1264 
1265  try // Parse with a satisfied CommandOptionDependent
1266  {
1267  CommandOptionWithAnyArg cmdOptF('F', "foo", "Foo", false);
1268  CommandOptionWithAnyArg cmdOptB('B', "bar", "Bar", false);
1269  CommandOptionRest cmdOptRest("Rest", false);
1270  CommandOptionDependent cod(&cmdOptB, &cmdOptRest);
1271  CommandOptionParser cop("Description");
1272  int argc = 6;
1273  char* argv[] = { const_cast<char*>("program"),
1274  const_cast<char*>("-F"),
1275  const_cast<char*>("value1"),
1276  const_cast<char*>("-B"),
1277  const_cast<char*>("value2"),
1278  const_cast<char*>("trailing") };
1279  cop.parseOptions(argc, argv);
1280  TUPASS("CommandOptionParser parsed the options without throwing an"
1281  " exception.");
1282  if (cop.hasErrors() )
1283  {
1284  std::ostringstream oss;
1285  oss << "CommandOptionParser encountered unexpected errors while"
1286  << " parsing: ";
1287  cop.dumpErrors(oss);
1288  TUFAIL(oss.str());
1289  }
1290  else
1291  {
1292  TUPASS("CommandOptionParser parsed without errors.");
1293  }
1294  }
1295  catch ( ... )
1296  {
1297  TUFAIL("CommandOptionParser() threw an exception while parsing but"
1298  " should not have.");
1299  }
1300 
1301  defaultCommandOptionList.clear();
1302 
1303  try // Parse with a violated CommandOptionDependent
1304  {
1305  CommandOptionWithAnyArg cmdOptF('F', "foo", "Foo", false);
1306  CommandOptionWithAnyArg cmdOptB('B', "bar", "Bar", false);
1307  CommandOptionRest cmdOptRest("Rest", false);
1308  CommandOptionDependent cod(&cmdOptB, &cmdOptF);
1309  CommandOptionParser cop("Description");
1310  int argc = 6;
1311  char* argv[] = { const_cast<char*>("program"),
1312  const_cast<char*>("-F"),
1313  const_cast<char*>("value1"),
1314  const_cast<char*>("--foo"),
1315  const_cast<char*>("value2"),
1316  const_cast<char*>("trailing") };
1317  cop.parseOptions(argc, argv);
1318  TUPASS("CommandOptionParser parsed the options without throwing an"
1319  " exception.");
1320  TUASSERT(cop.hasErrors());
1321  }
1322  catch ( ... )
1323  {
1324  TUFAIL("CommandOptionParser() threw an exception while parsing but"
1325  " should not have.");
1326  }
1327 
1328  defaultCommandOptionList.clear();
1329 
1330  try // Parse with a violated CommandOptionDependent
1331  {
1332  CommandOptionWithAnyArg cmdOptF('F', "foo", "Foo", false);
1333  CommandOptionWithAnyArg cmdOptB('B', "bar", "Bar", false);
1334  CommandOptionRest cmdOptRest("Rest", false);
1335  CommandOptionDependent cod(&cmdOptB, &cmdOptRest);
1336  CommandOptionParser cop("Description");
1337  int argc = 6;
1338  char* argv[] = { const_cast<char*>("program"),
1339  const_cast<char*>("-F"),
1340  const_cast<char*>("value1"),
1341  const_cast<char*>("--foo"),
1342  const_cast<char*>("value2"),
1343  const_cast<char*>("trailing") };
1344  cop.parseOptions(argc, argv);
1345  TUPASS("CommandOptionParser parsed the options without throwing an"
1346  " exception.");
1347  TUASSERT(cop.hasErrors());
1348  }
1349  catch ( ... )
1350  {
1351  TUFAIL("CommandOptionParser() threw an exception while parsing but"
1352  " should not have.");
1353  }
1354 
1355  defaultCommandOptionList.clear();
1356 
1357  try // Parse with a satisfied CommandOptionNOf
1358  {
1359  CommandOptionWithAnyArg cmdOptF('F', "foo", "Foo", false);
1360  CommandOptionWithAnyArg cmdOptB('B', "bar", "Bar", false);
1361  CommandOptionRest cmdOptRest("Rest", false);
1362  CommandOptionNOf conof(0);
1363  conof.addOption(&cmdOptB);
1364  CommandOptionParser cop("Description");
1365  int argc = 6;
1366  char* argv[] = { const_cast<char*>("program"),
1367  const_cast<char*>("-F"),
1368  const_cast<char*>("value1"),
1369  const_cast<char*>("--foo"),
1370  const_cast<char*>("value2"),
1371  const_cast<char*>("trailing") };
1372  cop.parseOptions(argc, argv);
1373  TUPASS("CommandOptionParser parsed the options without throwing an"
1374  " exception.");
1375  if (cop.hasErrors() )
1376  {
1377  std::ostringstream oss;
1378  oss << "CommandOptionParser encountered unexpected errors while"
1379  << " parsing: ";
1380  cop.dumpErrors(oss);
1381  TUFAIL(oss.str());
1382  }
1383  else
1384  {
1385  TUPASS("CommandOptionParser parsed without errors.");
1386  // @todo - Check which() result
1387  }
1388  }
1389  catch ( ... )
1390  {
1391  TUFAIL("CommandOptionParser() threw an exception while parsing but"
1392  " should not have.");
1393  }
1394 
1395  defaultCommandOptionList.clear();
1396 
1397  try // Parse with a satisfied CommandOptionNOf
1398  {
1399  CommandOptionWithAnyArg cmdOptF('F', "foo", "Foo", false);
1400  CommandOptionWithAnyArg cmdOptB('B', "bar", "Bar", false);
1401  CommandOptionRest cmdOptRest("Rest", false);
1402  CommandOptionNOf conof(1);
1403  conof.addOption(&cmdOptF);
1404  conof.addOption(&cmdOptB);
1405  CommandOptionParser cop("Description");
1406  int argc = 4;
1407  char* argv[] = { const_cast<char*>("program"),
1408  const_cast<char*>("-F"),
1409  const_cast<char*>("value1"),
1410  const_cast<char*>("trailing") };
1411  cop.parseOptions(argc, argv);
1412  TUPASS("CommandOptionParser parsed the options without throwing an"
1413  " exception.");
1414  if (cop.hasErrors() )
1415  {
1416  std::ostringstream oss;
1417  oss << "CommandOptionParser encountered unexpected errors while"
1418  << " parsing: ";
1419  cop.dumpErrors(oss);
1420  TUFAIL(oss.str());
1421  }
1422  else
1423  {
1424  TUPASS("CommandOptionParser parsed without errors.");
1425  // @todo - Check which() result
1426  }
1427  }
1428  catch ( ... )
1429  {
1430  TUFAIL("CommandOptionParser() threw an exception while parsing but"
1431  " should not have.");
1432  }
1433 
1434  defaultCommandOptionList.clear();
1435 
1436  try // Parse with a satisfied CommandOptionNOf
1437  {
1438  CommandOptionWithAnyArg cmdOptF('F', "foo", "Foo", false);
1439  CommandOptionWithAnyArg cmdOptB('B', "bar", "Bar", false);
1440  CommandOptionRest cmdOptRest("Rest", false);
1441  CommandOptionNOf conof(2);
1442  conof.addOption(&cmdOptF);
1443  conof.addOption(&cmdOptB);
1444  CommandOptionParser cop("Description");
1445  int argc = 6;
1446  char* argv[] = { const_cast<char*>("program"),
1447  const_cast<char*>("-F"),
1448  const_cast<char*>("value1"),
1449  const_cast<char*>("-B"),
1450  const_cast<char*>("value2"),
1451  const_cast<char*>("trailing") };
1452  cop.parseOptions(argc, argv);
1453  TUPASS("CommandOptionParser parsed the options without throwing an"
1454  " exception.");
1455  if (cop.hasErrors() )
1456  {
1457  std::ostringstream oss;
1458  oss << "CommandOptionParser encountered unexpected errors while"
1459  << " parsing: ";
1460  cop.dumpErrors(oss);
1461  TUFAIL(oss.str());
1462  }
1463  else
1464  {
1465  TUPASS("CommandOptionParser parsed without errors.");
1466  // @todo - Check which() result
1467  }
1468  }
1469  catch ( ... )
1470  {
1471  TUFAIL("CommandOptionParser() threw an exception while parsing but"
1472  " should not have.");
1473  }
1474 
1475  defaultCommandOptionList.clear();
1476 
1477  try // Parse with a satisfied CommandOptionNOf
1478  {
1479  CommandOptionWithAnyArg cmdOptF('F', "foo", "Foo", false);
1480  CommandOptionWithAnyArg cmdOptB('B', "bar", "Bar", false);
1481  CommandOptionRest cmdOptRest("Rest", false);
1482  CommandOptionNOf conof(2);
1483  conof.addOption(&cmdOptF);
1484  conof.addOption(&cmdOptB);
1485  CommandOptionParser cop("Description");
1486  int argc = 6;
1487  char* argv[] = { const_cast<char*>("program"),
1488  const_cast<char*>("-F"),
1489  const_cast<char*>("value1"),
1490  const_cast<char*>("--foo"),
1491  const_cast<char*>("value2"),
1492  const_cast<char*>("trailing") };
1493  cop.parseOptions(argc, argv);
1494  TUPASS("CommandOptionParser parsed the options without throwing an"
1495  " exception.");
1496  if (cop.hasErrors() )
1497  {
1498  std::ostringstream oss;
1499  oss << "CommandOptionParser encountered unexpected errors while"
1500  << " parsing: ";
1501  cop.dumpErrors(oss);
1502  TUFAIL(oss.str());
1503  }
1504  else
1505  {
1506  TUPASS("CommandOptionParser parsed without errors.");
1507  // @todo - Check which() result
1508  }
1509  }
1510  catch ( ... )
1511  {
1512  TUFAIL("CommandOptionParser() threw an exception while parsing but"
1513  " should not have.");
1514  }
1515 
1516  defaultCommandOptionList.clear();
1517 
1518  try // Parse with a violated CommandOptionNOf
1519  {
1520  CommandOptionWithAnyArg cmdOptF('F', "foo", "Foo", false);
1521  CommandOptionWithAnyArg cmdOptB('B', "bar", "Bar", false);
1522  CommandOptionRest cmdOptRest("Rest", false);
1523  CommandOptionNOf conof(0);
1524  conof.addOption(&cmdOptB);
1525  CommandOptionParser cop("Description");
1526  int argc = 6;
1527  char* argv[] = { const_cast<char*>("program"),
1528  const_cast<char*>("-F"),
1529  const_cast<char*>("value1"),
1530  const_cast<char*>("-B"),
1531  const_cast<char*>("value2"),
1532  const_cast<char*>("trailing") };
1533  cop.parseOptions(argc, argv);
1534  TUPASS("CommandOptionParser parsed the options without throwing an"
1535  " exception.");
1536  TUASSERT(cop.hasErrors());
1537  }
1538  catch ( ... )
1539  {
1540  TUFAIL("CommandOptionParser() threw an exception while parsing but"
1541  " should not have.");
1542  }
1543 
1544  defaultCommandOptionList.clear();
1545 
1546  try // Parse with a violated CommandOptionNOf
1547  {
1548  CommandOptionWithAnyArg cmdOptF('F', "foo", "Foo", false);
1549  CommandOptionWithAnyArg cmdOptB('B', "bar", "Bar", false);
1550  CommandOptionRest cmdOptRest("Rest", false);
1551  CommandOptionNOf conof(1);
1552  conof.addOption(&cmdOptF);
1553  conof.addOption(&cmdOptB);
1554  CommandOptionParser cop("Description");
1555  int argc = 6;
1556  char* argv[] = { const_cast<char*>("program"),
1557  const_cast<char*>("-F"),
1558  const_cast<char*>("value1"),
1559  const_cast<char*>("-B"),
1560  const_cast<char*>("value2"),
1561  const_cast<char*>("trailing") };
1562  cop.parseOptions(argc, argv);
1563  TUPASS("CommandOptionParser parsed the options without throwing an"
1564  " exception.");
1565  TUASSERT(cop.hasErrors());
1566  }
1567  catch ( ... )
1568  {
1569  TUFAIL("CommandOptionParser() threw an exception while parsing but"
1570  " should not have.");
1571  }
1572 
1573  defaultCommandOptionList.clear();
1574 
1575  try // Parse with a violated CommandOptionNOf
1576  {
1577  CommandOptionWithAnyArg cmdOptF('F', "foo", "Foo", false);
1578  CommandOptionWithAnyArg cmdOptB('B', "bar", "Bar", false);
1579  CommandOptionRest cmdOptRest("Rest", false);
1580  CommandOptionNOf conof(3);
1581  conof.addOption(&cmdOptF);
1582  conof.addOption(&cmdOptB);
1583  CommandOptionParser cop("Description");
1584  int argc = 6;
1585  char* argv[] = { const_cast<char*>("program"),
1586  const_cast<char*>("-F"),
1587  const_cast<char*>("value1"),
1588  const_cast<char*>("-B"),
1589  const_cast<char*>("value2"),
1590  const_cast<char*>("trailing") };
1591  cop.parseOptions(argc, argv);
1592  TUPASS("CommandOptionParser parsed the options without throwing an"
1593  " exception.");
1594  TUASSERT(cop.hasErrors());
1595  }
1596  catch ( ... )
1597  {
1598  TUFAIL("CommandOptionParser() threw an exception while parsing but"
1599  " should not have.");
1600  }
1601 
1602  defaultCommandOptionList.clear();
1603 
1604  try // Parse with a satisfied CommandOptionOneOf
1605  {
1606  CommandOptionWithAnyArg cmdOptF('F', "foo", "Foo", false);
1607  CommandOptionWithAnyArg cmdOptB('B', "bar", "Bar", false);
1608  CommandOptionRest cmdOptRest("Rest", false);
1609  CommandOptionOneOf cooo;
1610  cooo.addOption(&cmdOptB);
1611  CommandOptionParser cop("Description");
1612  int argc = 6;
1613  char* argv[] = { const_cast<char*>("program"),
1614  const_cast<char*>("-F"),
1615  const_cast<char*>("value1"),
1616  const_cast<char*>("-B"),
1617  const_cast<char*>("value2"),
1618  const_cast<char*>("trailing") };
1619  cop.parseOptions(argc, argv);
1620  TUPASS("CommandOptionParser parsed the options without throwing an"
1621  " exception.");
1622  if (cop.hasErrors() )
1623  {
1624  std::ostringstream oss;
1625  oss << "CommandOptionParser encountered unexpected errors while"
1626  << " parsing: ";
1627  cop.dumpErrors(oss);
1628  TUFAIL(oss.str());
1629  }
1630  else
1631  {
1632  TUPASS("CommandOptionParser parsed without errors.");
1633  TUASSERTE(CommandOption*,&cmdOptB,cooo.whichOne());
1634  }
1635  }
1636  catch ( ... )
1637  {
1638  TUFAIL("CommandOptionParser() threw an exception while parsing but"
1639  " should not have.");
1640  }
1641 
1642  defaultCommandOptionList.clear();
1643 
1644  try // Parse with a satisfied CommandOptionOneOf
1645  {
1646  CommandOptionWithAnyArg cmdOptF('F', "foo", "Foo", false);
1647  CommandOptionWithAnyArg cmdOptB('B', "bar", "Bar", false);
1648  CommandOptionRest cmdOptRest("Rest", false);
1649  CommandOptionOneOf cooo;
1650  cooo.addOption(&cmdOptF);
1651  cooo.addOption(&cmdOptRest);
1652  CommandOptionParser cop("Description");
1653  int argc = 6;
1654  char* argv[] = { const_cast<char*>("program"),
1655  const_cast<char*>("-F"),
1656  const_cast<char*>("value1"),
1657  const_cast<char*>("--foo"),
1658  const_cast<char*>("value2"),
1659  const_cast<char*>("trailing") };
1660  cop.parseOptions(argc, argv);
1661  TUPASS("CommandOptionParser parsed the options without throwing an"
1662  " exception.");
1663  if (cop.hasErrors() )
1664  {
1665  std::ostringstream oss;
1666  oss << "CommandOptionParser encountered unexpected errors while"
1667  << " parsing: ";
1668  cop.dumpErrors(oss);
1669  TUFAIL(oss.str());
1670  }
1671  else
1672  {
1673  TUPASS("CommandOptionParser parsed without errors.");
1674  TUASSERTE(CommandOption*,&cmdOptF,cooo.whichOne());
1675  }
1676  }
1677  catch ( ... )
1678  {
1679  TUFAIL("CommandOptionParser() threw an exception while parsing but"
1680  " should not have.");
1681  }
1682 
1683  defaultCommandOptionList.clear();
1684 
1685  try // Parse with a violated CommandOptionOneOf
1686  {
1687  CommandOptionWithAnyArg cmdOptF('F', "foo", "Foo", false);
1688  CommandOptionWithAnyArg cmdOptB('B', "bar", "Bar", false);
1689  CommandOptionRest cmdOptRest("Rest", false);
1690  CommandOptionOneOf cooo;
1691  cooo.addOption(&cmdOptB);
1692  cooo.addOption(&cmdOptRest);
1693  CommandOptionParser cop("Description");
1694  int argc = 5;
1695  char* argv[] = { const_cast<char*>("program"),
1696  const_cast<char*>("-F"),
1697  const_cast<char*>("value1"),
1698  const_cast<char*>("--foo"),
1699  const_cast<char*>("value2") };
1700  cop.parseOptions(argc, argv);
1701  TUPASS("CommandOptionParser parsed the options without throwing an"
1702  " exception.");
1703  TUASSERT(cop.hasErrors());
1704  }
1705  catch ( ... )
1706  {
1707  TUFAIL("CommandOptionParser() threw an exception while parsing but"
1708  " should not have.");
1709  }
1710 
1711  defaultCommandOptionList.clear();
1712 
1713  try // Parse with a satisfied CommandOptionAllOf
1714  {
1715  CommandOptionWithAnyArg cmdOptF('F', "foo", "Foo", false);
1716  CommandOptionWithAnyArg cmdOptB('B', "bar", "Bar", false);
1717  CommandOptionRest cmdOptRest("Rest", false);
1718  CommandOptionAllOf coao;
1719  coao.addOption(&cmdOptB);
1720  CommandOptionParser cop("Description");
1721  int argc = 6;
1722  char* argv[] = { const_cast<char*>("program"),
1723  const_cast<char*>("-F"),
1724  const_cast<char*>("value1"),
1725  const_cast<char*>("-B"),
1726  const_cast<char*>("value2"),
1727  const_cast<char*>("trailing") };
1728  cop.parseOptions(argc, argv);
1729  TUPASS("CommandOptionParser parsed the options without throwing an"
1730  " exception.");
1731  if (cop.hasErrors() )
1732  {
1733  std::ostringstream oss;
1734  oss << "CommandOptionParser encountered unexpected errors while"
1735  << " parsing: ";
1736  cop.dumpErrors(oss);
1737  TUFAIL(oss.str());
1738  }
1739  else
1740  {
1741  TUPASS("CommandOptionParser parsed without errors.");
1742  }
1743  }
1744  catch ( ... )
1745  {
1746  TUFAIL("CommandOptionParser() threw an exception while parsing but"
1747  " should not have.");
1748  }
1749 
1750  defaultCommandOptionList.clear();
1751 
1752  try // Parse with a satisfied CommandOptionAllOf
1753  {
1754  CommandOptionWithAnyArg cmdOptF('F', "foo", "Foo", false);
1755  CommandOptionWithAnyArg cmdOptB('B', "bar", "Bar", false);
1756  CommandOptionRest cmdOptRest("Rest", false);
1757  CommandOptionAllOf coao;
1758  coao.addOption(&cmdOptF);
1759  coao.addOption(&cmdOptB);
1760  coao.addOption(&cmdOptRest);
1761  CommandOptionParser cop("Description");
1762  int argc = 6;
1763  char* argv[] = { const_cast<char*>("program"),
1764  const_cast<char*>("-F"),
1765  const_cast<char*>("value1"),
1766  const_cast<char*>("-B"),
1767  const_cast<char*>("value2"),
1768  const_cast<char*>("trailing") };
1769  cop.parseOptions(argc, argv);
1770  TUPASS("CommandOptionParser parsed the options without throwing an"
1771  " exception.");
1772  if (cop.hasErrors() )
1773  {
1774  std::ostringstream oss;
1775  oss << "CommandOptionParser encountered unexpected errors while"
1776  << " parsing: ";
1777  cop.dumpErrors(oss);
1778  TUFAIL(oss.str());
1779  }
1780  else
1781  {
1782  TUPASS("CommandOptionParser parsed without errors.");
1783  }
1784  }
1785  catch ( ... )
1786  {
1787  TUFAIL("CommandOptionParser() threw an exception while parsing but"
1788  " should not have.");
1789  }
1790 
1791  defaultCommandOptionList.clear();
1792 
1793  try // Parse with a violated CommandOptionAllOf
1794  {
1795  CommandOptionWithAnyArg cmdOptF('F', "foo", "Foo", false);
1796  CommandOptionWithAnyArg cmdOptB('B', "bar", "Bar", false);
1797  CommandOptionRest cmdOptRest("Rest", false);
1798  CommandOptionAllOf coao;
1799  coao.addOption(&cmdOptF);
1800  coao.addOption(&cmdOptB);
1801  coao.addOption(&cmdOptRest);
1802  CommandOptionParser cop("Description");
1803  int argc = 6;
1804  char* argv[] = { const_cast<char*>("program"),
1805  const_cast<char*>("-F"),
1806  const_cast<char*>("value1"),
1807  const_cast<char*>("--foo"),
1808  const_cast<char*>("value2"),
1809  const_cast<char*>("trailing") };
1810  cop.parseOptions(argc, argv);
1811  TUPASS("CommandOptionParser parsed the options without throwing an"
1812  " exception.");
1813  TUASSERT(cop.hasErrors());
1814  }
1815  catch ( ... )
1816  {
1817  TUFAIL("CommandOptionParser() threw an exception while parsing but"
1818  " should not have.");
1819  }
1820 
1821  defaultCommandOptionList.clear();
1822 
1823 #ifdef BROKENTEST
1824 
1832  try // Parse with a satisfied CommandOptionGroupOr
1833  {
1834  CommandOptionWithAnyArg cmdOptF('F', "foo", "Foo", false);
1835  CommandOptionWithAnyArg cmdOptB('B', "bar", "Bar", false);
1836  CommandOptionRest cmdOptRest("Rest", false);
1837  CommandOptionGroupOr cogo;
1838  cogo.addOption(&cmdOptF);
1839  cogo.addOption(&cmdOptB);
1840  CommandOptionDependent(&cogo, &cmdOptRest);
1841  CommandOptionParser cop("Description");
1842  int argc = 6;
1843  char* argv[] = { const_cast<char*>("program"),
1844  const_cast<char*>("-F"),
1845  const_cast<char*>("value1"),
1846  const_cast<char*>("--foo"),
1847  const_cast<char*>("value2"),
1848  const_cast<char*>("trailing") };
1849  cop.parseOptions(argc, argv);
1850  TUPASS("CommandOptionParser parsed the options without throwing an"
1851  " exception.");
1852  if (cop.hasErrors() )
1853  {
1854  std::ostringstream oss;
1855  oss << "CommandOptionParser encountered unexpected errors while"
1856  << " parsing: ";
1857  cop.dumpErrors(oss);
1858  TUFAIL(oss.str());
1859  }
1860  else
1861  {
1862  TUPASS("CommandOptionParser parsed without errors.");
1863  TUASSERTE(CommandOption*,&cmdOptF,cogo.whichOne());
1864  }
1865  }
1866  catch ( ... )
1867  {
1868  TUFAIL("CommandOptionParser() threw an exception while parsing but"
1869  " should not have.");
1870  }
1871 
1872  defaultCommandOptionList.clear();
1873 
1874  try // Parse with a unsatisfied CommandOptionGroupOr
1875  {
1876  CommandOptionWithAnyArg cmdOptF('F', "foo", "Foo", false);
1877  CommandOptionWithAnyArg cmdOptB('B', "bar", "Bar", false);
1878  CommandOptionWithAnyArg cmdOptJ('J', "jig", "Jig", false);
1879  CommandOptionRest cmdOptRest("Rest", false);
1880  CommandOptionGroupOr cogo;
1881  cogo.addOption(&cmdOptF);
1882  cogo.addOption(&cmdOptB);
1883  CommandOptionDependent(&cogo, &cmdOptRest);
1884  CommandOptionParser cop("Description");
1885  int argc = 4;
1886  char* argv[] = { const_cast<char*>("program"),
1887  const_cast<char*>("-J"),
1888  const_cast<char*>("value1"),
1889  const_cast<char*>("trailing") };
1890  cop.parseOptions(argc, argv);
1891  TUPASS("CommandOptionParser parsed the options without throwing an"
1892  " exception.");
1893  TUASSERT(cop.hasErrors());
1894  }
1895  catch ( ... )
1896  {
1897  TUFAIL("CommandOptionParser() threw an exception while parsing but"
1898  " should not have.");
1899  }
1900 
1901  defaultCommandOptionList.clear();
1902 
1903  try // Parse with a satisfied CommandOptionGroupAnd
1904  {
1905  CommandOptionWithAnyArg cmdOptF('F', "foo", "Foo", false);
1906  CommandOptionWithAnyArg cmdOptB('B', "bar", "Bar", false);
1907  CommandOptionRest cmdOptRest("Rest", false);
1908  CommandOptionGroupAnd coga;
1909  coga.addOption(&cmdOptF);
1910  coga.addOption(&cmdOptB);
1911  CommandOptionDependent(&coga, &cmdOptRest);
1912  CommandOptionParser cop("Description");
1913  int argc = 6;
1914  char* argv[] = { const_cast<char*>("program"),
1915  const_cast<char*>("-F"),
1916  const_cast<char*>("value1"),
1917  const_cast<char*>("-B"),
1918  const_cast<char*>("value2"),
1919  const_cast<char*>("trailing") };
1920  cop.parseOptions(argc, argv);
1921  TUPASS("CommandOptionParser parsed the options without throwing an"
1922  " exception.");
1923  if (cop.hasErrors() )
1924  {
1925  std::ostringstream oss;
1926  oss << "CommandOptionParser encountered unexpected errors while"
1927  << " parsing: ";
1928  cop.dumpErrors(oss);
1929  TUFAIL(oss.str());
1930  }
1931  else
1932  {
1933  TUPASS("CommandOptionParser parsed without errors.");
1934  TUASSERTE(CommandOption*,&cmdOptF,coga.whichOne());
1935  }
1936  }
1937  catch ( ... )
1938  {
1939  TUFAIL("CommandOptionParser() threw an exception while parsing but"
1940  " should not have.");
1941  }
1942 
1943  defaultCommandOptionList.clear();
1944 
1945  try // Parse with a unsatisfied CommandOptionGroupAnd
1946  {
1947  CommandOptionWithAnyArg cmdOptF('F', "foo", "Foo", false);
1948  CommandOptionWithAnyArg cmdOptB('B', "bar", "Bar", false);
1949  CommandOptionRest cmdOptRest("Rest", false);
1950  CommandOptionGroupAnd coga;
1951  coga.addOption(&cmdOptF);
1952  coga.addOption(&cmdOptB);
1953  CommandOptionDependent(&coga, &cmdOptRest);
1954  CommandOptionParser cop("Description");
1955  int argc = 6;
1956  char* argv[] = { const_cast<char*>("program"),
1957  const_cast<char*>("-F"),
1958  const_cast<char*>("value1"),
1959  const_cast<char*>("--foo"),
1960  const_cast<char*>("value2"),
1961  const_cast<char*>("trailing") };
1962  cop.parseOptions(argc, argv);
1963  TUPASS("CommandOptionParser parsed the options without throwing an"
1964  " exception.");
1965  TUASSERT(cop.hasErrors());
1966  }
1967  catch ( ... )
1968  {
1969  TUFAIL("CommandOptionParser() threw an exception while parsing but"
1970  " should not have.");
1971  }
1972 
1973  defaultCommandOptionList.clear();
1974 #endif
1975 
1976  TURETURN();
1977 }
1978 
1979 
1980 void testNOfWhichRpt(unsigned expWhich, gnsstk::TestUtil& testFramework,
1981  unsigned argc, char *argv[])
1982 {
1983  try
1984  {
1985  defaultCommandOptionList.clear();
1986  CommandOptionWithAnyArg cmdOpt1('f', "foo", "Foo", false);
1987  CommandOptionWithAnyArg cmdOpt2('b', "bar", "Bar", false);
1988  CommandOptionWithAnyArg cmdOpt3('B', "baz", "Baz", false);
1989  CommandOptionNOf nof(2);
1990  CommandOptionParser cop("testNOfWhich");
1991  nof.addOption(&cmdOpt1);
1992  nof.addOption(&cmdOpt2);
1993  nof.addOption(&cmdOpt3);
1994  TUPASS("Constructed objects");
1995  cop.parseOptions(argc, argv);
1996  // based on the construction of nof and argv, only argc==5
1997  // can be valid
1998  if (argc == 5)
1999  {
2000  COPANOERR(cop);
2001  std::vector<CommandOption*> witches = nof.which();
2002  TUASSERTE(unsigned, expWhich, witches.size());
2003  }
2004  else
2005  {
2006  TUASSERT(cop.hasErrors());
2007  }
2008  }
2009  catch (...)
2010  {
2011  TUFAIL("Unexpected exception");
2012  }
2013 }
2014 
2015 
2017 {
2018  TUDEF("CommandOptionNOf", "which");
2019 
2020  // test a pair of different arguments
2021  char* argv1[] =
2022  { // argc (argv index+1)
2023  const_cast<char*>("program1"), // 1
2024  const_cast<char*>("-f"), // 2
2025  const_cast<char*>("wub1"), // 3
2026  const_cast<char*>("-b"), // 4
2027  const_cast<char*>("wub2"), // 5
2028  const_cast<char*>("-B"), // 6
2029  const_cast<char*>("wub3") // 7
2030  };
2031 
2032  // test a pair of identical arguments
2033  char* argv2[] =
2034  { // argc (argv index+1)
2035  const_cast<char*>("program2"), // 1
2036  const_cast<char*>("-f"), // 2
2037  const_cast<char*>("wub1"), // 3
2038  const_cast<char*>("-f"), // 4
2039  const_cast<char*>("wub2") // 5
2040  };
2041 
2042  for (unsigned argc = 1; argc <= 7; argc++)
2043  testNOfWhichRpt(2, testFramework, argc, argv1);
2044 
2045  testNOfWhichRpt(1, testFramework, 5, argv2);
2046 
2047  TURETURN();
2048 }
2049 
2050 
2055 int main(int argc, char *argv[])
2056 {
2057  int errorTotal = 0;
2058 
2059  CommandOptionParser_T testClass;
2060 
2061  errorTotal += testClass.testInitialization();
2062  errorTotal += testClass.testAddOption();
2063  errorTotal += testClass.testParseOptions();
2064  errorTotal += testClass.testOptionPresence();
2065  errorTotal += testClass.testNOfWhich();
2066 
2067  std::cout << "Total Failures for " << __FILE__ << ": " << errorTotal
2068  << std::endl;
2069 
2070  return( errorTotal );
2071 
2072 } // main()
gnsstk::CommandOptionParser::addOption
CommandOptionParser & addOption(gnsstk::CommandOption &co)
Definition: CommandOptionParser.cpp:80
YDSTime.hpp
CommandOptionParser_T::testInitialization
int testInitialization()
Definition: CommandOptionParser_T.cpp:76
gnsstk::defaultCommandOptionList
CommandOptionVec defaultCommandOptionList
Definition: CommandOption.cpp:55
gnsstk::CommandOption::getOrder
unsigned long getOrder(unsigned long idx=-1) const
Definition: CommandOption.cpp:158
gnsstk::CommandOptionWithStringArg
Definition: CommandOption.hpp:365
gnsstk::CommandOptionOneOf::addOption
void addOption(CommandOption *opt)
Add an option to the list of mutually exclusive options.
Definition: CommandOption.cpp:396
gnsstk::YDSTime
Definition: YDSTime.hpp:58
gnsstk::CommandOptionAllOf
Definition: CommandOption.hpp:575
COPANOERR
#define COPANOERR(COP)
Definition: CommandOptionParser_T.cpp:50
TUASSERTE
#define TUASSERTE(TYPE, EXP, GOT)
Definition: TestUtil.hpp:81
gnsstk::CommandOptionNOf
Definition: CommandOption.hpp:501
CommandOptionParser_T
Definition: CommandOptionParser_T.cpp:58
TUFAIL
#define TUFAIL(MSG)
Definition: TestUtil.hpp:228
gnsstk::CommandOptionRest
CommandOption to take the rest of the command line.
Definition: CommandOption.hpp:457
gnsstk::CommandOptionMutex
Definition: CommandOption.hpp:607
gnsstk::CommandOption::getCount
virtual unsigned long getCount() const
Definition: CommandOption.hpp:188
gnsstk::CommandOptionParser::dumpErrors
std::ostream & dumpErrors(std::ostream &out)
Writes the errors to out.
Definition: CommandOptionParser.cpp:346
CommandOptionParser.hpp
gnsstk::CommandOptionWithAnyArg
Definition: CommandOption.hpp:342
gnsstk::CommandOptionNoArg
Definition: CommandOption.hpp:295
gnsstk::CommandOptionDependent
Definition: CommandOption.hpp:641
gnsstk::CommandOptionOneOf::whichOne
CommandOption * whichOne() const
Definition: CommandOption.cpp:428
testNOfWhichRpt
void testNOfWhichRpt(unsigned expWhich, gnsstk::TestUtil &testFramework, unsigned argc, char *argv[])
Definition: CommandOptionParser_T.cpp:1980
gnsstk::CommandOptionNOf::which
std::vector< CommandOption * > which() const
Definition: CommandOption.cpp:381
gnsstk
For Sinex::InputHistory.
Definition: BasicFramework.cpp:50
gnsstk::CommandOptionGroupAnd
Definition: CommandOption.hpp:710
main
int main(int argc, char *argv[])
Definition: CommandOptionParser_T.cpp:2055
TUASSERT
#define TUASSERT(EXPR)
Definition: TestUtil.hpp:63
TestUtil.hpp
gnsstk::CommandOptionVec
std::vector< CommandOption * > CommandOptionVec
Definition: CommandOption.hpp:66
TURETURN
#define TURETURN()
Definition: TestUtil.hpp:232
CommandOptionParser_T::testAddOption
int testAddOption()
Definition: CommandOptionParser_T.cpp:191
TUPASS
#define TUPASS(MSG)
Definition: TestUtil.hpp:230
CommandOptionParser_T::~CommandOptionParser_T
~CommandOptionParser_T()
Definition: CommandOptionParser_T.cpp:62
gnsstk::CommandOptionNOf::addOption
void addOption(CommandOption *opt)
Add an option to the list of mutually exclusive options.
Definition: CommandOption.cpp:343
gnsstk::CommonTime
Definition: CommonTime.hpp:84
gnsstk::CommandOption
Definition: CommandOption.hpp:115
gnsstk::CommandOptionWithArg
Definition: CommandOption.hpp:319
CommandOptionParser_T::testOptionPresence
int testOptionPresence()
Definition: CommandOptionParser_T.cpp:1124
gnsstk::CommandOptionWithNumberArg
Definition: CommandOption.hpp:394
gnsstk::CommandOptionGroupOr
Definition: CommandOption.hpp:678
TUDEF
#define TUDEF(CLASS, METHOD)
Definition: TestUtil.hpp:56
CommandOptionWithCommonTimeArg.hpp
gnsstk::YDSTime::convertToCommonTime
virtual CommonTime convertToCommonTime() const
Definition: YDSTime.cpp:61
CommandOptionParser_T::testParseOptions
int testParseOptions()
Definition: CommandOptionParser_T.cpp:277
gnsstk::CommandOptionParser::parseOptions
void parseOptions(int argc, char *argv[])
Parses the command line.
Definition: CommandOptionParser.cpp:115
std
Definition: Angle.hpp:142
gnsstk::CommandOptionWithCommonTimeArg::getTime
const std::vector< CommonTime > & getTime() const
Return the times scanned in from the command line.
Definition: CommandOptionWithCommonTimeArg.hpp:96
gnsstk::CommandOption::getValue
const std::vector< std::string > & getValue() const
Definition: CommandOption.hpp:194
gnsstk::CommandOptionWithDecimalArg
Definition: CommandOption.hpp:423
gnsstk::CommandOptionParser
Definition: CommandOptionParser.hpp:86
gnsstk::CommandOptionWithCommonTimeArg
Definition: CommandOptionWithCommonTimeArg.hpp:62
gnsstk::TestUtil
Definition: TestUtil.hpp:265
gnsstk::CommandOptionOneOf
Definition: CommandOption.hpp:540
CommandOptionParser_T::CommandOptionParser_T
CommandOptionParser_T()
Definition: CommandOptionParser_T.cpp:61
CommandOptionParser_T::testNOfWhich
int testNOfWhich()
Definition: CommandOptionParser_T.cpp:2016
gnsstk::CommandOptionParser::hasErrors
bool hasErrors()
Returns true if any processing errors occurred.
Definition: CommandOptionParser.hpp:114
gnsstk::CommandOption::setMaxCount
CommandOption & setMaxCount(const unsigned long l)
Definition: CommandOption.hpp:164


gnsstk
Author(s):
autogenerated on Wed Oct 25 2023 02:40:38