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

Last change on this file since 10222 was 10222, checked in by tbretz, 11 years ago
Adapted to changed in Configuration class
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, 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 po::multiple_occurrences *MO = dynamic_cast<po::multiple_occurrences*>(&e);
152 if (MO)
153 cout << "Error: " << e.what() << " of '" << MO->get_option_name() << "' option." << endl;
154 else
155 cout << "Error: " << e.what() << endl;
156 cout << endl;
157
158 return -1;
159 }
160
161 if (conf.HasHelp() || conf.HasPrint())
162 return -1;
163
164 cout << "------------------------------" << endl;
165
166 cout << "Program " << argv[0] << " started successfully." << endl;
167 return 0;
168/*
169 if (vm.count("compression"))
170 cout << "Compression level was set to " << vm["compression"].as<int>() << ".\n";
171 else
172 cout << "Compression level was not set.\n";
173
174
175 cout << "Test default is always: " << vm["test-def"].as<int>() << "\n";
176 cout << "Optimization level is " << vm["optimization"].as<int>() << "\n";
177 //cout << "Int2: " << vm["Int2"].as<int>() << "\n";
178
179 cout << conf.GetString("unregistered") << endl;
180 cout << conf.GetString("Section1.unregistered") << endl;
181 cout << conf.Has("Section2.unregistered") << endl;
182 cout << conf.GetString("Section2.Section3.unregistered") << endl;
183 cout << "test-db: " << conf.GetString("test-db") << endl;
184
185
186 if (vm.count("include-path"))
187 {
188 vector<string> v = vm["include-path"].as< vector<string> >();
189 for (vector<string>::iterator s=v.begin(); s<v.end(); s++)
190 cout << "Incl P: " << *s << endl;
191 }
192
193 if (vm.count("input-file"))
194 {
195 vector<string> v = vm["input-file"].as< vector<string> >();
196 for (vector<string>::iterator s=v.begin(); s<v.end(); s++)
197 cout << "Incl F: " << *s << endl;
198 }
199
200 if (vm.count("output-file"))
201 {
202 vector<string> v = vm["output-file"].as< vector<string> >();
203 for (vector<string>::iterator s=v.begin(); s<v.end(); s++)
204 cout << "Out: " << *s << endl;
205 }
206
207 if (vm.count("test-file"))
208 {
209 vector<string> v = vm["test-file"].as< vector<string> >();
210 for (vector<string>::iterator s=v.begin(); s<v.end(); s++)
211 cout << "Testf: " << *s << endl;
212 }
213
214 cout << "Linux: " << conf.Get<string>("linux") << endl;
215
216 if (vm.count("path"))
217 cout << "Path: " << vm["path"].as<string>() << endl;
218 if (vm.count("file1"))
219 cout << "File1: " << vm["file1"].as<string>() << endl;
220 if (vm.count("int1"))
221 cout << "Int1: " << vm["int1"].as<int>() << endl;
222 if (vm.count("float1"))
223 cout << "Float1: " << vm["float1"].as<float>() << endl;
224
225 if (vm.count("test"))
226 {
227 vector<string> v = vm["test"].as< vector<string> >();
228 for (vector<string>::iterator s=v.begin(); s<v.end(); s++)
229 cout << "Test: " << *s << endl;
230 }*/
231}
232// ***************************************************************************
233/** @example argv.cc
234
235Example for the usage of the class Configuration
236
237**/
238// ***************************************************************************
Note: See TracBrowser for help on using the repository browser.