Class: Melon::Commands::Base

Inherits:
Object
  • Object
show all
Includes:
Helpers
Defined in:
lib/melon/commands/base.rb

Direct Known Subclasses

Add, Check, Help, List, Remove, Show

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Helpers

#blockquote, #error, #expand_dir, #format_command, #recursively_expand, #resolve_symlinks, #wrap_text

Constructor Details

#initialize(args, options) ⇒ Base

Returns a new instance of Base.



11
12
13
14
# File 'lib/melon/commands/base.rb', line 11

def initialize(args, options)
  self.args = args
  self.options = options
end

Instance Attribute Details

#argsObject

Returns the value of attribute args.



8
9
10
# File 'lib/melon/commands/base.rb', line 8

def args
  @args
end

#descriptionObject (readonly)

Returns the value of attribute description.



9
10
11
# File 'lib/melon/commands/base.rb', line 9

def description
  @description
end

#optionsObject

Returns the value of attribute options.



8
9
10
# File 'lib/melon/commands/base.rb', line 8

def options
  @options
end

Class Method Details

.command_nameObject



16
17
18
# File 'lib/melon/commands/base.rb', line 16

def self.command_name
  self.to_s.split("::")[-1].downcase
end

.descriptionObject



53
54
55
# File 'lib/melon/commands/base.rb', line 53

def self.description
  raise
end

Instance Method Details

#helptextObject



49
# File 'lib/melon/commands/base.rb', line 49

def helptext; end

#parse_options!Object



58
59
60
61
62
63
64
65
66
67
# File 'lib/melon/commands/base.rb', line 58

def parse_options!
  begin
    parser.parse!(args)
  rescue OptionParser::ParseError => e
    error "#{self.class.command_name}: #{e}"
  end

  # verify remaining args are files - overrideable
  verify_args
end

#parserObject



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/melon/commands/base.rb', line 20

def parser
  @parser ||= OptionParser.new do |p|
    p.banner = usagebanner
    p.separator ""
    p.separator blockquote(self.class.description + ".", :margin => 0)
    p.separator ""

    if helptext
      p.separator blockquote(" " + helptext)
      p.separator ""
    end

    if self.respond_to? :parser_options
      p.separator "Options:"
      p.separator ""
      parser_options(p)
    end
  end
end

#runObject



51
# File 'lib/melon/commands/base.rb', line 51

def run; raise; end

#usageargsObject



48
# File 'lib/melon/commands/base.rb', line 48

def usageargs; end

#usagebannerObject



41
42
43
44
45
46
# File 'lib/melon/commands/base.rb', line 41

def usagebanner
  usage = "Usage: melon #{self.class.command_name}"
  usage << " [options]" if self.respond_to?(:parser_options)
  usage << ' ' << usageargs if usageargs
  usage
end

#verify_argsObject



69
70
71
72
73
# File 'lib/melon/commands/base.rb', line 69

def verify_args
  args.each do |arg|
    error "no such file: #{arg}" unless File.exists?(arg)
  end
end