Class: Gonzui::CommandLineApplication

Inherits:
Object
  • Object
show all
Extended by:
Util
Includes:
Util
Defined in:
lib/gonzui/cmdapp/app.rb

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Util

assert, assert_equal, assert_equal_all, assert_non_nil, assert_not_reached, benchmark, command_exist?, commify, eprintf, format_bytes, program_name, protect_from_signals, require_command, set_verbosity, shell_escape, unix?, vprintf, windows?, wprintf

Constructor Details

#initializeCommandLineApplication

Returns a new instance of CommandLineApplication.



22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/gonzui/cmdapp/app.rb', line 22

def initialize
  @config = Config.new

  # ~/.gonzuirc has precedence over /etc/gonzuirc
  file_names = []
  file_names.push(File.join(ENV['HOME'], ".gonzuirc")) if ENV['HOME']
  file_names.push(File.join(SYSCONFDIR, "gonzuirc"))
  file_names.each {|file_name|
    if File.exist?(file_name)
      @config.load(file_name)
      break
    end
  }
end

Class Method Details

.startObject



166
167
168
169
170
171
172
173
# File 'lib/gonzui/cmdapp/app.rb', line 166

def self.start
  app = self.new
  begin
    app.start
  rescue IncompatibleDBError => e
    eprintf("%s", e.message)
  end
end

Instance Method Details

#be_quietObject



62
63
64
65
# File 'lib/gonzui/cmdapp/app.rb', line 62

def be_quiet
  devnull = if windows? then "NUL" else "/dev/null" end
  STDOUT.reopen(devnull)
end

#do_get_option_tableObject

Raises:

  • (NotImplementedError)


162
163
164
# File 'lib/gonzui/cmdapp/app.rb', line 162

def do_get_option_table
  raise NotImplementedError.new
end

#do_process_options(options) ⇒ Object

Raises:

  • (NotImplementedError)


158
159
160
# File 'lib/gonzui/cmdapp/app.rb', line 158

def do_process_options(options)
  raise NotImplementedError.new
end

#do_show_usageObject

Hook methods

Raises:

  • (NotImplementedError)


150
151
152
# File 'lib/gonzui/cmdapp/app.rb', line 150

def do_show_usage
  raise NotImplementedError.new
end

#do_startObject

Raises:

  • (NotImplementedError)


154
155
156
# File 'lib/gonzui/cmdapp/app.rb', line 154

def do_start
  raise NotImplementedError.new
end

#dump_configObject



57
58
59
60
# File 'lib/gonzui/cmdapp/app.rb', line 57

def dump_config
  @config.dump
  exit
end

#ensure_db_directory_availableObject



67
68
69
70
# File 'lib/gonzui/cmdapp/app.rb', line 67

def ensure_db_directory_available
  eprintf "#{@config.db_directory}: DB directory not found" unless
    File.directory?(@config.db_directory)
end

#init_loggerObject



132
133
134
# File 'lib/gonzui/cmdapp/app.rb', line 132

def init_logger
  @logger = Logger.new(@config.gonzui_log_file)
end

#init_logger_with_stderrObject



136
137
138
139
# File 'lib/gonzui/cmdapp/app.rb', line 136

def init_logger_with_stderr
  @logger = Logger.new(@config.gonzui_log_file)
  @logger.attach(STDERR)
end

#parse_optionsObject



114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
# File 'lib/gonzui/cmdapp/app.rb', line 114

def parse_options
  option_table = [
    ["--help",                GetoptLong::NO_ARGUMENT],
    ["--version",             GetoptLong::NO_ARGUMENT],
    ["--quiet",         "-q", GetoptLong::NO_ARGUMENT],
    ["--verbose",       "-v", GetoptLong::NO_ARGUMENT],
    ["--db-dir",        "-d", GetoptLong::REQUIRED_ARGUMENT],
    ["--db-cache",            GetoptLong::REQUIRED_ARGUMENT],
    ["--list-formats",        GetoptLong::NO_ARGUMENT],
    ["--dump-config",         GetoptLong::NO_ARGUMENT],
    ["--gonzuirc",            GetoptLong::REQUIRED_ARGUMENT]
  ]
  option_table.concat(do_get_option_table)
  options = parse_options_to_hash(option_table)
  process_common_options(options)
  do_process_options(options)
end

#parse_options_to_hash(option_table) ⇒ Object



37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/gonzui/cmdapp/app.rb', line 37

def parse_options_to_hash(option_table)
  begin
    options = {}
    parser = GetoptLong.new
    parser.set_options(*option_table)
    parser.quiet = true
    parser.each_option {|name, arg|
      options[name.sub(/^--/, "")] = arg
    }
    return options
  rescue => e
    eprintf("%s", e.message)
  end
end

#process_common_options(options) ⇒ Object



95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
# File 'lib/gonzui/cmdapp/app.rb', line 95

def process_common_options(options)
  show_version if options["version"]
  show_usage if options["help"]
  show_formats if options["list-formats"]
  @config.quiet = true if options["quiet"]
  @config.verbose = true if options["verbose"]
  @config.load(File.expand_path(options["gonzuirc"])) if 
    options["gonzuirc"]
  @config.db_directory = File.expand_path(options["db-dir"]) if
    options["db-dir"]
  @config.db_cache_size = (options["db-cache"].to_f * 1024 ** 2).to_i if
    options["db-cache"]

  UTF8.set_preference(@config.encoding_preference)
  set_verbosity(@config.verbose)
  be_quiet if @config.quiet
  dump_config if options["dump-config"]
end

#show_formatsObject



87
88
89
90
91
92
93
# File 'lib/gonzui/cmdapp/app.rb', line 87

def show_formats
  max = LangScan::modules.sort_by {|m| m.name.length }.last.name.length
  LangScan::modules.sort_by {|m| m.name }.each {|m|
    printf("%-#{max}s %s\n", m.name, m.abbrev)
  }
  exit
end

#show_usageObject



72
73
74
75
76
77
78
79
80
81
82
83
84
85
# File 'lib/gonzui/cmdapp/app.rb', line 72

def show_usage
  do_show_usage
  cache_size = @config.db_cache_size / 1024 ** 2
  puts "      --gonzuirc=FILE            specify alternate run control file"
  puts "      --dump-config              dump configuration"
  puts "  -d, --db-dir=DIR               use DB directory DIR"
  puts "      --db-cache=NUM             use NUM megabytes of DB cache [#{cache_size}]"
  puts "      --list-formats             list all supported formats"
  puts "      --help                     show this help message"
  puts "  -q, --quiet                    suppress all normal output"
  puts "  -v, --verbose                  output progress and statistics"
  puts "      --version                  print version information and exit"
  exit
end

#show_versionObject



52
53
54
55
# File 'lib/gonzui/cmdapp/app.rb', line 52

def show_version
  printf("%s %s\n", program_name, Gonzui::VERSION)
  exit
end

#startObject



141
142
143
144
145
146
# File 'lib/gonzui/cmdapp/app.rb', line 141

def start
  begin
    do_start
  rescue Interrupt, Errno::EPIPE
  end
end