88#include " expressionwrapper.h"
99#include " mruby++.h"
1010
11+ static std::ostream* Debug = &std::cerr;
12+
1113// ! \brief Parses and represents command line arguments.
1214struct ProgramOptions
1315{
1416 // ! \brief Parse command line arguments and return a new \c ProgramOptions struct.
1517 // ! \exception std::runtime_error when command line argument parsing fails.
1618 ProgramOptions (int argc, char ** argv);
1719
20+ // ! `-d`: \c true if you want debug output
21+ bool debug = false ;
1822 // ! `-h`: \c true if the program should exit with its usage info.
1923 bool help = false ;
2024 // ! `-v`: \c true if the program should exit with its version info.
@@ -73,6 +77,9 @@ ProgramOptions::ProgramOptions(int argc, char** argv)
7377 {
7478 switch (*argit)
7579 {
80+ case ' d' :
81+ debug = true ;
82+ break ;
7683 case ' h' :
7784 help = true ;
7885 break ;
@@ -107,15 +114,26 @@ void print_version(std::ostream& stream = std::cout)
107114void print_usage (std::ostream& stream = std::cout)
108115{
109116 stream << R"( Usage: rq [options] [--] [EXPRESSION...]
117+ -d extra debug output
110118 -v print the version number
111119 -h show this message)" << std::endl;
112120}
113121
122+ class NullBuffer : public std ::streambuf
123+ {
124+ public:
125+ int overflow (int c) { return c; }
126+ };
127+
114128// ! \brief Main entry point.
115129// ! \param argc Number of arguments.
116130// ! \param argv List of arguments.
117131int main (int argc, char ** argv)
118132{
133+ // set up null buffer for debug logging
134+ NullBuffer null_buffer;
135+ std::ostream null_stream (&null_buffer);
136+
119137 try
120138 {
121139 ProgramOptions opts (argc, argv);
@@ -131,9 +149,14 @@ int main(int argc, char** argv)
131149 return EXIT_SUCCESS;
132150 }
133151
152+ if (!opts.debug )
153+ {
154+ Debug = &null_stream;
155+ }
156+
134157 MRuby rb;
135158
136- std::cout << " reading from stdin" << std::endl;
159+ *Debug << " reading from stdin" << std::endl;
137160 if (!rb.eval (" item = JSON.parse(STDIN.read)" ))
138161 {
139162 std::cerr << " rq: read from stdin failed:" << std::endl;
@@ -142,11 +165,11 @@ int main(int argc, char** argv)
142165 return EXIT_FAILURE;
143166 }
144167
145- std::cout << " running " << opts.expressions .size () << " expressions" << std::endl;
168+ *Debug << " running " << opts.expressions .size () << " expressions" << std::endl;
146169 for (const auto & expr : opts.expressions )
147170 {
148171 auto wrapped_expr = ExpressionWrapper::wrap (expr);
149- std::cout << " ----> " << wrapped_expr << std::endl;
172+ *Debug << " ----> " << wrapped_expr << std::endl;
150173 if (!rb.eval (wrapped_expr))
151174 {
152175 std::cerr << " rq: expression " << expr << " failed to run:" << std::endl;
@@ -156,7 +179,7 @@ int main(int argc, char** argv)
156179 }
157180 }
158181
159- std::cout << " printing item" << std::endl;
182+ *Debug << " printing item" << std::endl;
160183 if (!rb.eval (" puts JSON.generate(item, pretty_print: true, indent_width: 2)" ))
161184 {
162185 std::cerr << " rq: printing item failed" << std::endl;
0 commit comments