22#include < set>
33
44#include " simdjson.h"
5+ #ifndef __cpp_exceptions
6+ #define CXXOPTS_NO_EXCEPTIONS
7+ #endif
8+ #include " cxxopts.hpp"
59
610size_t count_nonasciibytes (const uint8_t *input, size_t length) {
711 size_t count = 0 ;
@@ -180,18 +184,37 @@ stat_t simdjson_compute_stats(const simdjson::padded_string &p) {
180184}
181185
182186int main (int argc, char *argv[]) {
183- int myoptind = 1 ;
184- if (myoptind >= argc) {
185- std::cerr << " Reads json, prints stats. " << std::endl;
186- std::cerr << " Usage: " << argv[0 ] << " <jsonfile>" << std::endl;
187- exit (1 );
187+ #ifdef __cpp_exceptions
188+ try {
189+ #endif
190+ std::string progName = " jsonstat" ;
191+ std::string progUsage = " Reads json, prints stats.\n " ;
192+ progUsage += argv[0 ];
193+ progUsage += " <jsonfile>" ;
194+
195+ cxxopts::Options options (progName, progUsage);
196+
197+ options.add_options ()
198+ (" h,help" , " Print usage." )
199+ (" f,file" , " File name." , cxxopts::value<std::string>())
200+ ;
201+
202+ options.parse_positional ({" file" });
203+ auto result = options.parse (argc, argv);
204+
205+ if (result.count (" help" )) {
206+ std::cerr << options.help () << std::endl;
207+ return EXIT_SUCCESS;
188208 }
189- const char *filename = argv[myoptind];
190- if (myoptind + 1 < argc) {
191- std::cerr << " warning: ignoring everything after " << argv[myoptind + 1 ]
192- << std::endl;
209+
210+ if (!result.count (" file" )) {
211+ std::cerr << " No filename specified." << std::endl;
212+ std::cerr << options.help () << std::endl;
213+ return EXIT_FAILURE;
193214 }
194215
216+ const char *filename = result[" file" ].as <std::string>().c_str ();
217+
195218 auto [p, error] = simdjson::padded_string::load (filename);
196219 if (error) {
197220 std::cerr << " Could not load the file " << filename << std::endl;
@@ -206,33 +229,33 @@ int main(int argc, char *argv[]) {
206229 // a JSON object and then to serialize it.
207230
208231 printf (R"( {
209- "integer_count" = %10zu,
210- "integer32_count" = %10zu,
211- "unsigned_integer32_count" = %10zu,
212- "unsigned_integer_count" = %10zu,
213- "float_count" = %10zu,
214- "string_count" = %10zu,
215- "string_byte_count" = %10zu,
216- "ascii_string_count" = %10zu,
217- "string_maximum_length" = %10zu,
218- "backslash_count" = %10zu,
219- "non_ascii_byte_count" = %10zu,
220- "object_count" = %10zu,
221- "maximum_object_size" = %10zu,
222- "array_count" = %10zu,
223- "maximum_array_size" = %10zu,
224- "null_count" = %10zu,
225- "true_count" = %10zu,
226- "false_count" = %10zu,
227- "byte_count" = %10zu,
228- "structural_indexes_count" = %10zu,
229- "key_count" = %10zu,
230- "ascii_key_count" = %10zu,
231- "key_maximum_length" = %10zu,
232- "key_distinct_count" = %10zu,
232+ "integer_count" = %10zu,
233+ "integer32_count" = %10zu,
234+ "unsigned_integer32_count" = %10zu,
235+ "unsigned_integer_count" = %10zu,
236+ "float_count" = %10zu,
237+ "string_count" = %10zu,
238+ "string_byte_count" = %10zu,
239+ "ascii_string_count" = %10zu,
240+ "string_maximum_length" = %10zu,
241+ "backslash_count" = %10zu,
242+ "non_ascii_byte_count" = %10zu,
243+ "object_count" = %10zu,
244+ "maximum_object_size" = %10zu,
245+ "array_count" = %10zu,
246+ "maximum_array_size" = %10zu,
247+ "null_count" = %10zu,
248+ "true_count" = %10zu,
249+ "false_count" = %10zu,
250+ "byte_count" = %10zu,
251+ "structural_indexes_count" = %10zu,
252+ "key_count" = %10zu,
253+ "ascii_key_count" = %10zu,
254+ "key_maximum_length" = %10zu,
255+ "key_distinct_count" = %10zu,
233256 "repeated_key_distinct_count"= %10zu,
234- "repeated_key_byte_count" = %10zu;
235- "maximum_depth" = %10zu
257+ "repeated_key_byte_count" = %10zu;
258+ "maximum_depth" = %10zu
236259}
237260)" ,
238261 s.integer_count ,s.integer32_count ,s.unsigned_integer32_count ,s.unsigned_integer_count ,
@@ -244,4 +267,10 @@ int main(int argc, char *argv[]) {
244267 s.ascii_key_count , s.key_maximum_length , s.all_keys .size (), s.repeated_keys .size (),
245268 s.repeated_key_byte_count , s.maximum_depth );
246269 return EXIT_SUCCESS;
247- }
270+ #ifdef __cpp_exceptions
271+ } catch (const cxxopts::OptionException& e) {
272+ std::cout << " error parsing options: " << e.what () << std::endl;
273+ return EXIT_FAILURE;
274+ }
275+ #endif
276+ }
0 commit comments