@@ -11,27 +11,29 @@ namespace po = boost::program_options;
1111using namespace std ;
1212bool continue_reading = true ;
1313
14- void handle_printing (Summarizer *summarizer, int delay)
14+ void handle_printing (Summarizer *summarizer, int delay, int precision )
1515{
16- printElements (vector<string>{" Average" , " Min" , " Median" , " p95" , " p99" , " Max" , " \n " });
16+ printElements (vector<string>{" Average" , " Min" , " Median" , " p95" , " p99" , " Max" , " \n " }, precision );
1717 while (true )
1818 {
1919 cout << " \r " ;
20- summarizer->print_summery ();
20+ summarizer->print_summery (precision );
2121 this_thread::sleep_for (chrono::seconds (delay));
22- if (continue_reading == false ){
23- cout<<endl;
22+ if (continue_reading == false )
23+ {
24+ cout << endl;
2425 break ;
2526 }
2627 }
2728}
2829
29- void start (int delay) {
30+ void start (int delay, int precision)
31+ {
3032 int number;
3133 string line;
3234 Summarizer summarizer;
3335
34- thread timer (handle_printing, &summarizer, delay);
36+ thread timer (handle_printing, &summarizer, delay, precision );
3537
3638 while (continue_reading)
3739 {
@@ -52,49 +54,60 @@ void start(int delay) {
5254 timer.join ();
5355}
5456
55- int main (int ac, char * av[])
57+ int main (int ac, char * av[])
5658{
57- try {
59+ int delay, precision;
60+
61+ try
62+ {
5863 po::options_description desc (" Allowed options" );
5964 desc.add_options ()
6065 (" help,h" , " produce help message" )
6166 (" file,f" , po::value<string>(), " read input from a file" )
62- (" delay,d" , po::value<int >(), " delay time between re-calculating" )
67+ (" delay,d" , po::value<int >(&delay)->default_value (1 ), " delay time between re-calculating" )
68+ (" precision,p" , po::value<int >(&precision)->default_value (2 ), " control the precision parameter" )
6369 ;
6470
65- po::variables_map vm;
66- po::store (po::parse_command_line (ac, av, desc), vm);
67- po::notify (vm);
71+ po::positional_options_description p;
72+ p.add (" file" , 1 );
73+
74+ po::variables_map vm;
75+ po::store (po::command_line_parser (ac, av).
76+ options (desc).positional (p).run (), vm);
77+ po::notify (vm);
6878
69- if (vm.count (" help" )) {
79+ if (vm.count (" help" ))
80+ {
81+ cout << " summarizer - Summarize a stream of numbers by printing some aggregation functions every specified interval\n " ;
82+ cout << " Usage: summarizer [file_name] [options]\n " ;
7083 cout << desc << " \n " ;
7184 return 0 ;
7285 }
7386
74- if (vm.count (" file" )) {
87+ if (vm.count (" file" ))
88+ {
7589 string file_name = vm[" file" ].as <string>();
7690 // close the stdin so when we open the file it take the lowest
7791 // available file descriptor which is 0
7892 close (0 );
7993 int fd = open (file_name.c_str (), 0 );
80- if (fd < 0 ) {
81- cout<<" cat: cannot open " <<file_name<<endl;
94+ if (fd < 0 )
95+ {
96+ cout << " cat: cannot open " << file_name << endl;
8297 exit (1 );
8398 }
84-
85- }
86- int delay = 1 ;
87- if (vm.count (" delay" )) {
88- delay = vm[" delay" ].as <int >();
8999 }
90- start (delay);
91- } catch (exception& e) {
100+ start (delay, precision);
101+ }
102+ catch (exception &e)
103+ {
92104 cerr << " Error: " << e.what () << " \n " ;
93105 return 1 ;
94106 }
95- catch (...) {
107+ catch (...)
108+ {
96109 cerr << " Exception of unknown type!\n " ;
97110 }
98-
111+
99112 return 0 ;
100113}
0 commit comments