Module: CommandLine

Defined in:
lib/commandline.rb

Class Method Summary collapse

Class Method Details

.argv_for_windows(argv) ⇒ Object



51
52
53
54
55
56
# File 'lib/commandline.rb', line 51

def argv_for_windows(argv)
  return unless Helper.os_windows?
  argv.map! do |arg|
    arg.class == Integer ? arg : arg&.encode(Encoding::UTF_8)
  end
end

.load_default_arguments(cmd) ⇒ Object



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

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

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



91
92
93
94
95
96
# File 'lib/commandline.rb', line 91

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

.proc_default_arguments(argv, name) ⇒ Object



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

def proc_default_arguments(argv, name)
  if argv.empty? && STDIN.tty?
    argv.concat(load_default_arguments(name))
  end
end

.run(*argv, catch_exit: false, io: $stdout) ⇒ Object



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/commandline.rb', line 15

def run(*argv, catch_exit: false, io: $stdout)
  argv.flatten!
  argv_for_windows(argv)
  cmd_name = take_command_name(argv)
  proc_default_arguments(argv, cmd_name)
  if argv.delete("--multiple")
    multiple_argument_extract(argv)
  end
  unless STDIN.tty?
    # pipeで接続された場合、標準入力からIDリストを受け取って引数に繋げる
    argv += (STDIN.gets || "").split
  end
  command = Command.get_list[cmd_name]
  if catch_exit
    command.execute!(argv, io: io)
  else
    cmd = command.new
    cmd.stream_io = io
    cmd.execute(argv)
  end
ensure
  Command::Convert.display_sending_error_list
end

.run!(*argv, io: $stdout) ⇒ Object

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



42
43
44
# File 'lib/commandline.rb', line 42

def run!(*argv, io: $stdout)
  run(*argv, catch_exit: true, io: io)
end

.take_command_name(argv) ⇒ Object



58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
# File 'lib/commandline.rb', line 58

def take_command_name(argv)
  argv.unshift("help") if argv.empty?
  name = argv.shift.downcase
  name = Command::Shortcuts[name] || name
  name = case name
         when "-v", "--version"
           "version"
         when "-h", "--help"
           "help"
         else
           name
         end
  unless Narou.already_init?
    unless %w(help version init).include?(name)
      name = "help"
    end
  end
  unless Command.get_list.include?(name)
    error "不明なコマンドです。narou help を確認してください"
    exit Narou::EXIT_ERROR_CODE
  end
  name
end