CommandOption_example_1.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 
43 using namespace std;
44 using namespace gnsstk;
45 
46 // Given that Doxygen removes Doxygen comments when rendering
47 // examples, please do not consider the absence of comments in the
48 // HTML rendering to be representative. Refer to the file in the
49 // depot instead.
50 
51 // Interesting examples:
52 // CommandOption1 -f CPackConfig.cmake --scream foo bar baz
53 
56 {
57 public:
59  CommandOption1Example(const string& applName);
61  bool initialize(int argc, char *argv[], bool pretty = true) noexcept override;
63  void process() override;
65  void shutDown() override;
72 };
73 
74 
76 CommandOption1Example(const string& applName)
77  : BasicFramework(applName, "Example application for CommandOption"),
78  fileOpt('f', "file", "input file", true),
79  screamOpt(0, "scream", "print a message very loudly"),
80  restOpt("FILE [...]", true)
81 {
83 }
84 
85 
87 initialize(int argc, char *argv[], bool pretty) noexcept
88 {
89  if (!BasicFramework::initialize(argc, argv, pretty))
90  return false;
91  // fileOpt is required and thus no checking need be done. We
92  // can just get the first argument. If there was no argument or
93  // too many instances of fileOpt specified, the above check will
94  // have returned false already.
95  ifstream fs(fileOpt.getValue()[0].c_str());
96  if (!fs)
97  {
98  // print an error message for the intended file
99  std::perror(fileOpt.getValue()[0].c_str());
100  // pretty safe to assume it's that the file doesn't exist
101  exitCode = BasicFramework::EXIST_ERROR;
102  // We're done. Stop the program.
103  return false;
104  }
105  if (screamOpt)
106  cout << "HELLO WORLD" << endl;
107  const vector<string>& restVec = restOpt.getValue();
108  cout << "Remaining values:" << endl;
109  for (unsigned i = 0; i < restVec.size(); i++)
110  {
111  cout << " #" << i << " = " << restVec[i] << endl;
112  }
113  return true;
114 }
115 
116 
119 {
120  cout << "Nothing to process" << endl;
121 }
122 
123 
126 {
127  cout << "Shutting down" << endl;
128 }
129 
130 
131 int main(int argc, char *argv[])
132 {
133  try
134  {
135  CommandOption1Example app(argv[0]);
136  if (app.initialize(argc, argv))
137  {
138  app.run();
139  }
140  return app.exitCode;
141  }
142  catch (gnsstk::Exception& e)
143  {
144  cerr << e << endl;
145  }
146  catch (std::exception& e)
147  {
148  cerr << e.what() << endl;
149  }
150  catch (...)
151  {
152  cerr << "Caught unknown exception" << endl;
153  }
155 }
CommandOption1Example::initialize
bool initialize(int argc, char *argv[], bool pretty=true) noexcept override
Process command-line arguments.
Definition: CommandOption_example_1.cpp:87
CommandOption1Example
Class to show how to add a simple command-line argument to an application.
Definition: CommandOption_example_1.cpp:55
CommandOption1Example::screamOpt
CommandOptionNoArg screamOpt
Another example.
Definition: CommandOption_example_1.cpp:69
gnsstk::CommandOptionRest
CommandOption to take the rest of the command line.
Definition: CommandOption.hpp:457
CommandOption1Example::restOpt
CommandOptionRest restOpt
Everything else after the above command-line options.
Definition: CommandOption_example_1.cpp:71
gnsstk::Exception::what
std::string what() const
Dump to a string.
Definition: Exception.cpp:193
CommandOption1Example::fileOpt
CommandOptionWithAnyArg fileOpt
Command-line option example.
Definition: CommandOption_example_1.cpp:67
CommandOption1Example::CommandOption1Example
CommandOption1Example(const string &applName)
Initialize command-line arguments.
Definition: CommandOption_example_1.cpp:76
gnsstk::CommandOptionWithAnyArg
Definition: CommandOption.hpp:342
gnsstk::BasicFramework::EXCEPTION_ERROR
static const int EXCEPTION_ERROR
Definition: BasicFramework.hpp:392
gnsstk::CommandOptionNoArg
Definition: CommandOption.hpp:295
gnsstk
For Sinex::InputHistory.
Definition: BasicFramework.cpp:50
main
int main(int argc, char *argv[])
Definition: CommandOption_example_1.cpp:131
gnsstk::Exception
Definition: Exception.hpp:151
initialize
int initialize(string &errors)
Definition: RinEdit.cpp:513
CommandOption1Example::shutDown
void shutDown() override
Clean up.
Definition: CommandOption_example_1.cpp:125
gnsstk::BasicFramework
Definition: BasicFramework.hpp:387
gnsstk::BasicFramework::run
bool run() noexcept
Definition: BasicFramework.cpp:126
BasicFramework.hpp
std
Definition: Angle.hpp:142
gnsstk::BasicFramework::exitCode
int exitCode
Definition: BasicFramework.hpp:450
gnsstk::CommandOption::setMaxCount
CommandOption & setMaxCount(const unsigned long l)
Definition: CommandOption.hpp:164
CommandOption1Example::process
void process() override
Do the processing.
Definition: CommandOption_example_1.cpp:118


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