source: trunk/FACT++/src/argv.cc@ 10535

Last change on this file since 10535 was 10372, checked in by tbretz, 14 years ago
Changed second argument in Parse to const as well as in all main functions.
File size: 8.5 KB
Line 
1#include "Configuration.h"
2
3#include <iostream>
4
5using namespace std;
6
7// --------------------------------------------------------------------------
8//
9//! Main Doxygen/Autotools integration example program.
10//!
11//! @param conf Number of command line options.
12//! @param opt The command line options.
13//! @return The exit status.
14//
15void SetupConfiguration(Configuration &conf, int &opt)
16{
17 /*
18 // Default in case the option was not specified
19 typed_value* default_value(const T& v)
20 typed_value* default_value(const T& v, const std::string& textual)
21
22 // Default value in case the option was given
23 // forces -o to become -o5
24 // forces --opt to become --opt=3
25 typed_value* implicit_value(const T &v)
26 typed_value* implicit_value(const T &v, const std::string& textual)
27
28 // notifier function when the final value is determined
29 typed_value* notifier(function1<void, const T&> f)
30
31 /// Merge values from different sources (e.g. file, command line)
32 typed_value* composing()
33
34 // Specifies that the value can span multiple tokens.
35 typed_value* multitoken()
36 typed_value* zero_tokens()
37
38 // Specifies that the value must occur.
39 typed_value* required()
40 */
41
42 // To merge the options from several parsers (e.g. comand_line and
43 // config file) use po_strings()->composing()
44 /*
45 po::options_description generic("Generic options");
46 generic.add_options()
47 ("help-config", "Print available configuration file options.")
48 ("help-env", "Print available environment variables.")
49 ("help", "Print available commandline options.")
50 ("print-unknown", "Print unrecognized options.")
51 ("config", po_string("config.txt"), "Set configuration file name.")
52 ;
53
54
55 // Declare the supported options.
56 po::options_description generic("Generaic options");
57 generic.add_options()
58// ("testreq", po_int()->required(), "set compression level (madatory)")
59 ("default", po_string("my_default"), "set compression level")
60 ("unknown", po_int(1), "set compression level")
61 ("U", po_int(2)->implicit_value(1), "set compression level")
62 ;
63 */
64 // Declare a group of options that will be
65 // allowed both on command line and in
66 // config file
67 po::options_description config("Configuration");
68 config.add_options()
69 ("compression", var<int>(), "set compression level")
70 ("optimization", var<int>(10, &opt), "optimization level")
71 ("test-def", var<int>(42), "optimization level")
72 ("include-path,I", vars<string>()/*->composing()*/, "include path")
73 ("test,T", vars<string>()/*->composing()*/, "include path")
74 ("file1", vars<string>(), "include path")
75 ("int1", var<int>(), "include path")
76 ("Int2", var<int>(), "include path")
77 ("Int1", var<int>(), "include path")
78 ("test-db", var<string>("database"), "include path")
79 ("float1", var<double>(), "include path")
80// (",A", po_float(), "include path")
81 ("radec", po::value<vector<double>>(), "include path")
82 ;
83
84 // !!! Option which are "shorted" must be placed last.
85 // Can this be switched off?
86
87 po::options_description sections("Sections");
88 config.add_options()
89 ("unregistered", var<string>(), "include path")
90 ("Section1.unregistered", var<string>(), "include path")
91// ("Section2*", po_string(), "include path")
92 // The latter accepts all options starting with Section2.
93 ;
94
95 // Hidden options, will be allowed both on command line and
96 // in config file, but will not be shown to the user.
97 po::options_description hidden("Hidden options");
98 hidden.add_options()
99 ("input-file", vars<string>(), "input file")
100 ("output-file", vars<string>(), "output file")
101 ("test-file", vars<string>(), "test file")
102 ;
103
104 po::options_description env("Environment options");
105 env.add_options()
106 ("linux", var<string>(), "LINUX env")
107 ("path", var<string>(), "PATH env")
108 ("dns", var<string>(), "DIM_DNS_SERVER env")
109 ;
110
111 conf.AddEnv("linux", "LINUX");
112 conf.AddEnv("path", "PATH");
113 conf.AddEnv("dns", "DIM_DNS_SERVER");
114
115 // define translation from position to name
116 po::positional_options_description p;
117 p.add("output-file", 2); // The first 2 positional options is output-file
118 p.add("test-file", 3); // The next three positional options is output-file
119 p.add("input-file", -1); // All others go to...
120
121 conf.AddOptionsCommandline(config);
122 conf.AddOptionsCommandline(sections);
123 conf.AddOptionsCommandline(hidden, false);
124
125 conf.AddOptionsConfigfile(config);
126 conf.AddOptionsConfigfile(sections);
127 conf.AddOptionsConfigfile(hidden, false);
128
129 conf.AddOptionsEnvironment(env);
130
131 conf.AddOptionsDatabase(config);
132
133 conf.SetArgumentPositions(p);
134}
135
136
137int main(int argc, const char **argv)
138{
139 int opt;
140
141 Configuration conf(argv[0]);
142 SetupConfiguration(conf, opt);
143
144 po::variables_map vm;
145 try
146 {
147 vm = conf.Parse(argc, argv);
148 }
149 catch (std::exception &e)
150 {
151#if BOOST_VERSION > 104000
152 po::multiple_occurrences *MO = dynamic_cast<po::multiple_occurrences*>(&e);
153 if (MO)
154 cout << "Error: " << e.what() << " of '" << MO->get_option_name() << "' option." << endl;
155 else
156#endif
157 cout << "Error: " << e.what() << endl;
158 cout << endl;
159
160 return -1;
161 }
162
163 if (conf.HasHelp() || conf.HasPrint())
164 return -1;
165
166 cout << "------------------------------" << endl;
167
168 cout << "Program " << argv[0] << " started successfully." << endl;
169 return 0;
170/*
171 if (vm.count("compression"))
172 cout << "Compression level was set to " << vm["compression"].as<int>() << ".\n";
173 else
174 cout << "Compression level was not set.\n";
175
176
177 cout << "Test default is always: " << vm["test-def"].as<int>() << "\n";
178 cout << "Optimization level is " << vm["optimization"].as<int>() << "\n";
179 //cout << "Int2: " << vm["Int2"].as<int>() << "\n";
180
181 cout << conf.GetString("unregistered") << endl;
182 cout << conf.GetString("Section1.unregistered") << endl;
183 cout << conf.Has("Section2.unregistered") << endl;
184 cout << conf.GetString("Section2.Section3.unregistered") << endl;
185 cout << "test-db: " << conf.GetString("test-db") << endl;
186
187
188 if (vm.count("include-path"))
189 {
190 vector<string> v = vm["include-path"].as< vector<string> >();
191 for (vector<string>::iterator s=v.begin(); s<v.end(); s++)
192 cout << "Incl P: " << *s << endl;
193 }
194
195 if (vm.count("input-file"))
196 {
197 vector<string> v = vm["input-file"].as< vector<string> >();
198 for (vector<string>::iterator s=v.begin(); s<v.end(); s++)
199 cout << "Incl F: " << *s << endl;
200 }
201
202 if (vm.count("output-file"))
203 {
204 vector<string> v = vm["output-file"].as< vector<string> >();
205 for (vector<string>::iterator s=v.begin(); s<v.end(); s++)
206 cout << "Out: " << *s << endl;
207 }
208
209 if (vm.count("test-file"))
210 {
211 vector<string> v = vm["test-file"].as< vector<string> >();
212 for (vector<string>::iterator s=v.begin(); s<v.end(); s++)
213 cout << "Testf: " << *s << endl;
214 }
215
216 cout << "Linux: " << conf.Get<string>("linux") << endl;
217
218 if (vm.count("path"))
219 cout << "Path: " << vm["path"].as<string>() << endl;
220 if (vm.count("file1"))
221 cout << "File1: " << vm["file1"].as<string>() << endl;
222 if (vm.count("int1"))
223 cout << "Int1: " << vm["int1"].as<int>() << endl;
224 if (vm.count("float1"))
225 cout << "Float1: " << vm["float1"].as<float>() << endl;
226
227 if (vm.count("test"))
228 {
229 vector<string> v = vm["test"].as< vector<string> >();
230 for (vector<string>::iterator s=v.begin(); s<v.end(); s++)
231 cout << "Test: " << *s << endl;
232 }*/
233}
234// ***************************************************************************
235/** @example argv.cc
236
237Example for the usage of the class Configuration
238
239**/
240// ***************************************************************************
Note: See TracBrowser for help on using the repository browser.