Skip to content

Commit 18199a7

Browse files
committed
Added commandline processing
1 parent daabfa1 commit 18199a7

File tree

3 files changed

+48
-15
lines changed

3 files changed

+48
-15
lines changed

include/json_to_cpp.h

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -29,12 +29,11 @@
2929

3030
namespace daw {
3131
namespace json_to_cpp {
32-
struct config_t {
33-
bool enable_comments;
32+
struct config_t final {
3433
bool enable_jsonlink;
35-
config_t( );
36-
~config_t( );
3734

35+
config_t( ) = default;
36+
~config_t( ) = default;
3837
config_t( config_t const & ) = default;
3938
config_t( config_t && ) = default;
4039
config_t & operator=( config_t const & ) = default;

src/json_to_cpp.cpp

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -35,11 +35,7 @@
3535

3636
namespace daw {
3737
namespace json_to_cpp {
38-
config_t::config_t( ):
39-
enable_comments{ true },
40-
enable_jsonlink{ true } { }
4138

42-
config_t::~config_t( ) { }
4339
struct state_t {
4440
bool has_arrays;
4541
bool has_integrals;

src/main.cpp

Lines changed: 45 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
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

9495
int 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

Comments
 (0)