Module: CommandLine

Extended by:
CommandLine
Included in:
CommandLine
Defined in:
lib/commandline.rb

Instance Method Summary collapse

Instance Method Details

#load_default_arguments(cmd) ⇒ Object



48
49
50
51
# File 'lib/commandline.rb', line 48

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

#multiple_argument_extract(argv) ⇒ Object

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



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

def multiple_argument_extract(argv)
  delimiter = Inventory.load("local_setting", :local)["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
# File 'lib/commandline.rb', line 14

def run(argv)
  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)
end