rdbstats  2.0.7
Options.cc
1 #include <cstdlib>
2 #include <string>
3 #include <vector>
4 #include <iostream>
5 #include <algorithm>
6 
7 #include <CLI/CLI.hpp>
8 
9 #include "RdbStats.h"
10 
11 #include "Options.h"
12 #include "config.h"
13 #include "usage.hpp"
14 
15 Options::Options( int argc, char* argv[] ) {
16 
17  CLI::App app( "Compute statistics on an rdb table" );
18 
19  try {
20 
21  app.add_option("-i,--input", input, "Input RDB file" )->check(CLI::ExistingFile);
22 
23  app.add_option("-o,--output", output, "Output RDB file" );
24 
25  auto opt_all = app.add_flag("-a,--all", all, "Generate statistics for all numerical columns" );
26  auto opt_columns = app.add_option( "columns", columns, "specific columns to process" );
27  opt_all->excludes(opt_columns);
28 
29  auto opt_group = app.add_option( "-g,--group,-b,--break", group, "group data by the specified columns" );
30  auto opt_rows = app.add_option( "-r,--rows", rows, "ranges of rows to operate on" );
31 
32  opt_group->excludes( opt_rows );
33 
34  auto normalize_map = std::map<std::string,Normalize>( {
35  { "ave", Normalize::Average },
36  { "med", Normalize::Median },
37  { "none", Normalize::None }
38  } );
39 
40  app.add_option( "-n,--normalize", normalize, "Normalize the output statistics" )
41  ->transform( CLI::Transformer( normalize_map ));
42 
43  app.add_option("-p,--percentiles", percentiles, "Generate percentile statistics" );
44 
45  app.add_flag( "-q,--quartiles", quartiles, "Generate quartile statistics" );
46 
47  app.add_option("-d,--defn", override, "override column definition" );
48 
49  app.add_flag( "--manual,--usage", manual, "output detailed help, then exit" );
50 
51  app.parse( argc, argv );
52 
53  if ( manual ) {
54  usage();
55  exit_requested = true;
56  exit_value = EXIT_SUCCESS;
57  }
58  }
59 
60  catch ( const CLI::ParseError &e ) {
61  exit_requested = true;
62  exit_value = app.exit(e);
63  }
64 
65 }
66 
67 void
68 Options::print( std::ostream& os ) {
69 
70  os << "columns: " << to_string( columns ) << "\n";
71 
72  os << "all: " << ( all ? "true" : "false" ) << "\n";
73 
74  os << "groups: " << to_string( group ) << "\n";
75 
76  os << "input: " << ( input.empty() ? "stdin" : input ) << "\n";
77  os << "output: " <<( output.empty() ? "stdout" : output ) << "\n";
78 
79  os << "normalize: " << to_string( normalize ) << "\n";
80 
81  os << "override: " << to_string( override ) << "\n";
82  os << "percentiles: " << percentiles << "\n";
83  os << "quartiles: " << (quartiles ? "true" : "false") << "\n";
84  os << "rows: " << to_string( rows ) << "\n";
85 }
86 
87 std::ostream& operator << ( std::ostream& os, Options& a ) {
88  a.print( os );
89  return os;
90 }
91 
92 std::string to_string( Normalize normalize ) {
93  return
94  normalize == Normalize::Average ? "average"
95  : normalize == Normalize::Median ? "median"
96  : normalize == Normalize::None ? "none"
97  : "UNKNOWN";
98 }