4109c8612dcbb8591333643ce456ece5262a6e28
[metaproxy-moved-to-github.git] / src / yp2_prog.cpp
1 /* $Id: yp2_prog.cpp,v 1.2 2006-01-19 09:32:08 adam Exp $
2    Copyright (c) 2005, 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 int main(int argc, char **argv)
21 {
22     try 
23     {
24         po::options_description desc("Allowed options");
25         desc.add_options()
26             ("help", "produce help message")
27             ("config", po::value< std::vector<std::string> >(), "xml config")
28             ;
29         
30         po::positional_options_description p;
31         p.add("config", -1);
32         
33         po::variables_map vm;        
34         po::store(po::command_line_parser(argc, argv).
35                   options(desc).positional(p).run(), vm);
36         po::notify(vm);    
37         
38         if (vm.count("help")) {
39             std::cout << desc << "\n";
40             return 1;
41         }
42         
43         xmlDocPtr doc = 0;
44         if (vm.count("config"))
45         {
46             std::vector<std::string> config_fnames = 
47                 vm["config"].as< std::vector<std::string> >();
48
49             if (config_fnames.size() != 1)
50             {
51                 std::cerr << "Only one configuration must be given\n";
52                 std::exit(1);
53             }
54             
55             doc = xmlParseFile(config_fnames[0].c_str());
56             if (!doc)
57             {
58                 std::cerr << "xmlParseFile failed\n";
59                 std::exit(1);
60             }
61         }
62         else
63         {
64             std::cerr << "No configuration given\n";
65             std::exit(1);
66         }
67         if (doc)
68         {
69             try {
70                 yp2::FactoryStatic factory;
71                 yp2::RouterFleXML router(doc, factory);
72                 yp2::Package pack;
73                 pack.router(router).move();
74             }
75             catch (std::runtime_error &e) {
76                 std::cout << "std::runtime error: " << e.what() << "\n";
77                 exit(1);
78             }
79             xmlFreeDoc(doc);
80         }
81     }
82     catch ( ... ) {
83         std::cerr << "Unknown Exception" << std::endl;
84         std::exit(1);
85     }
86     std::exit(0);
87 }
88
89
90 /*
91  * Local variables:
92  * c-basic-offset: 4
93  * indent-tabs-mode: nil
94  * c-file-style: "stroustrup"
95  * End:
96  * vim: shiftwidth=4 tabstop=8 expandtab
97  */