FastJet 3.4.1
CmdLine.cc
1///////////////////////////////////////////////////////////////////////////////
2// File: CmdLine.cc //
3// Part of the CmdLine library //
4// //
5// Copyright (c) 2007 Gavin Salam //
6// //
7// This program is free software; you can redistribute it and/or modify //
8// it under the terms of the GNU General Public License as published by //
9// the Free Software Foundation; either version 2 of the License, or //
10// (at your option) any later version. //
11// //
12// This program is distributed in the hope that it will be useful, //
13// but WITHOUT ANY WARRANTY; without even the implied warranty of //
14// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the //
15// GNU General Public License for more details. //
16// //
17// You should have received a copy of the GNU General Public License //
18// along with this program; if not, write to the Free Software //
19// Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA //
20// //
21// $Revision:: 139 $//
22// $Date:: 2007-01-23 16:09:23 +0100 (Tue, 23 Jan 2007) $//
23///////////////////////////////////////////////////////////////////////////////
24
25
26
27#include "CmdLine.hh"
28#include<string>
29#include<sstream>
30#include<iostream> // testing
31#include<vector>
32#include<cstddef> // for size_t
33#include<cstdlib> // for exit
34using namespace std;
35
36// initialise the various structures that we shall
37// use to access the command-line options;
38//
39// If an option appears several times, it is its LAST value
40// that will be used in searching for option values (opposite of f90)
41CmdLine::CmdLine (const int argc, char** argv) {
42 __arguments.resize(argc);
43 for(int iarg = 0; iarg < argc; iarg++){
44 __arguments[iarg] = argv[iarg];
45 }
46 this->init();
47}
48
49/// constructor from a vector of strings, one argument per string
50CmdLine::CmdLine (const vector<string> & args) {
51 __arguments = args;
52 this->init();
53}
54
55//----------------------------------------------------------------------
56void CmdLine::init (){
57 __command_line = "";
58 for(size_t iarg = 0; iarg < __arguments.size(); iarg++){
59 __command_line += __arguments[iarg];
60 __command_line += " ";
61 }
62
63 // group things into options
64 bool next_may_be_val = false;
65 string currentopt;
66 for(size_t iarg = 1; iarg < __arguments.size(); iarg++){
67 // if expecting an option value, then take it (even if
68 // it is actually next option...)
69 if (next_may_be_val) {__options[currentopt] = iarg;}
70 // now see if it might be an option itself
71 string arg = __arguments[iarg];
72 bool thisisopt = (arg.compare(0,1,"-") == 0);
73 if (thisisopt) {
74 // set option to a standard undefined value and say that
75 // we expect (possibly) a value on next round
76 currentopt = arg;
77 __options[currentopt] = -1;
78 __options_used[currentopt] = false;
79 next_may_be_val = true;}
80 else {
81 // otherwise throw away the argument for now...
82 next_may_be_val = false;
83 currentopt = "";
84 }
85 }
86}
87
88// indicates whether an option is present
89bool CmdLine::present(const string & opt) const {
90 bool result = (__options.find(opt) != __options.end());
91 if (result) __options_used[opt] = true;
92 return result;
93}
94
95// indicates whether an option is present and has a value associated
96bool CmdLine::present_and_set(const string & opt) const {
97 bool result = present(opt) && __options[opt] > 0;
98 return result;
99}
100
101
102// return the string value corresponding to the specified option
103string CmdLine::string_val(const string & opt) const {
104 if (!this->present_and_set(opt)) {
105 cerr << "Error: Option "<<opt
106 <<" is needed but is not present_and_set"<<endl;
107 exit(-1);
108 }
109 string arg = __arguments[__options[opt]];
110 // this may itself look like an option -- if that is the case
111 // declare the option to have been used
112 if (arg.compare(0,1,"-") == 0) {__options_used[arg] = true;}
113 return arg;
114}
115
116// as above, but if opt is not present_and_set, return default
117string CmdLine::string_val(const string & opt, const string & defval) const {
118 if (this->present_and_set(opt)) {return string_val(opt);}
119 else {return defval;}
120}
121
122// Return the integer value corresponding to the specified option;
123// Not too sure what happens if option is present_and_set but does not
124// have string value...
125int CmdLine::int_val(const string & opt) const {
126 int result;
127 string optstring = string_val(opt);
128 istringstream optstream(optstring);
129 optstream >> result;
130 if (optstream.fail()) {
131 cerr << "Error: could not convert option ("<<opt<<") value ("
132 <<optstring<<") to int"<<endl;
133 exit(-1);}
134 return result;
135}
136
137// as above, but if opt is not present_and_set, return default
138int CmdLine::int_val(const string & opt, const int & defval) const {
139 if (this->present_and_set(opt)) {return int_val(opt);}
140 else {return defval;}
141}
142
143
144// Return the integer value corresponding to the specified option;
145// Not too sure what happens if option is present_and_set but does not
146// have string value...
147double CmdLine::double_val(const string & opt) const {
148 double result;
149 string optstring = string_val(opt);
150 istringstream optstream(optstring);
151 optstream >> result;
152 if (optstream.fail()) {
153 cerr << "Error: could not convert option ("<<opt<<") value ("
154 <<optstring<<") to double"<<endl;
155 exit(-1);}
156 return result;
157}
158
159// as above, but if opt is not present_and_set, return default
160double CmdLine::double_val(const string & opt, const double & defval) const {
161 if (this->present_and_set(opt)) {return double_val(opt);}
162 else {return defval;}
163}
164
165
166// return the full command line including the command itself
167string CmdLine::command_line() const {
168 return __command_line;
169}
170
171
172// return true if all options have been asked for at some point or other
174 bool result = true;
175 for(map<string,bool>::const_iterator opt = __options_used.begin();
176 opt != __options_used.end(); opt++) {
177 bool this_one = opt->second;
178 if (! this_one) {cerr << "Option "<<opt->first<<" unused/unrecognized"<<endl;}
179 result = result && this_one;
180 }
181 return result;
182}
183
184
185/// report failure of conversion
186void CmdLine::_report_conversion_failure(const string & opt,
187 const string & optstring) const {
188 cerr << "Error: could not convert option ("<<opt<<") value ("
189 <<optstring<<") to requested type"<<endl;
190 exit(-1);
191}
192
bool all_options_used() const
return true if all options have been asked for at some point or other
Definition: CmdLine.cc:173
int int_val(const string &opt) const
return the integer value corresponding to the given option
Definition: CmdLine.cc:125
double double_val(const string &opt) const
return the double value corresponding to the given option
Definition: CmdLine.cc:147
bool present_and_set(const string &opt) const
true if the option is present and corresponds to a value
Definition: CmdLine.cc:96
string command_line() const
return the full command line
Definition: CmdLine.cc:167
string string_val(const string &opt) const
return the string value corresponding to the given option
Definition: CmdLine.cc:103
bool present(const string &opt) const
true if the option is present
Definition: CmdLine.cc:89