Class: MyHelp::Command

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

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(argv = []) ⇒ Command

Returns a new instance of Command.



16
17
18
19
20
# File 'lib/my_help.rb', line 16

def initialize(argv=[])
  @argv = argv
  p @target_dir = File.expand_path("../../lib/daddygongon", __FILE__)
  p @argv
end

Class Method Details

.run(argv = []) ⇒ Object



12
13
14
# File 'lib/my_help.rb', line 12

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

Instance Method Details

#clean_exeObject



66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
# File 'lib/my_help.rb', line 66

def clean_exe
  p entries=Dir.entries(@target_dir)[2..-1]
  entries.each{|file|
    p file
    next if file[0]=='#' or file[-1]=='~'
    p file_name=file.split('_')
    target_files = [file, file_name[0][0]+"_"+file_name[1][0]]
    target = File.join(@target_dir,file)
    target_files.each{|name|
      print "\n"
      p target=File.join('exe',name)
      FileUtils::Verbose.rm(target)
    }
  }
end

#edit_help(file) ⇒ Object



88
89
90
91
# File 'lib/my_help.rb', line 88

def edit_help(file)
  p target_help=File.join(@target_dir,file)
  system "emacs #{target_help}"
end

#executeObject



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

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
    }
    opt.on('-l', '--list', '個別(specific)ヘルプのList表示.'){list_helps}
    opt.on('-e NAME', '--edit NAME', 'NAME(例:test_help)をEdit編集.'){|file| edit_help(file)}
    opt.on('-i NAME', '--init NAME', 'NAME(例:test_help)のtemplateを作成.'){|file| init_help(file)}
    opt.on('-m', '--make', 'make and install:local all helps.'){make_help}
    opt.on('-c', '--clean', 'clean up exe dir.'){clean_exe}
  end
  begin
    command_parser.parse!(@argv)
  rescue=> eval
    p eval
  end
  exit
end

#init_help(file) ⇒ Object



82
83
84
85
86
# File 'lib/my_help.rb', line 82

def init_help(file)
  p target_help=File.join(@target_dir,file)
  p template = File.join(File.dirname(@target_dir),'my_help','template_help')
  FileUtils::Verbose.cp(template,target_help)
end

#list_helpsObject



93
94
95
96
97
98
# File 'lib/my_help.rb', line 93

def list_helps
  print "Specific help file:\n"
  Dir.entries(@target_dir)[2..-1].each{|file|
    print "  "+file+"\n"
  }
end

#make_helpObject



43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/my_help.rb', line 43

def make_help
  p entries=Dir.entries(@target_dir)[2..-1]
  entries.each{|file|
    p file
    next if file[0]=='#' or file[-1]=='~'
    exe_cont="#!/usr/bin/env ruby\nrequire 'specific_help'\n"
    p file_name=file.split('_')
    target_files = [file, file_name[0][0]+"_"+file_name[1][0]]
    target = File.join(@target_dir,file)
    exe_cont << "help_file = '#{target}'\n"
    exe_cont << "SpecificHelp::Command.run(help_file, ARGV)\n"
    target_files.each{|name|
      print "\n"
      p target=File.join('exe',name)
      File.open(target,'w'){|file|
        print exe_cont
        file.print exe_cont
      }
      FileUtils.chmod('a+x', target, :verbose => true)
    }
  }
end