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

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