]> Creatis software - creaImageIO.git/blob - appli/gimmick/main.cxx
Added command line interaction with the synchronization option.
[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     ("file,f",po::value< vector<string> >(),"Adds the file to the local database")
29     ("dir,d",po::value< vector<string> >(),"Adds the directory to the local database")
30     ("sync,s","Synchronizes the local database with the files")
31     ("recurse,r","Recurse into sub-directories  (default=off)")
32         ("repair,R","Repair the database (on synchronization) (default=off)")
33         ("check,c","Check for attribute differences (on synchronization) (default=off)");
34
35   
36   //Describes third group of options
37   po::options_description option("OPTIONS");
38   option.add_options()
39     ("verbose,v",po::value<int>(&verb),"Verbosity level (default=`1')")
40     ("debug,D",po::value<int>(&deb),"Debug messages level (default=`0')");
41  
42   //Adds the groups into a big one
43   po::options_description cmdline_options;
44   cmdline_options.add(generic).add(command).add(option);
45   
46   //Adds the corresponding variables
47   po::variables_map vm;
48   po::store(po::parse_command_line(ac, av, cmdline_options), vm);
49   po::notify(vm);    
50   
51   //Does something on each option
52   //GENERIC
53   if (vm.count("help")) {
54     cout << "Usage: gimmick COMMAND [OPTIONS]:\n";
55     cout << cmdline_options << "\n";
56   }
57   if (vm.count("version")) {
58     cout << "gimmick 0.1.0\n";
59   }
60   
61   //OPTIONS
62   if (vm.count("verbose")) {
63     g.SetMessageLevel(verb);
64     cout << "Verbose level is now "<<verb<<"\n";
65   }
66   if (vm.count("debug")) {
67     g.SetDebugMessageLevel(deb);
68     cout << "Debug level is now "<<deb<<"\n";
69   }
70   
71   //COMMANDS
72   if (  vm.count("print")
73         ||vm.count("file")
74         ||vm.count("dir")
75         ||vm.count("sync")
76         )
77
78     try
79       {
80         g.Initialize();
81         if (vm.count("print")) {
82           g.GetTreeHandler(handler)->LoadChildren(0,0);
83           g.Print(handler);
84         }
85         if (vm.count("file")) {
86           std::vector<std::string> files=vm["file"].as< vector<string> >();
87           g.AddFiles(handler,files);
88         }
89         if (vm.count("dir")) {
90           std::vector<std::string> dirs=vm["dir"].as< vector<string> >();
91           bool recu=false;
92           std::stringstream out;
93           out<<vm.count("recurse");
94           if(out.str().compare("1")==0){recu=true;}
95           g.AddDir(handler,dirs.front(), recu);
96         }
97         if (vm.count("sync")) {
98           bool rep=false;
99       bool chk=false;
100           std::stringstream out;
101           out<<vm.count("repair");
102           if(out.str().compare("1")==0){rep=true;}
103           std::stringstream out2;
104           out2<<vm.count("check");
105           if(out2.str().compare("1")==0){chk=true;}
106           g.Synchronize(rep,chk);
107         }       
108         g.Finalize();
109       }
110     catch (crea::Exception e)
111       {
112         e.Print();
113       }
114           
115   
116   return 0;
117 }