In a blog post about unconditional programming it recommends writing code that limits if statements.
How do I handle command-line switches without if statements?
For example using OptionParser I made a cat clone that will upcase the stream if the --upcase
switch is set:
#!/usr/bin/env ruby
require 'optparse'
options = {}
OptionParser.new do |opts|
opts.banner = "Concatenate files or read from stdin\nUsage: argf_options_parser [options] [file ...]"
opts.on("-u", "--upcase", "Upcase stream") do
options[:upcase] = true
end
end.parse!
if options[:upcase]
puts ARGF.read.upcase
else
puts ARGF.read
end
How would I handle that switch without an if..else
block?
Also looking for general strategies for writing unconditional code.
Aucun commentaire:
Enregistrer un commentaire