CommandOption_example_4.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 // CommandOption4
56 // CommandOption4 --scream
57 // CommandOption4 --scream --hush
58 // CommandOption4 -x
59 // CommandOption4 -x -y -z
60 // CommandOption4 --hush -xyz
61 
64 {
65 public:
67  CommandOption4Example(const string& applName);
69  bool initialize(int argc, char *argv[], bool pretty = true) noexcept override;
71  void process() override;
73  void shutDown() override;
82 
91 };
92 
93 
95 CommandOption4Example(const string& applName)
96  : BasicFramework(applName, "Example application for CommandOption"),
97  xOpt('x', "", "you say you want an x"),
98  yOpt('y', "", "you say you want an y"),
99  zOpt('z', "", "you say you want an z"),
100  screamOpt(0, "scream", "print a message very loudly"),
101  hushOpt(0, "hush", "don't print a message very loudly"),
102  hushAndXYZOpt(&hushOpt, &xyzOpts)
103 {
104  // Define the options that must be used together.
108  // Only one of these options may be specified.
111 }
112 
113 
115 initialize(int argc, char *argv[], bool pretty) noexcept
116 {
117  if (!BasicFramework::initialize(argc, argv, pretty))
118  return false;
119  if (screamOpt)
120  {
121  cout << "HELLO WORLD x" << screamOpt.getCount() << endl;
122  }
123  if (hushOpt)
124  {
125  cout << "ok i'll be quiet x" << hushOpt.getCount() << endl;
126  }
127  // whichOne returns the option that was used
128  CommandOption *which = hushOrScreamOpt.whichOne();
129  if (which != nullptr)
130  {
131  cout << "You used " << which->getFullOptionString() << endl;
132  }
133  // do some processing of x y and z options.
134  // ...
135  return true;
136 }
137 
138 
141 {
142  cout << "Nothing to do" << endl;
143 }
144 
145 
148 {
149  cout << "Shutting down" << endl;
150 }
151 
152 
153 int main(int argc, char *argv[])
154 {
155  try
156  {
157  CommandOption4Example app(argv[0]);
158  if (app.initialize(argc, argv))
159  {
160  app.run();
161  }
162  return app.exitCode;
163  }
164  catch (gnsstk::Exception& e)
165  {
166  cerr << e << endl;
167  }
168  catch (std::exception& e)
169  {
170  cerr << e.what() << endl;
171  }
172  catch (...)
173  {
174  cerr << "Caught unknown exception" << endl;
175  }
177 }
CommandOption4Example::yOpt
CommandOptionNoArg yOpt
generic option y
Definition: CommandOption_example_4.cpp:77
CommandOption4Example::zOpt
CommandOptionNoArg zOpt
generic option z
Definition: CommandOption_example_4.cpp:79
CommandOption4Example::process
void process() override
Do the processing.
Definition: CommandOption_example_4.cpp:140
CommandOption4Example::xyzOpts
CommandOptionAllOf xyzOpts
Make sure that x y and z are specified together.
Definition: CommandOption_example_4.cpp:81
StringUtils.hpp
gnsstk::CommandOptionOneOf::addOption
void addOption(CommandOption *opt)
Add an option to the list of mutually exclusive options.
Definition: CommandOption.cpp:396
gnsstk::CommandOptionAllOf
Definition: CommandOption.hpp:575
CommandOption4Example
Example of using CommandOptionNOf in an application.
Definition: CommandOption_example_4.cpp:63
CommandOption4Example::hushAndXYZOpt
CommandOptionDependent hushAndXYZOpt
Make sure that if xyz options are used, hush is specified.
Definition: CommandOption_example_4.cpp:90
CommandOption4Example::shutDown
void shutDown() override
Clean up.
Definition: CommandOption_example_4.cpp:147
gnsstk::Exception::what
std::string what() const
Dump to a string.
Definition: Exception.cpp:193
gnsstk::CommandOptionMutex
Definition: CommandOption.hpp:607
gnsstk::BasicFramework::EXCEPTION_ERROR
static const int EXCEPTION_ERROR
Definition: BasicFramework.hpp:392
gnsstk::CommandOptionNoArg
Definition: CommandOption.hpp:295
gnsstk::CommandOptionDependent
Definition: CommandOption.hpp:641
gnsstk
For Sinex::InputHistory.
Definition: BasicFramework.cpp:50
CommandOption4Example::CommandOption4Example
CommandOption4Example(const string &applName)
Initialize command-line arguments.
Definition: CommandOption_example_4.cpp:95
gnsstk::Exception
Definition: Exception.hpp:151
initialize
int initialize(string &errors)
Definition: RinEdit.cpp:513
gnsstk::BasicFramework
Definition: BasicFramework.hpp:387
CommandOption4Example::initialize
bool initialize(int argc, char *argv[], bool pretty=true) noexcept override
Process command-line arguments.
Definition: CommandOption_example_4.cpp:115
gnsstk::CommandOption
Definition: CommandOption.hpp:115
main
int main(int argc, char *argv[])
Definition: CommandOption_example_4.cpp:153
CommandOption4Example::xOpt
CommandOptionNoArg xOpt
generic option x
Definition: CommandOption_example_4.cpp:75
gnsstk::BasicFramework::run
bool run() noexcept
Definition: BasicFramework.cpp:126
CommandOptionWithCommonTimeArg.hpp
CommandOption4Example::hushOrScreamOpt
CommandOptionMutex hushOrScreamOpt
Make sure only one of hushOpt or screamOpt are used.
Definition: CommandOption_example_4.cpp:88
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
CommandOption4Example::screamOpt
CommandOptionNoArg screamOpt
Option for demonstrating CommandOptionOneOf.
Definition: CommandOption_example_4.cpp:84
CommandOption4Example::hushOpt
CommandOptionNoArg hushOpt
Option for demonstrating CommandOptionOneOf.
Definition: CommandOption_example_4.cpp:86
TimeString.hpp


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