Skip to content

Commit 245e577

Browse files
committed
Treat hyphen output targets as stdout
1 parent da63dd8 commit 245e577

3 files changed

Lines changed: 77 additions & 19 deletions

File tree

‎spec/parser_spec.cr‎

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,4 +70,54 @@ describe YouPlot2::Parser do
7070
File.delete(pass_path) if File.exists?(pass_path)
7171
end
7272
end
73+
74+
it "treats hyphen output targets as stdout" do
75+
argv = ["line", "-o", "-", "-O", "-", "-w", "17"]
76+
77+
params = YouPlot2::Parameters.new
78+
options = YouPlot2::Options.new
79+
parser = YouPlot2::Parser.new(argv, params, options)
80+
81+
parser.parse
82+
83+
options.output.object_id.should eq(STDOUT.object_id)
84+
options.output_path.should be_nil
85+
options.pass.try(&.object_id).should eq(STDOUT.object_id)
86+
options.pass_path.should be_nil
87+
params.width.should eq(17)
88+
parser.input_files.should eq([] of String)
89+
end
90+
91+
it "treats long hyphen output targets as stdout" do
92+
argv = ["line", "--output", "-", "--pass", "-", "-w", "17"]
93+
94+
params = YouPlot2::Parameters.new
95+
options = YouPlot2::Options.new
96+
parser = YouPlot2::Parser.new(argv, params, options)
97+
98+
parser.parse
99+
100+
options.output.object_id.should eq(STDOUT.object_id)
101+
options.output_path.should be_nil
102+
options.pass.try(&.object_id).should eq(STDOUT.object_id)
103+
options.pass_path.should be_nil
104+
params.width.should eq(17)
105+
parser.input_files.should eq([] of String)
106+
end
107+
108+
it "keeps file output targets as paths" do
109+
argv = ["line", "-o", "plot.txt", "-O", "data.tsv", "input.csv"]
110+
111+
params = YouPlot2::Parameters.new
112+
options = YouPlot2::Options.new
113+
parser = YouPlot2::Parser.new(argv, params, options)
114+
115+
parser.parse
116+
117+
options.output.object_id.should eq(STDERR.object_id)
118+
options.output_path.should eq("plot.txt")
119+
options.pass.should be_nil
120+
options.pass_path.should eq("data.tsv")
121+
parser.input_files.should eq(["input.csv"])
122+
end
73123
end

‎src/youplot2/backends/unicode_plot.cr‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -227,7 +227,7 @@ module YouPlot2
227227

228228
private def parse_bar_values(raw_values : Array(String?)) : BarValues
229229
if raw_values.all? { |v| integer_literal?(v) }
230-
raw_values.map { |v| v.to_s.to_i64 }
230+
raw_values.map(&.to_s.to_i64)
231231
else
232232
raw_values.map { |v| to_f64_or_zero(v) }
233233
end

‎src/youplot2/parser.cr‎

Lines changed: 26 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -90,26 +90,14 @@ module YouPlot2
9090
end
9191

9292
private def add_common_options(opt : OptionParser)
93-
opt.on("-O", "--pass",
94-
"pass input to stdout for pipeline use") do
95-
@options.pass = STDOUT
96-
@options.pass_path = nil
97-
end
98-
opt.on("--pass FILE",
99-
"pass input to FILE for pipeline use") do |v|
100-
@options.pass = nil
101-
@options.pass_path = v
93+
opt.on("-O [FILE]", "--pass [FILE]",
94+
"pass input to stdout or FILE for pipeline use") do |v|
95+
set_pass_target(v)
10296
end
10397

104-
opt.on("-o", "--output",
105-
"write plot to stdout") do
106-
@options.output = STDOUT
107-
@options.output_path = nil
108-
end
109-
opt.on("--output FILE",
110-
"write plot to FILE (default: stderr)") do |v|
111-
@options.output = STDERR
112-
@options.output_path = v
98+
opt.on("-o [FILE]", "--output [FILE]",
99+
"write plot to stdout or FILE (default: stderr)") do |v|
100+
set_output_target(v)
113101
end
114102

115103
opt.on("-d", "--delimiter DELIM",
@@ -173,6 +161,26 @@ module YouPlot2
173161
end
174162
end
175163

164+
private def set_pass_target(value : String) : Nil
165+
if value.empty? || value == "-"
166+
@options.pass = STDOUT
167+
@options.pass_path = nil
168+
else
169+
@options.pass = nil
170+
@options.pass_path = value
171+
end
172+
end
173+
174+
private def set_output_target(value : String) : Nil
175+
if value.empty? || value == "-"
176+
@options.output = STDOUT
177+
@options.output_path = nil
178+
else
179+
@options.output = STDERR
180+
@options.output_path = value
181+
end
182+
end
183+
176184
private def add_subcommands
177185
add_barplot_commands
178186
add_count_commands

0 commit comments

Comments
 (0)