]> Creatis software - cpPlugins.git/blob - appli/bash/ExecutePipeline.cxx
Moved to version 1.0
[cpPlugins.git] / appli / bash / ExecutePipeline.cxx
1 // =========================================================================
2 // @author Leonardo Florez-Valencia (florez-l@javeriana.edu.co)
3 // =========================================================================
4
5 #include <iostream>
6 #include <vector>
7 #include <boost/filesystem.hpp>
8 #include <boost/program_options.hpp>
9 #include <cpPlugins/Pipeline.h>
10
11 int main( int argc, char* argv[] )
12 {
13   typedef cpPlugins::Pipeline TPipeline;
14   namespace bpo = boost::program_options;
15
16   // Declare the supported options.
17   bpo::options_description d( "Allowed options" );
18   d.add_options( )
19     ( "help,h", "produce help message" )
20     ( "interactive,i", bpo::bool_switch( )->default_value( false ), "Run an interactive menu to configure pipeline" )
21     ( "overwrite,o", bpo::bool_switch( )->default_value( false ), "Overwrite XML file" )
22     ( "contents,c", bpo::bool_switch( )->default_value( false ), "Print pipeline contents" )
23     ( "value,v", bpo::value< std::vector< std::string > >( ), "Parameter values" )
24     ( "pipeline,p", bpo::value< std::string >( )->required( ), "Pipeline file" )
25     ;
26   bpo::positional_options_description p;
27   p.add( "pipeline", -1 );
28   
29   // Parse input arguments
30   bpo::variables_map v;
31   try
32   {
33     bpo::store(
34       bpo::
35       command_line_parser( argc, argv ).options( d ).positional( p ).run( ),
36       v
37       );
38     bpo::notify( v );
39     if( v.count( "help" ) > 0 )
40     {
41       std::cerr << d << std::endl;
42       return( 0 );
43
44     } // fi
45   }
46   catch( std::exception& err )
47   {
48     std::cerr << "Error caught: " << err.what( ) << std::endl;
49     std::cerr << d << std::endl;
50     return( 1 );
51
52   } // yrt
53
54   try
55   {
56     // Load pipeline
57     TPipeline::SharedPtr pipeline = TPipeline::New( );
58     pipeline->ExecutionDebugOn( );
59     pipeline->LoadXML( v[ "pipeline" ].as< std::string >( ) );
60
61     // Configure pipeline
62     if( v.count( "value" ) > 0 )
63     {
64       for(
65         const std::string& pv:
66           v[ "value" ].as< std::vector< std::string > >( )
67         )
68       {
69         std::size_t b = pv.find_first_of( "=" );
70         pipeline->SetInValue( pv.substr( 0, b ), pv.substr( b + 1 ) );
71
72       } // rof
73
74     } // fi
75
76     // Just print pipeline contents
77     if( v[ "contents" ].as< bool >( ) )
78     {
79       /* TODO
80          TPipeline::TParametersMap pmap = pipeline->GetParametersMap( );
81          TPipeline::TParametersMap::const_iterator pIt;
82          for( pIt = pmap.begin( ); pIt != pmap.end( ); ++pIt )
83          {
84          std::cout
85          << pIt->first << " ("
86          << pIt->second.first << "="
87          << pIt->second.second << ")"
88          << std::endl;
89
90          } // rof
91       */
92       return( 0 );
93
94     } // fi
95
96     // Interactively configure pipeline
97     if( v[ "interactive" ].as< bool >( ) )
98     {
99       std::cout << "** Interactively configuring pipeline **" << std::endl;
100       /* TODO
101          TPipeline::TParametersMap pmap = pipeline->GetParametersMap( );
102          TPipeline::TParametersMap::const_iterator pIt;
103          for( pIt = pmap.begin( ); pIt != pmap.end( ); ++pIt )
104          {
105          std::cout
106          << pIt->first << " ("
107          << pIt->second.first << "="
108          << pIt->second.second << "): "
109          << std::ends;
110          std::string line;
111          std::getline( std::cin, line );
112          if( line != "" )
113          {
114          std::size_t p = pIt->first.find( "@" );
115          pipeline->SetParameter(
116          pIt->first.substr( p + 1 ),
117          pIt->first.substr( 0, p ),
118          line
119          );
120
121          } // fi
122
123          } // rof
124       */
125     } // fi
126
127     // Execute pipeline
128     pipeline->Update( );
129
130     // Save pipeline
131     if( v[ "overwrite" ].as< bool >( ) )
132       pipeline->SaveXML( v[ "pipeline" ].as< std::string >( ) );
133   }
134   catch( std::exception& err )
135   {
136     std::cerr << "ERROR CAUGHT: " << err.what( ) << std::endl;
137     return( 1 );
138
139   } // yrt
140   return( 0 );
141 }
142
143 // eof - $RCSfile$