Module: CommandLine

Defined in:
lib/commandline.rb

Class Method Summary collapse

Class Method Details

.load_default_arguments(cmd) ⇒ Object



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

def load_default_arguments(cmd)
  default_arguments_list = Inventory.load("local_setting")
  (default_arguments_list["default_args.#{cmd}"] || "").split
end

.multiple_argument_extract(argv) ⇒ Object

引数をスペース以外による区切り文字で展開する



72
73
74
75
76
77
# File 'lib/commandline.rb', line 72

def multiple_argument_extract(argv)
  delimiter = Inventory.load("local_setting")["multiple-delimiter"] || ","
  argv.map! { |arg|
    arg.split(delimiter)
  }.flatten!
end

.run(argv) ⇒ Object



14
15
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
45
46
47
48
49
50
51
# File 'lib/commandline.rb', line 14

def run(argv)
  argv.flatten!
  if Helper.os_windows?
    argv.map! do |arg|
      arg.encode(Encoding::UTF_8)
    end
  end
  argv.unshift("help") if argv.empty?
  arg = argv.shift.downcase
  arg = Command::Shortcuts[arg] || arg
  arg = "help" if arg == "-h" || arg == "--help"
  arg = "version" if arg == "-v" || arg == "--version"
  unless Narou.already_init?
    unless ["help", "version", "init"].include?(arg)
      arg = "help"
    end
  end
  unless Command.get_list.include?(arg)
    error "不明なコマンドです"
    puts
    arg = "help"
  end
  if argv.empty? && STDIN.tty?
    argv += load_default_arguments(arg)
  end
  if argv.delete("--multiple")
    multiple_argument_extract(argv)
  end
  unless STDIN.tty?
    # pipeで接続された場合、標準入力からIDリストを受け取って引数に繋げる
    argv += (STDIN.gets || "").split
  end
  Command.get_list[arg].new.execute(argv)
ensure
  if Command::Convert.exists_sending_error_list?
    Command::Convert.display_sending_error_list
  end
end

.run!(argv) ⇒ Object

exit を捕捉して終了コードを返す



56
57
58
59
60
61
62
# File 'lib/commandline.rb', line 56

def run!(argv)
  run(argv)
rescue SystemExit => e
  e.status
else
  0
end