Module: Tidyup::Runner

Defined in:
lib/tidyup/runner.rb

Class Method Summary collapse

Class Method Details

.helpObject



42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/tidyup/runner.rb', line 42

def help
  maxn = options.transpose.first.map(&:size).max
  maxd = options.transpose.last .map(&:size).max
  "Usage: tidyup [file ...]\n" +
  options.map{ |(name, desc)|
    if desc.empty?
      name
    else
      sprintf("  %-*s  %-*s", maxn, name, maxd, desc)
    end
  }.join("\n")
end

.optionsObject



5
6
7
8
9
# File 'lib/tidyup/runner.rb', line 5

def options
  @options ||=
  [['-h, --help'       , 'Print this message'],
   ['-v, --version'    , 'Print the version' ]]
end

.parse(argv) ⇒ Object



22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/tidyup/runner.rb', line 22

def parse argv
  paths = []
  until argv.empty?
    case arg = argv.shift
    when /^-h/, '--help'
      puts(help)
      exit

    when /^-v/, '--version'
      require 'tidyup/version'
      puts(Tidyup::VERSION)
      exit

    else
      paths << arg
    end
  end
  paths
end

.run(argv = ARGV) ⇒ Object



11
12
13
14
15
16
17
18
19
20
# File 'lib/tidyup/runner.rb', line 11

def run argv=ARGV
  paths = parse(argv)
  require 'tidyup'
  input = if paths.empty?
            $stdin.read
          else
            paths.map{ |path| File.read(path) }.join(' ')
          end
  puts Tidyup.tidyup(input)
end