Skip to content

Commit 1b66603

Browse files
committed
Changes the default value to stdout if no argument is specified with the -o option
#9
1 parent b368dc4 commit 1b66603

2 files changed

Lines changed: 36 additions & 20 deletions

File tree

lib/youplot/command/parser.rb

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -35,12 +35,13 @@ def create_default_parser
3535
opt.on_tail('') # Add a blank line at the end
3636
opt.separator('')
3737
opt.on('Common options:')
38-
opt.on('-O', '--pass [VAL]', 'file to output standard input data to [stdout]',
38+
opt.on('-O', '--pass [FILE]', 'file to output input data to [stdout]',
3939
'for inserting YouPlot in the middle of Unix pipes') do |v|
4040
@options[:pass] = v || $stdout
4141
end
42-
opt.on('-o', '--output VAL', 'file to output results to [stderr]') do |v|
43-
@options[:output] = v
42+
opt.on('-o', '--output [FILE]', 'file to output plots to [stdout]',
43+
'If no option is specified, plot will print to stderr') do |v|
44+
@options[:output] = v || $stdout
4445
end
4546
opt.on('-d', '--delimiter VAL', String, 'use DELIM instead of TAB for field delimiter') do |v|
4647
@options[:delimiter] = v

test/youplot/command_test.rb

Lines changed: 32 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -7,23 +7,27 @@ class YouPlotCommandTest < Test::Unit::TestCase
77
class << self
88
def startup
99
@stdin = $stdin.dup
10+
@stdout = $stdout.dup
1011
@stderr = $stderr.dup
1112
end
1213

1314
def shutdown
1415
$stdin = @stdin
16+
$stdout = @stdout
1517
$stderr = @stderr
1618
end
1719
end
1820

1921
def setup
2022
$stdin = File.open(File.expand_path('../fixtures/iris.csv', __dir__), 'r')
21-
@tmp_file = Tempfile.new
22-
$stderr = @tmp_file
23+
@stderr_file = Tempfile.new
24+
@stdout_file = Tempfile.new
25+
$stderr = @stderr_file
26+
$stdout = @stdout_file
2327
end
2428

2529
def teardown
26-
@tmp_file.close
30+
@stderr_file.close
2731
end
2832

2933
def fixture(fname)
@@ -32,71 +36,82 @@ def fixture(fname)
3236

3337
test :bar do
3438
YouPlot::Command.new(['bar', '-H', '-d,', '-t', 'IRIS-BARPLOT']).run
35-
assert_equal fixture('iris-barplot.txt'), @tmp_file.read
39+
assert_equal fixture('iris-barplot.txt'), @stderr_file.read
3640
end
3741

3842
test :barplot do
3943
YouPlot::Command.new(['barplot', '-H', '-d,', '-t', 'IRIS-BARPLOT']).run
40-
assert_equal fixture('iris-barplot.txt'), @tmp_file.read
44+
assert_equal fixture('iris-barplot.txt'), @stderr_file.read
4145
end
4246

4347
test :hist do
4448
YouPlot::Command.new(['hist', '-H', '-d,', '-t', 'IRIS-HISTOGRAM']).run
45-
assert_equal fixture('iris-histogram.txt'), @tmp_file.read
49+
assert_equal fixture('iris-histogram.txt'), @stderr_file.read
4650
end
4751

4852
test :histogram do
4953
YouPlot::Command.new(['histogram', '-H', '-d,', '-t', 'IRIS-HISTOGRAM']).run
50-
assert_equal fixture('iris-histogram.txt'), @tmp_file.read
54+
assert_equal fixture('iris-histogram.txt'), @stderr_file.read
5155
end
5256

5357
test :line do
5458
YouPlot::Command.new(['line', '-H', '-d,', '-t', 'IRIS-LINEPLOT']).run
55-
assert_equal fixture('iris-lineplot.txt'), @tmp_file.read
59+
assert_equal fixture('iris-lineplot.txt'), @stderr_file.read
5660
end
5761

5862
test :lineplot do
5963
YouPlot::Command.new(['lineplot', '-H', '-d,', '-t', 'IRIS-LINEPLOT']).run
60-
assert_equal fixture('iris-lineplot.txt'), @tmp_file.read
64+
assert_equal fixture('iris-lineplot.txt'), @stderr_file.read
6165
end
6266

6367
test :lines do
6468
YouPlot::Command.new(['lines', '-H', '-d,', '-t', 'IRIS-LINEPLOTS']).run
65-
assert_equal fixture('iris-lineplots.txt'), @tmp_file.read
69+
assert_equal fixture('iris-lineplots.txt'), @stderr_file.read
6670
end
6771

6872
test :lineplots do
6973
YouPlot::Command.new(['lineplots', '-H', '-d,', '-t', 'IRIS-LINEPLOTS']).run
70-
assert_equal fixture('iris-lineplots.txt'), @tmp_file.read
74+
assert_equal fixture('iris-lineplots.txt'), @stderr_file.read
7175
end
7276

7377
test :s do
7478
YouPlot::Command.new(['s', '-H', '-d,', '-t', 'IRIS-SCATTER']).run
75-
assert_equal fixture('iris-scatter.txt'), @tmp_file.read
79+
assert_equal fixture('iris-scatter.txt'), @stderr_file.read
7680
end
7781

7882
test :scatter do
7983
YouPlot::Command.new(['scatter', '-H', '-d,', '-t', 'IRIS-SCATTER']).run
80-
assert_equal fixture('iris-scatter.txt'), @tmp_file.read
84+
assert_equal fixture('iris-scatter.txt'), @stderr_file.read
8185
end
8286

8387
test :d do
8488
YouPlot::Command.new(['d', '-H', '-d,', '-t', 'IRIS-DENSITY']).run
85-
assert_equal fixture('iris-density.txt'), @tmp_file.read
89+
assert_equal fixture('iris-density.txt'), @stderr_file.read
8690
end
8791

8892
test :density do
8993
YouPlot::Command.new(['density', '-H', '-d,', '-t', 'IRIS-DENSITY']).run
90-
assert_equal fixture('iris-density.txt'), @tmp_file.read
94+
assert_equal fixture('iris-density.txt'), @stderr_file.read
9195
end
9296

9397
test :box do
9498
YouPlot::Command.new(['box', '-H', '-d,', '-t', 'IRIS-BOXPLOT']).run
95-
assert_equal fixture('iris-boxplot.txt'), @tmp_file.read
99+
assert_equal fixture('iris-boxplot.txt'), @stderr_file.read
96100
end
97101

98102
test :boxplot do
99103
YouPlot::Command.new(['boxplot', '-H', '-d,', '-t', 'IRIS-BOXPLOT']).run
100-
assert_equal fixture('iris-boxplot.txt'), @tmp_file.read
104+
assert_equal fixture('iris-boxplot.txt'), @stderr_file.read
105+
end
106+
107+
test :plot_output_stdout do
108+
YouPlot::Command.new(['bar', '-o', '-H', '-d,', '-t', 'IRIS-BARPLOT']).run
109+
assert_equal "", @stderr_file.read
110+
end
111+
112+
test :data_output_stdout do
113+
YouPlot::Command.new(['bar', '-O', '-H', '-d,', '-t', 'IRIS-BARPLOT']).run
114+
assert_equal fixture('iris-barplot.txt'), @stderr_file.read
115+
assert_equal File.read(File.expand_path('../fixtures/iris.csv', __dir__)), @stdout_file.read
101116
end
102117
end

0 commit comments

Comments
 (0)