Class: Shift::CLI

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

Overview

The shifter command line interface.

Constant Summary collapse

USAGE =
"Usage:   shifter <file> [action] [format]\n" +
"In pipe: shifter - format [action]\n\n" +
"Available formats and actions:\n\n" +
Shift.inspect_actions

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(path, a = nil, b = nil) ⇒ CLI

Returns a new instance of CLI.



24
25
26
27
28
29
# File 'lib/shift/cli.rb', line 24

def initialize(path, a=nil, b=nil)
  @path = path
  @format, @action = stdin? ? [a,b] : [b,a]
  @format ||= :echo
  @action ||= :default
end

Class Method Details

.newObject



16
17
18
19
20
21
22
# File 'lib/shift/cli.rb', line 16

def self.new
  begin
    super(*ARGV)
  rescue ArgumentError
    abort USAGE
  end
end

Instance Method Details

#dataObject



35
36
37
# File 'lib/shift/cli.rb', line 35

def data
  stdin? ? STDIN.read : File.read(@path)
end

#run!Object



39
40
41
# File 'lib/shift/cli.rb', line 39

def run!
  puts Shift(data, @format).process(@action)
end

#stdin?Boolean

Returns:

  • (Boolean)


31
32
33
# File 'lib/shift/cli.rb', line 31

def stdin?
  @path == '-'
end