CommandOption_example_3.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 //
28 // This software was developed by Applied Research Laboratories at the
29 // University of Texas at Austin, under contract to an agency or agencies
30 // within the U.S. Department of Defense. The U.S. Government retains all
31 // rights to use, duplicate, distribute, disclose, or release this software.
32 //
33 // Pursuant to DoD Directive 523024
34 //
35 // DISTRIBUTION STATEMENT A: This software has been approved for public
36 // release, distribution is unlimited.
37 //
38 //==============================================================================
39 #include <iostream>
40 #include <fstream>
41 #include <BasicFramework.hpp>
42 #include <StringUtils.hpp>
44 #include <TimeString.hpp>
45 
46 using namespace std;
47 using namespace gnsstk;
48 
49 // Given that Doxygen removes Doxygen comments when rendering
50 // examples, please do not consider the absence of comments in the
51 // HTML rendering to be representative. Refer to the file in the
52 // depot instead.
53 
54 // Interesting examples:
55 // CommandOption3 -f "1920 12345" -f "1921 0" --scream
56 // CommandOption3 -f "1920 12345" -Z 1105530967 --scream
57 // CommandOption3 -Z 1105530967 -f "1920 12345" --scream
58 
61 {
62 public:
64  CommandOption3Example(const string& applName);
66  bool initialize(int argc, char *argv[], bool pretty = true) noexcept override;
68  void process() override;
70  void shutDown() override;
83 
90 };
91 
92 
94 CommandOption3Example(const string& applName)
95  : BasicFramework(applName, "Example application for CommandOption"),
96  gpsEWSOpt('o', "ews", "%E %G %g",
97  "\"GPSEpoch 10bitGPSweek SecondOfWeek\""),
98  gpsWSOpt('f', "ws", "%F %g", "\"FullGPSWeek SecondOfWeek\""),
99  gpsWZOpt('w', "wz", "%F %Z", "\"FullGPSWeek Zcount\""),
100  gpsZ29Opt(0, "z29", "%E %c", "\"29bitZcount\""),
101  gpsZ32Opt('Z', "z32", "%C", "\"32bitZcount\""),
102  screamOpt(0, "scream", "print a message very loudly"),
103  hushOpt(0, "hush", "don't print a message very loudly"),
104  allTimesOption(2) // two and only two of the options must be used
105 {
106  // Define the options that will be used. As noted above, 2 of
107  // these options must be used on the command-line.
113  // At least one of these options must be specified, but there is
114  // no restriction on whether more than one is specified
115  // (setMaxCount has no effect on CommandOptionOneOf).
118 }
119 
120 
122 initialize(int argc, char *argv[], bool pretty) noexcept
123 {
124  if (!BasicFramework::initialize(argc, argv, pretty))
125  return false;
126  // which() constructs a new vector and returns it, so we call
127  // which() once and store the data for local use.
128  CommandOptionVec timeOpts = allTimesOption.which();
129  // timeOpts now contains a vector of all the command-line, so we
130  // can iterate through it to figure out what options were used.
131  for (unsigned i = 0; i < timeOpts.size(); i++)
132  {
133  // In this example, the command-line options can be used
134  // twice, so we iterate through the values stored in each
135  // option.
136  // If you want to restrict the command-line so that only a
137  // certain number of a given option used in the
138  // CommandOptionNOf validation, e.g. only specify GPS
139  // week-second once in this example, then use the
140  // setMaxCount() method on the individual options.
141  for (unsigned j = 0; j < timeOpts[i]->getCount(); j++)
142  {
144  (CommandOptionWithCommonTimeArg*)timeOpts[i];
145  // Because the values are stored with each command-line
146  // option, we use getOrder to determine the order in which
147  // the user specified the options on the command-line.
148  // This isn't always necessary but the information is here
149  // if needed.
150  unsigned order = timeOpts[i]->getOrder(j);
151  cout << "Option #" << order << " " << opt->getValue()[j]
152  << " = "
153  << printTime(opt->getTime()[j], "%04Y/%02m/%02d %02H:%02M:%02S")
154  << endl;
155  }
156  }
157  if (screamOpt)
158  {
159  cout << "HELLO WORLD x" << screamOpt.getCount() << endl;
160  }
161  if (hushOpt)
162  {
163  cout << "ok i'll be quiet x" << hushOpt.getCount() << endl;
164  }
165  // whichOne returns the option that was used, which is a little
166  // strange given multiple can be used (CommandOptionMutex is
167  // better for a "one and only one" relation).
168  CommandOption *which = hushScreamOpt.whichOne();
169  if (which != nullptr)
170  {
171  cout << "You used " << which->getFullOptionString()
172  << " (and possibly others)" << endl;
173  }
174  return true;
175 }
176 
177 
180 {
181  cout << "Nothing to do" << endl;
182 }
183 
184 
187 {
188  cout << "Shutting down" << endl;
189 }
190 
191 
192 int main(int argc, char *argv[])
193 {
194  try
195  {
196  CommandOption3Example app(argv[0]);
197  if (app.initialize(argc, argv))
198  {
199  app.run();
200  }
201  return app.exitCode;
202  }
203  catch (gnsstk::Exception& e)
204  {
205  cerr << e << endl;
206  }
207  catch (std::exception& e)
208  {
209  cerr << e.what() << endl;
210  }
211  catch (...)
212  {
213  cerr << "Caught unknown exception" << endl;
214  }
216 }
gnsstk::CommandOption::getOrder
unsigned long getOrder(unsigned long idx=-1) const
Definition: CommandOption.cpp:158
StringUtils.hpp
gnsstk::CommandOptionOneOf::addOption
void addOption(CommandOption *opt)
Add an option to the list of mutually exclusive options.
Definition: CommandOption.cpp:396
gnsstk::CommandOptionNOf
Definition: CommandOption.hpp:501
CommandOption3Example::allTimesOption
CommandOptionNOf allTimesOption
Make sure that two of the time options have been specified.
Definition: CommandOption_example_3.cpp:82
gnsstk::Exception::what
std::string what() const
Dump to a string.
Definition: Exception.cpp:193
CommandOption3Example
Example of using CommandOptionNOf in an application.
Definition: CommandOption_example_3.cpp:60
gnsstk::BasicFramework::EXCEPTION_ERROR
static const int EXCEPTION_ERROR
Definition: BasicFramework.hpp:392
gnsstk::CommandOptionNoArg
Definition: CommandOption.hpp:295
CommandOption3Example::gpsEWSOpt
CommandOptionWithCommonTimeArg gpsEWSOpt
Express time as GPS epoch-week-second.
Definition: CommandOption_example_3.cpp:72
gnsstk
For Sinex::InputHistory.
Definition: BasicFramework.cpp:50
CommandOption3Example::gpsZ32Opt
CommandOptionWithCommonTimeArg gpsZ32Opt
Express time as GPS 32-bit full zcount.
Definition: CommandOption_example_3.cpp:80
gnsstk::Exception
Definition: Exception.hpp:151
CommandOption3Example::process
void process() override
Do the processing.
Definition: CommandOption_example_3.cpp:179
initialize
int initialize(string &errors)
Definition: RinEdit.cpp:513
CommandOption3Example::hushOpt
CommandOptionNoArg hushOpt
Option for demonstrating CommandOptionOneOf.
Definition: CommandOption_example_3.cpp:87
gnsstk::BasicFramework
Definition: BasicFramework.hpp:387
CommandOption3Example::shutDown
void shutDown() override
Clean up.
Definition: CommandOption_example_3.cpp:186
gnsstk::CommandOptionVec
std::vector< CommandOption * > CommandOptionVec
Definition: CommandOption.hpp:66
CommandOption3Example::hushScreamOpt
CommandOptionOneOf hushScreamOpt
Make sure at least one of hushOpt or screamOpt are used.
Definition: CommandOption_example_3.cpp:89
main
int main(int argc, char *argv[])
Definition: CommandOption_example_3.cpp:192
gnsstk::CommandOptionNOf::addOption
void addOption(CommandOption *opt)
Add an option to the list of mutually exclusive options.
Definition: CommandOption.cpp:343
gnsstk::CommandOption
Definition: CommandOption.hpp:115
CommandOption3Example::CommandOption3Example
CommandOption3Example(const string &applName)
Initialize command-line arguments.
Definition: CommandOption_example_3.cpp:94
gnsstk::BasicFramework::run
bool run() noexcept
Definition: BasicFramework.cpp:126
CommandOption3Example::gpsWSOpt
CommandOptionWithCommonTimeArg gpsWSOpt
Express time as GPS fullweek-second.
Definition: CommandOption_example_3.cpp:74
CommandOptionWithCommonTimeArg.hpp
gnsstk::printTime
std::string printTime(const CommonTime &t, const std::string &fmt)
Definition: TimeString.cpp:64
gnsstk::CommandOption::getFullOptionString
std::string getFullOptionString() const
Definition: CommandOption.cpp:115
BasicFramework.hpp
std
Definition: Angle.hpp:142
gnsstk::BasicFramework::exitCode
int exitCode
Definition: BasicFramework.hpp:450
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::CommandOptionWithCommonTimeArg
Definition: CommandOptionWithCommonTimeArg.hpp:62
CommandOption3Example::screamOpt
CommandOptionNoArg screamOpt
Option for demonstrating CommandOptionOneOf.
Definition: CommandOption_example_3.cpp:85
CommandOption3Example::gpsZ29Opt
CommandOptionWithCommonTimeArg gpsZ29Opt
Express time as GPS 29-bit full zcount.
Definition: CommandOption_example_3.cpp:78
CommandOption3Example::gpsWZOpt
CommandOptionWithCommonTimeArg gpsWZOpt
Express time as GPS fullweek-zcount.
Definition: CommandOption_example_3.cpp:76
gnsstk::CommandOptionOneOf
Definition: CommandOption.hpp:540
TimeString.hpp
CommandOption3Example::initialize
bool initialize(int argc, char *argv[], bool pretty=true) noexcept override
Process command-line arguments.
Definition: CommandOption_example_3.cpp:122


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