|
22 | 22 |
|
23 | 23 | #include <boost/algorithm/string/predicate.hpp> |
24 | 24 | #include <boost/optional.hpp> |
| 25 | +#include <boost/program_options.hpp> |
25 | 26 | #include <boost/utility/string_view.hpp> |
26 | 27 | #include <cstdlib> |
27 | 28 | #include <curl/curl.h> |
@@ -93,11 +94,35 @@ namespace { |
93 | 94 |
|
94 | 95 | int main( int argc, char ** argv ) { |
95 | 96 | using namespace daw::json_to_cpp; |
96 | | - if( argc != 2 ) { |
97 | | - std::cerr << "Must supply a url or a file as the json source\n"; |
| 97 | + |
| 98 | + boost::program_options::options_description desc{ "Options" }; |
| 99 | + desc.add_options( ) |
| 100 | + ( "help", "print option descriptions" ) |
| 101 | + ( "in_file", boost::program_options::value<std::string>( ), "json source file path or url" ) |
| 102 | + ( "out_file", boost::program_options::value<std::string>( ), "output c++ file" ) |
| 103 | + ( "use_jsonlink", boost::program_options::value<bool>( )->default_value( true ), "Use JsonLink serializaion/deserialization" ); |
| 104 | + |
| 105 | + boost::program_options::variables_map vm; |
| 106 | + try { |
| 107 | + boost::program_options::store( boost::program_options::parse_command_line( argc, argv, desc ), vm ); |
| 108 | + |
| 109 | + if( vm.count( "help" ) ) { |
| 110 | + std::cout << "Command line options\n" << desc << std::endl; |
| 111 | + return EXIT_SUCCESS; |
| 112 | + } |
| 113 | + boost::program_options::notify( vm ); |
| 114 | + } catch( boost::program_options::error const & po_error ) { |
| 115 | + std::cerr << "ERROR: " << po_error.what( ) << '\n'; |
| 116 | + std::cerr << desc << std::endl; |
| 117 | + return EXIT_FAILURE; |
| 118 | + } |
| 119 | + |
| 120 | + if( !vm.count( "in_file" ) ) { |
| 121 | + std::cout << "Missing in_file parameter\n"; |
| 122 | + std::cout << "Command line options\n" << desc << std::endl; |
98 | 123 | exit( EXIT_FAILURE ); |
99 | 124 | } |
100 | | - std::string const uri{ argv[1] }; |
| 125 | + auto uri = vm["in_file"].as<std::string>( ); |
101 | 126 | std::string json_str; |
102 | 127 | if( is_url( uri ) ) { |
103 | 128 | auto tmp = download( uri ); |
@@ -109,17 +134,30 @@ int main( int argc, char ** argv ) { |
109 | 134 | } |
110 | 135 | } else { |
111 | 136 | std::ifstream in_file; |
112 | | - in_file.open( argv[1] ); |
| 137 | + in_file.open( uri ); |
113 | 138 | if( !in_file ) { |
114 | | - std::cerr << "Must supply valid json file on commandline\n"; |
| 139 | + std::cerr << "Could not open json in_file '" << uri << "'\n"; |
115 | 140 | exit( EXIT_FAILURE ); |
116 | 141 | } |
117 | 142 | std::copy( std::istream_iterator<char>{ in_file }, std::istream_iterator<char>{ }, std::back_inserter( json_str ) ); |
118 | 143 | in_file.close( ); |
119 | 144 | } |
120 | 145 | config_t config{ }; |
121 | | - generate_cpp( json_str, std::cout, config ); |
122 | | - std::cout << std::endl; |
| 146 | + config.enable_jsonlink = vm["use_jsonlink"].as<bool>( ); |
| 147 | + |
| 148 | + if( vm.count( "out_file" ) ) { |
| 149 | + std::ofstream out_file; |
| 150 | + auto const out_filename = vm["out_file"].as<std::string>( ); |
| 151 | + out_file.open( out_filename, std::ios::out | std::ios::trunc ); |
| 152 | + if( !out_file ) { |
| 153 | + std::cerr << "Could not open cpp out_file '" << out_filename << "'\n"; |
| 154 | + exit( EXIT_FAILURE ); |
| 155 | + } |
| 156 | + generate_cpp( json_str, out_file, config ); |
| 157 | + } else { |
| 158 | + generate_cpp( json_str, std::cout, config ); |
| 159 | + std::cout << std::endl; |
| 160 | + } |
123 | 161 |
|
124 | 162 | return EXIT_SUCCESS; |
125 | 163 | } |
|