Class: SpecificHelp::Command

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

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(file, argv = []) ⇒ Command

Returns a new instance of Command.



14
15
16
17
18
19
20
21
# File 'lib/specific_help.rb', line 14

def initialize(file,argv=[])
  @source_file = file
  @help_cont = YAML.load(File.read(file))
  @help_cont[:head].each{|line| print line.chomp+"\n" }
  @help_cont[:license].each{|line| print line } if @help_cont[:license] != nil
  print "---\n" #separater
  @argv = argv
end

Class Method Details

.run(file, argv = []) ⇒ Object



10
11
12
# File 'lib/specific_help.rb', line 10

def self.run(file,argv=[])
  new(file, argv).execute
end

Instance Method Details

#disp(lines) ⇒ Object



64
65
66
67
68
# File 'lib/specific_help.rb', line 64

def disp(lines)
  lines.each{|line|
    puts "  #{line}"
  }
end

#disp_from_help_cont(key_word) ⇒ Object



70
71
72
73
74
# File 'lib/specific_help.rb', line 70

def disp_from_help_cont(key_word)
  items =@help_cont[key_word]
  puts items[:title]
  disp(items[:cont])
end

#edit_helpObject



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

def edit_help
  system("emacs #{@source_file}")
end

#executeObject



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/specific_help.rb', line 23

def execute
  @argv << '--help' if @argv.size==0
  command_parser = OptionParser.new do |opt|
    opt.on('-v', '--version','show program Version.') { |v|
      opt.version = MyHelp::VERSION
      puts opt.ver
    }
    @help_cont.each_pair{|key,val|
      next if key==:head or key==:license
      opts = val[:opts]
      opt.on(opts[:short],opts[:long],opts[:desc]) {disp_from_help_cont(key)}
    }
    opt.on('--edit','edit help contents'){edit_help}
    opt.on('--to_hiki','convert to hikidoc format'){to_hiki}
  end
#      begin
  command_parser.parse!(@argv)
#      rescue=> eval
#       p eval
#      end
  exit
end

#to_hikiObject



50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/specific_help.rb', line 50

def to_hiki
  puts '<<<'
  @help_cont.each_pair{|key,val|
    if key==:head or key==:license
      disp(val)
    else
      items =@help_cont[key]
      puts items[:title]
      disp(items[:cont])
    end
  }
  puts '>>>'
end