Skip to content

Commit b6975cd

Browse files
committed
add more control from the command line options
1 parent db531c4 commit b6975cd

File tree

5 files changed

+50
-37
lines changed

5 files changed

+50
-37
lines changed

main.cpp

Lines changed: 39 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -11,27 +11,29 @@ namespace po = boost::program_options;
1111
using namespace std;
1212
bool 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
}

modules/summarizer.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ void Summarizer::add_number(double number)
1717
numbers.insert(number);
1818
}
1919

20-
void Summarizer::print_summery()
20+
void Summarizer::print_summery(int precision)
2121
{
2222
double count = numbers.size();
2323
double average = sum / count;
@@ -37,5 +37,5 @@ void Summarizer::print_summery()
3737
// maximum exists at the end of the set: 0+50+45+4+1 = 100
3838
std::advance(it, 0.01 * count);
3939
double maximum = *it;
40-
printElements(vector<double>{average, min, median, p95, p99, maximum});
40+
printElements(vector<double>{average, min, median, p95, p99, maximum}, precision);
4141
}

modules/summarizer.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ class Summarizer
1111
public:
1212
Summarizer();
1313
void add_number(double number);
14-
void print_summery();
14+
void print_summery(int precision);
1515
};
1616

1717
#endif

utils/utils.cpp

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,22 +7,22 @@
77
using namespace std;
88

99
template <typename T>
10-
void printElement(T t)
10+
void printElement(T t, int precision)
1111
{
12-
cout << left << setw(10) << fixed << setprecision(2) << t;
12+
cout << left << setw(10) << fixed << setprecision(precision) << t;
1313
}
1414

1515

1616
template<typename T>
17-
void printElements(vector<T> elements)
17+
void printElements(vector<T> elements, int precision)
1818
{
1919
for( auto element : elements )
2020
{
21-
printElement(element);
21+
printElement(element, precision);
2222
}
2323
}
24-
template void printElements(vector<double> elements);
25-
template void printElements(vector<string> elements);
24+
template void printElements(vector<double> elements, int precision);
25+
template void printElements(vector<string> elements, int precision);
2626

2727

2828

utils/utils.hpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,10 @@
77
using namespace std;
88

99
template <typename T>
10-
void printElement(T t);
10+
void printElement(T t, int precision);
1111

1212
template<typename T>
13-
void printElements(vector<T> elements);
13+
void printElements(vector<T> elements, int precision);
1414

1515
bool is_number(const string &s);
1616

0 commit comments

Comments
 (0)