Skip to content

Commit 01bab0e

Browse files
committed
add -d flag for debug output on stderr
1 parent b6ce1d6 commit 01bab0e

17 files changed

+77
-48
lines changed

src/main.cpp

Lines changed: 27 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,17 @@
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.
1214
struct 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)
107114
void 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.
117131
int 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;
Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +0,0 @@
1-
reading from stdin
2-
running 1 expressions
3-
----> item = (item.unknown_method)

test/fixtures/test_basic_object_languages_select_stdout.txt

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,3 @@
1-
reading from stdin
2-
running 2 expressions
3-
----> item = (item["languages"])
4-
----> item = (item.select { |lang| %w[C++ Ruby].include?(lang) })
5-
printing item
61
[
72
"C++",
83
"Ruby"

test/fixtures/test_basic_object_languages_stdout.txt

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,3 @@
1-
reading from stdin
2-
running 1 expressions
3-
----> item = (item["languages"])
4-
printing item
51
[
62
"C++",
73
"Crystal",

test/fixtures/test_basic_object_return_object_stdout.txt

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,3 @@
1-
reading from stdin
2-
running 1 expressions
3-
----> item = ({ location: item["location"], short_location: item["location"].upcase[0..1] })
4-
printing item
51
{
62
"location": "Europe",
73
"short_location": "EU"
Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1 @@
1-
reading from stdin
2-
running 1 expressions
3-
----> item = (item["location"])
4-
printing item
51
"Europe"

test/fixtures/test_basic_object_stdout.txt

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,3 @@
1-
reading from stdin
2-
running 0 expressions
3-
printing item
41
{
52
"name": "Jyrki",
63
"location": "Europe",
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
rq: expression -d failed to run:
2+
(unknown):0: undefined method 'd' (NoMethodError)
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
reading from stdin
2+
running 2 expressions
3+
----> item = (item.first)
4+
----> item = ({ one: item["foo"], two: item.dig("bar", "name") })
5+
printing item
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
Usage: rq [options] [--] [EXPRESSION...]
2+
-d extra debug output
23
-v print the version number
34
-h show this message

0 commit comments

Comments
 (0)