Class: Charwidth::CLI

Inherits:
Object
  • Object
show all
Defined in:
lib/charwidth/cli.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.run(argv) ⇒ Object



4
5
6
# File 'lib/charwidth/cli.rb', line 4

def self.run(argv)
  new.run(argv)
end

Instance Method Details

#run(argv) ⇒ Object



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/charwidth/cli.rb', line 8

def run(argv)
  options = {}
  optparse = OptionParser.new do |o|
    o.banner += " TEXT\nNormalize character width in arguments or STDIN.\nAvailable character types are #{Charwidth::ClassMethods::TYPES.join(",")}."
    o.on("--only=TYPES", "Comma separated character types that should be converted") {|types| options[:only] = types.split(",").map(&:to_sym) }
    o.on("--except=TYPES", "Comma separated character types that should not be converted") {|types| options[:except] = types.split(",").map(&:to_sym) }
    o.parse!(argv)
  end
  src = nil
  if ARGV.empty?
    if $stdin.tty?
      warn optparse.banner
      warn optparse.help
      exit 1
    else
      src = $stdin.read
    end
  else
    src = ARGV.join(" ")
  end
  print Charwidth.normalize(src, options)
end