]> Creatis software - creaImageIO.git/blob - appli/gimmick/main.cxx
b064a25829d19c22f0706e81a9681f42ddff3e6d
[creaImageIO.git] / appli / gimmick / main.cxx
1 #include <boost/program_options.hpp>
2
3 using namespace boost;
4 namespace po = boost::program_options;
5
6 #include <creaImageIOGimmick.h>
7 #include <creaMessageManager.h>
8
9 using namespace std;
10
11 int main(int ac, char* av[])
12 {
13   
14   creaImageIO::Gimmick g;
15   int verb,deb;
16   std::string handler("Local database");
17   
18   //Describes first group of options
19   po::options_description generic("GENERIC");
20   generic.add_options()
21     ("help,h", "Print help and exit")
22     ("version,V", "Print version and exit");
23   
24   //Describes second group of options
25   po::options_description command("COMMANDS");
26   command.add_options()
27     ("print,p","Prints the local database tree  (default=off)")
28     ("files,f",po::value< vector<string> >(),"Adds the file(s) to the local database")
29     ("dir,d",po::value< vector<string> >(),"Adds the directory to the local database")
30     ("sync,s",po::value< vector<string> >(),"Synchronizes the local database with the files")
31         ("copy,c",po::value< vector<string> >(),"Copies the files into a local directory");
32
33   //Describes third group of options
34   po::options_description option("OPTIONS");
35   option.add_options()
36     ("verbose,v",po::value<int>(&verb),"Verbosity level (default=`1')")
37     ("debug,D",po::value<int>(&deb),"Debug messages level (default=`0')")
38         ("recurse,r","Recurse into sub-directories  (default=off)")
39         ("repair,R","Repair the database (on synchronization) (default=off)")
40         ("check,C","Check for attribute differences (on synchronization) (default=off)")
41         ("handler,H",po::value<string>(&handler),"Handler name (default=`Local database')");
42  
43   //Adds the groups into a big one
44   po::options_description cmdline_options;
45   cmdline_options.add(generic).add(command).add(option);
46   
47   //Adds the corresponding variables
48   po::variables_map vm;
49   po::store(po::parse_command_line(ac, av, cmdline_options), vm);
50   po::notify(vm);    
51   
52   
53   //Does something on each option
54   //GENERIC
55   if (vm.count("help")) {
56     cout << "Usage: gimmick COMMAND [OPTIONS]:\n";
57     cout << cmdline_options << "\n";
58   }
59   if (vm.count("version")) {
60     cout << "gimmick 0.1.0\n";
61   }
62   
63   //OPTIONS
64   if (vm.count("verbose")) {
65     g.SetMessageLevel(verb);
66     cout << "Verbose level is now "<<verb<<"\n";
67   }
68   if (vm.count("debug")) {
69     g.SetDebugMessageLevel(deb);
70     cout << "Debug level is now "<<deb<<"\n";
71   }
72   
73   //COMMANDS
74   if (  vm.count("print")
75         ||vm.count("file")
76         ||vm.count("dir")
77         ||vm.count("sync")
78         ||vm.count("copy")
79         )
80   {
81
82     try
83       {
84         g.Initialize();
85         if (vm.count("print")) {
86           g.GetTreeHandler(handler)->LoadChildren(0,0);
87           g.Print(handler);
88         }
89         if (vm.count("file")) {
90           std::vector<std::string> files=vm["file"].as< vector<string> >();
91           g.AddFiles(handler,files);
92         }
93         if (vm.count("dir")) {
94           std::vector<std::string> dirs=vm["dir"].as< vector<string> >();
95           bool recu=false;
96           std::stringstream out;
97           out<<vm.count("recurse");
98           if(out.str().compare("1")==0){recu=true;}
99           g.AddDir(handler,dirs.front(), recu);
100         }
101         if (vm.count("sync")) {
102           bool rep=false;
103       bool chk=false;
104           std::vector<std::string> name=vm["sync"].as< vector<string> >();
105           std::stringstream out;
106           out<<vm.count("repair");
107           if(out.str().compare("1")==0){rep=true;}
108           std::stringstream out2;
109           out2<<vm.count("check");
110           if(out2.str().compare("1")==0){chk=true;}
111           cout<<g.Synchronize(name.front(),rep,chk)<<"\n";
112         }       
113         if (vm.count("copy")) {
114           std::vector<std::string> name=vm["copy"].as< vector<string> >();
115           g.CopyFiles(name,handler);
116         }
117         g.Finalize();
118       }
119     catch (crea::Exception e)
120       {
121         e.Print();
122       }
123   }
124           
125   
126   return 0;
127 }