CommandOption_example_2.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 // CommandOption2 -n -1 -D 1.2 -D 3.4 -t "2020/4/5 1:2:3"
56 
60 {
61 public:
63  CommandOption2Example(const string& applName);
65  bool initialize(int argc, char *argv[], bool pretty = true) noexcept override;
67  void process() override;
69  void shutDown() override;
77  int num;
79  std::vector<double> dub;
82 };
83 
84 
86 CommandOption2Example(const string& applName)
87  : BasicFramework(applName, "Example application for CommandOption"),
88  numOpt('n', "num", "your choice of integer"),
89  floatOpt('D', "data", "specify any number of decimal numbers"),
90  timeOpt('t', "time", "%Y/%m/%d %H:%M:%S",
91  "specify a time (%Y/%m/%d %H:%M:%S)", true),
92  num(0)
93 {
96 }
97 
98 
100 initialize(int argc, char *argv[], bool pretty) noexcept
101 {
102  if (!BasicFramework::initialize(argc, argv, pretty))
103  return false;
104  // numOpt is not required and so check if it was actually specified.
105  if (numOpt.getCount() > 0)
106  {
107  // convert the string value to integer
108  num = gnsstk::StringUtils::asInt(numOpt.getValue()[0]);
109  }
110  // floatOpt is also not required
111  if (floatOpt.getCount() > 0)
112  {
113  const std::vector<std::string>& vals(floatOpt.getValue());
114  // make our target storage the same size as the source so we
115  // don't have to reallocate over and over.
116  dub.resize(vals.size(), 0.);
117  for (unsigned i = 0; i < vals.size(); i++)
118  {
119  dub[i] = gnsstk::StringUtils::asDouble(vals[i]);
120  }
121  }
122  // timeOpt is required, and the getTime method returns a CommonTime object
123  beginTime = timeOpt.getTime()[0];
124 
125  return true;
126 }
127 
128 
131 {
132  cout << "Your number is " << num << endl;
133  cout << "Data:" << endl;
134  for (unsigned i = 0; i < dub.size(); i++)
135  {
136  cout << " dub[" << i << "] = " << dub[i] << endl;
137  }
138  cout << "Requested time:"
139  << printTime(beginTime, "%Y/%02m/%02d %02H:%02M:%02S")
140  << endl;
141 }
142 
143 
146 {
147  cout << "Shutting down" << endl;
148 }
149 
150 
151 int main(int argc, char *argv[])
152 {
153  try
154  {
155  CommandOption2Example app(argv[0]);
156  if (app.initialize(argc, argv))
157  {
158  app.run();
159  }
160  return app.exitCode;
161  }
162  catch (gnsstk::Exception& e)
163  {
164  cerr << e << endl;
165  }
166  catch (std::exception& e)
167  {
168  cerr << e.what() << endl;
169  }
170  catch (...)
171  {
172  cerr << "Caught unknown exception" << endl;
173  }
175 }
CommandOption2Example::timeOpt
CommandOptionWithCommonTimeArg timeOpt
Command-line option example for time.
Definition: CommandOption_example_2.cpp:75
gnsstk::StringUtils::asInt
long asInt(const std::string &s)
Definition: StringUtils.hpp:713
CommandOption2Example::shutDown
void shutDown() override
Clean up.
Definition: CommandOption_example_2.cpp:145
CommandOption2Example::initialize
bool initialize(int argc, char *argv[], bool pretty=true) noexcept override
Process command-line arguments.
Definition: CommandOption_example_2.cpp:100
StringUtils.hpp
main
int main(int argc, char *argv[])
Definition: CommandOption_example_2.cpp:151
gnsstk::Exception::what
std::string what() const
Dump to a string.
Definition: Exception.cpp:193
gnsstk::BasicFramework::EXCEPTION_ERROR
static const int EXCEPTION_ERROR
Definition: BasicFramework.hpp:392
CommandOption2Example::floatOpt
CommandOptionWithDecimalArg floatOpt
Command-line option example with float checks.
Definition: CommandOption_example_2.cpp:73
gnsstk
For Sinex::InputHistory.
Definition: BasicFramework.cpp:50
gnsstk::Exception
Definition: Exception.hpp:151
initialize
int initialize(string &errors)
Definition: RinEdit.cpp:513
gnsstk::BasicFramework
Definition: BasicFramework.hpp:387
CommandOption2Example::dub
std::vector< double > dub
the decimal choices
Definition: CommandOption_example_2.cpp:79
gnsstk::CommonTime
Definition: CommonTime.hpp:84
CommandOption2Example::process
void process() override
Do the processing.
Definition: CommandOption_example_2.cpp:130
gnsstk::CommandOptionWithNumberArg
Definition: CommandOption.hpp:394
CommandOption2Example::num
int num
storage for integer
Definition: CommandOption_example_2.cpp:77
gnsstk::StringUtils::asDouble
double asDouble(const std::string &s)
Definition: StringUtils.hpp:705
gnsstk::BasicFramework::run
bool run() noexcept
Definition: BasicFramework.cpp:126
CommandOptionWithCommonTimeArg.hpp
CommandOption2Example::CommandOption2Example
CommandOption2Example(const string &applName)
Initialize command-line arguments.
Definition: CommandOption_example_2.cpp:86
CommandOption2Example
Definition: CommandOption_example_2.cpp:59
gnsstk::printTime
std::string printTime(const CommonTime &t, const std::string &fmt)
Definition: TimeString.cpp:64
BasicFramework.hpp
std
Definition: Angle.hpp:142
gnsstk::BasicFramework::exitCode
int exitCode
Definition: BasicFramework.hpp:450
gnsstk::CommandOptionWithDecimalArg
Definition: CommandOption.hpp:423
gnsstk::CommandOptionWithCommonTimeArg
Definition: CommandOptionWithCommonTimeArg.hpp:62
CommandOption2Example::beginTime
gnsstk::CommonTime beginTime
requested start time
Definition: CommandOption_example_2.cpp:81
TimeString.hpp
gnsstk::CommandOption::setMaxCount
CommandOption & setMaxCount(const unsigned long l)
Definition: CommandOption.hpp:164
CommandOption2Example::numOpt
CommandOptionWithNumberArg numOpt
Command-line option example with integer checks.
Definition: CommandOption_example_2.cpp:71


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