Fixed bug #569: Some options throws unknown exception.
[metaproxy-moved-to-github.git] / src / metaproxy_prog.cpp
1 /* $Id: metaproxy_prog.cpp,v 1.3 2006-04-29 08:47:40 adam Exp $
2    Copyright (c) 2005-2006, Index Data.
3
4 %LICENSE%
5  */
6
7 #include "config.hpp"
8
9 #include <boost/program_options.hpp>
10 namespace po = boost::program_options;
11
12 #include <iostream>
13 #include <stdexcept>
14
15 #include "filter.hpp"
16 #include "package.hpp"
17 #include "router_flexml.hpp"
18 #include "factory_static.hpp"
19
20 namespace mp = metaproxy_1;
21
22 int main(int argc, char **argv)
23 {
24     try 
25     {
26         po::options_description desc("Allowed options");
27         desc.add_options()
28             ("help,h", "produce help message")
29             ("config", po::value< std::vector<std::string> >(), "xml config")
30             ;
31         
32         po::positional_options_description p;
33         p.add("config", -1);
34         
35         po::variables_map vm;        
36         po::store(po::command_line_parser(argc, argv).
37                   options(desc).positional(p).run(), vm);
38         po::notify(vm);
39         
40         if (vm.count("help")) {
41             std::cout << desc << "\n";
42             return 1;
43         }
44         
45         xmlDocPtr doc = 0;
46         if (vm.count("config"))
47         {
48             std::vector<std::string> config_fnames = 
49                 vm["config"].as< std::vector<std::string> >();
50
51             if (config_fnames.size() != 1)
52             {
53                 std::cerr << "Only one configuration must be given\n";
54                 std::exit(1);
55             }
56             
57             doc = xmlParseFile(config_fnames[0].c_str());
58             if (!doc)
59             {
60                 std::cerr << "xmlParseFile failed\n";
61                 std::exit(1);
62             }
63         }
64         else
65         {
66             std::cerr << "No configuration given\n";
67             std::exit(1);
68         }
69         if (doc)
70         {
71             try {
72                 mp::FactoryStatic factory;
73                 mp::RouterFleXML router(doc, factory);
74                 mp::Package pack;
75                 pack.router(router).move();
76             }
77             catch (std::runtime_error &e) {
78                 std::cout << "std::runtime error: " << e.what() << "\n";
79                 std::exit(1);
80             }
81             xmlFreeDoc(doc);
82         }
83     }
84     catch (po::unknown_option &e) {
85         std::cerr << e.what() << "; use --help for list of options\n";
86         std::exit(1);
87     }
88     catch (std::logic_error &e) {
89         std::cerr << "std::logic error: " << e.what() << "\n";
90         std::exit(1);
91     }
92     catch (std::runtime_error &e) {
93         std::cout << "std::runtime error: " << e.what() << "\n";
94         std::exit(1);
95     }
96     catch ( ... ) {
97         std::cerr << "Unknown Exception" << std::endl;
98         std::exit(1);
99     }
100     std::exit(0);
101 }
102
103
104 /*
105  * Local variables:
106  * c-basic-offset: 4
107  * indent-tabs-mode: nil
108  * c-file-style: "stroustrup"
109  * End:
110  * vim: shiftwidth=4 tabstop=8 expandtab
111  */