Class: Munger::CLI

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

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(*args) ⇒ CLI

Returns a new instance of CLI.



11
12
13
14
# File 'lib/munger/cli.rb', line 11

def initialize(*args)
  @args = args
  parse_args
end

Class Method Details

.run!(*args) ⇒ Object



7
8
9
# File 'lib/munger/cli.rb', line 7

def self.run!(*args)
  return self.new(*args).run
end

Instance Method Details

#parse_argsObject



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/munger/cli.rb', line 16

def parse_args
  @content = OptionParser.new do |opts|
    opts.banner = "Usage: munge <options> CONTENT"
    opts.on("--before PATTERN", "Insert before the line matching PATTERN") {|@pattern| set_mode(:before) }
    opts.on("--after PATTERN", "Insert after the line matching PATTERN") {|@pattern| set_mode(:after) }
    opts.on("--replace PATTERN", "Replace the line matching PATTERN") {|@pattern| set_mode(:replace) }
    opts.on("--append", "Append to the file") { set_mode(:append) }
    opts.on("--replace-or-append PATTERN", "Replace the line matching PATTERN, or append if not found") {|@pattern| set_mode(:replace_or_append) }

    opts.on("--tag TAG", "Mark our insertion with this TAG") {|@tag|}
    opts.on("--input INPUT", "The filename to read; '-' for stdin") \
            {|@input_path|}
    opts.on("--output OUTPUT", "The filename to write; '-' for stdout") {|@output_path|}
    opts.on("--content CONTENT", "A file from which to read the content to insert; '-' for stdin") \
            {|@content_path|}
    opts.on("-v", "--verbose", "Increase verbosity") {|@verbose|}
  end.parse!(@args).first

  set_mode(:append) unless @mode
  abort "No --input path specified" unless @input_path
  abort "No --output path specified" unless @output_path
  abort "Can't specify --content with CONTENT" if (@content != nil && @content_path != nil)
  abort "No content specified" unless (@content || @content_path)

  @options = Munger::Munge::OPTIONS.inject({}) do |h, option|
    h[option] = instance_variable_get("@#{option}") rescue nil
    h
  end
end

#runObject



46
47
48
# File 'lib/munger/cli.rb', line 46

def run
  Munger::munge(@options)
end

#set_mode(new_mode) ⇒ Object



50
51
52
53
# File 'lib/munger/cli.rb', line 50

def set_mode(new_mode)
  abort("Only one --before|--after|--replace|--append allowed") if @mode
  @mode = new_mode
end