2222
2323#include < boost/algorithm/string/predicate.hpp>
2424#include < boost/optional.hpp>
25+ #include < boost/program_options.hpp>
2526#include < boost/utility/string_view.hpp>
2627#include < cstdlib>
2728#include < curl/curl.h>
@@ -93,11 +94,35 @@ namespace {
9394
9495int main ( int argc, char ** argv ) {
9596 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;
98123 exit ( EXIT_FAILURE );
99124 }
100- std::string const uri{ argv[ 1 ] } ;
125+ auto uri = vm[ " in_file " ]. as <std::string>( ) ;
101126 std::string json_str;
102127 if ( is_url ( uri ) ) {
103128 auto tmp = download ( uri );
@@ -109,17 +134,30 @@ int main( int argc, char ** argv ) {
109134 }
110135 } else {
111136 std::ifstream in_file;
112- in_file.open ( argv[ 1 ] );
137+ in_file.open ( uri );
113138 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 " ;
115140 exit ( EXIT_FAILURE );
116141 }
117142 std::copy ( std::istream_iterator<char >{ in_file }, std::istream_iterator<char >{ }, std::back_inserter ( json_str ) );
118143 in_file.close ( );
119144 }
120145 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+ }
123161
124162 return EXIT_SUCCESS;
125163}
0 commit comments