1 /************************************************************************
3 ** Copyright (C) 2010 Strahinja Markovic
5 ** This file is part of FlightCrew.
7 ** FlightCrew is free software: you can redistribute it and/or modify
8 ** it under the terms of the GNU Lesser General Public License as published
9 ** by the Free Software Foundation, either version 3 of the License, or
10 ** (at your option) any later version.
12 ** FlightCrew is distributed in the hope that it will be useful,
13 ** but WITHOUT ANY WARRANTY; without even the implied warranty of
14 ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 ** GNU Lesser General Public License for more details.
17 ** You should have received a copy of the GNU Lesser General Public License
18 ** along with FlightCrew. If not, see <http://www.gnu.org/licenses/>.
20 *************************************************************************/
23 #include <flightcrew.h>
24 namespace fc = FlightCrew;
26 // We set the MSVC warning level down to 3
27 // for code that we have no control over
29 # pragma warning( push, 3 )
32 #include <boost/foreach.hpp>
33 // We're most definitely not going to use
34 // it as BOOST_FOREACH.
35 #define foreach BOOST_FOREACH
37 #include <boost/program_options.hpp>
38 namespace po = boost::program_options;
40 // ... and then we reset the warning level
41 // back to normal (warning level 4)
43 # pragma warning( pop )
46 static const std::string FLIGHTCREW_VERSION = FLIGHTCREW_FULL_VERSION;
48 bool ValidateFiles( const std::vector< std::string > &files_to_validate )
50 bool seen_problems = false;
52 foreach( const std::string &file, files_to_validate )
54 std::vector< fc::Result > results;
58 results = fc::ValidateEpub( file );
61 catch ( fc::FileDoesNotExistEx& )
63 std::cerr << "File " << file << " does not exist.\n\n";
67 foreach( const fc::Result &result, results )
70 std::cerr << result.GetFilepath();
72 if ( result.GetErrorLine() > 0 )
74 std::cerr << "(" << result.GetErrorLine() << ")";
78 if ( result.GetResultType() == fc::ResultType_ERROR )
80 std::cerr << "error ";
84 std::cerr << "warning ";
86 std::cerr << result.GetResultId() << ": ";
87 std::cerr << result.GetMessage() << "\n";
90 if ( !results.empty() )
99 int main( int argc, char *argv[] )
103 po::options_description options( "Allowed options" );
104 options.add_options()
105 ( "help", "produce help message" )
106 ( "version", "show the program version")
107 ( "input-file", po::value< std::vector< std::string > >(), "input file" );
109 po::positional_options_description positionals;
110 // -1 for count means unlimited
111 positionals.add( "input-file", -1 );
113 po::variables_map var_map;
114 store( po::command_line_parser( argc, argv ).
115 options( options ).positional( positionals ).run(), var_map );
116 po::notify( var_map );
118 if ( var_map.count( "help" ) )
120 std::cout << "Usage: flightcrew-cli [options] file...\n";
121 std::cout << options << "\n";
125 if ( var_map.count( "version" ) )
127 std::cout << "flightcrew-cli version: " << FLIGHTCREW_VERSION << " " << "\n";
131 if ( var_map.count( "input-file" ) )
133 std::vector< std::string > files_to_validate =
134 var_map[ "input-file" ].as< std::vector< std::string > >();
136 bool problems_found = ValidateFiles( files_to_validate );
137 if ( !problems_found )
139 std::cout << "No problems found.\n";
141 return problems_found;
146 std::cout << "No input file specified. Use --help for usage information.\n";
150 catch ( std::exception& exception )
152 std::cerr << "Error during run: " << exception.what() << "\n";
158 std::cerr << "Unknown exception!\n";