1 #include <iostream>
2 #include <fstream>
3 #include <BasicFramework.hpp>
4 #include <StringUtils.hpp>
5 #include <CommandOptionWithCommonTimeArg.hpp>
6 #include <TimeString.hpp>
7 
8 using namespace std;
9 using namespace gpstk;
10 
11 // Given that Doxygen removes Doxygen comments when rendering
12 // examples, please do not consider the absence of comments in the
13 // HTML rendering to be representative.  Refer to the file in the
14 // depot instead.
15 
16 // Interesting examples:
17 // CommandOption4
18 // CommandOption4 --scream
19 // CommandOption4 --scream --hush
20 // CommandOption4 -x
21 // CommandOption4 -x -y -z
22 // CommandOption4 --hush -xyz
23 
24 /// Example of using CommandOptionNOf in an application.
25 class CommandOption4Example : public BasicFramework
26 {
27 public:
28       /// Initialize command-line arguments
29    CommandOption4Example(const string& applName);
30       /// Process command-line arguments
31    bool initialize(int argc, char *argv[], bool pretty = true) throw() override;
32       /// Do the processing.
33    void process() override;
34       /// Clean up.
35    void shutDown() override;
36       /// generic option x
37    CommandOptionNoArg xOpt;
38       /// generic option y
39    CommandOptionNoArg yOpt;
40       /// generic option z
41    CommandOptionNoArg zOpt;
42       /// Make sure that x y and z are specified together.
43    CommandOptionAllOf xyzOpts;
44 
45       /// Option for demonstrating CommandOptionOneOf
46    CommandOptionNoArg screamOpt;
47       /// Option for demonstrating CommandOptionOneOf
48    CommandOptionNoArg hushOpt;
49       /// Make sure only one of hushOpt or screamOpt are used
50    CommandOptionMutex hushOrScreamOpt;
51       /// Make sure that if xyz options are used, hush is specified
52    CommandOptionDependent hushAndXYZOpt;
53 };
54 
55 
56 CommandOption4Example ::
CommandOption4Example(const string & applName)57 CommandOption4Example(const string& applName)
58       : BasicFramework(applName, "Example application for CommandOption"),
59         xOpt('x', "", "you say you want an x"),
60         yOpt('y', "", "you say you want an y"),
61         zOpt('z', "", "you say you want an z"),
62         screamOpt(0, "scream", "print a message very loudly"),
63         hushOpt(0, "hush", "don't print a message very loudly"),
64         hushAndXYZOpt(&hushOpt, &xyzOpts)
65 {
66       // Define the options that must be used together.
67    xyzOpts.addOption(&xOpt);
68    xyzOpts.addOption(&yOpt);
69    xyzOpts.addOption(&zOpt);
70       // Only one of these options may be specified.
71    hushOrScreamOpt.addOption(&screamOpt);
72    hushOrScreamOpt.addOption(&hushOpt);
73 }
74 
75 
76 bool CommandOption4Example ::
initialize(int argc,char * argv[],bool pretty)77 initialize(int argc, char *argv[], bool pretty) throw()
78 {
79    if (!BasicFramework::initialize(argc, argv, pretty))
80       return false;
81    if (screamOpt)
82    {
83       cout << "HELLO WORLD x" << screamOpt.getCount() << endl;
84    }
85    if (hushOpt)
86    {
87       cout << "ok i'll be quiet x" << hushOpt.getCount() << endl;
88    }
89       // whichOne returns the option that was used
90    CommandOption *which = hushOrScreamOpt.whichOne();
91    if (which != nullptr)
92    {
93       cout << "You used " << which->getFullOptionString() << endl;
94    }
95       // do some processing of x y and z options.
96       // ...
97    return true;
98 }
99 
100 
101 void CommandOption4Example ::
process()102 process()
103 {
104    cout << "Nothing to do" << endl;
105 }
106 
107 
108 void CommandOption4Example ::
shutDown()109 shutDown()
110 {
111    cout << "Shutting down" << endl;
112 }
113 
114 
main(int argc,char * argv[])115 int main(int argc, char *argv[])
116 {
117    try
118    {
119       CommandOption4Example app(argv[0]);
120       if (app.initialize(argc, argv))
121       {
122          app.run();
123       }
124       return app.exitCode;
125    }
126    catch (gpstk::Exception& e)
127    {
128       cerr << e << endl;
129    }
130    catch (std::exception& e)
131    {
132       cerr << e.what() << endl;
133    }
134    catch (...)
135    {
136       cerr << "Caught unknown exception" << endl;
137    }
138    return gpstk::BasicFramework::EXCEPTION_ERROR;
139 }
140