Sha256: ed80eb676ab725cd77fc2cab713fcf285d0b44dfd78e97c2510ecf81a97f527a

Contents?: true

Size: 1.87 KB

Versions: 33

Compression:

Stored size: 1.87 KB

Contents

# -*- coding: utf-8 -*-
#
# Copyright 2013 whiteleaf. All rights reserved.
#

require_relative "narou"
require_relative "command"
require_relative "helper"
require_relative "inventory"

module CommandLine
  module_function

  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

  #
  # exit を捕捉して終了コードを返す
  #
  def run!(argv)
    run(argv)
  rescue SystemExit => e
    e.status
  else
    0
  end

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

  #
  # 引数をスペース以外による区切り文字で展開する
  #
  def multiple_argument_extract(argv)
    delimiter = Inventory.load("local_setting")["multiple-delimiter"] || ","
    argv.map! { |arg|
      arg.split(delimiter)
    }.flatten!
  end
end

Version data entries

33 entries across 33 versions & 1 rubygems

Version Path
narou-3.2.0.1 lib/commandline.rb
narou-3.2.0 lib/commandline.rb
narou-3.1.11 lib/commandline.rb
narou-3.1.10 lib/commandline.rb
narou-3.1.9 lib/commandline.rb
narou-3.1.8 lib/commandline.rb
narou-3.1.7 lib/commandline.rb
narou-3.1.6 lib/commandline.rb
narou-3.1.5 lib/commandline.rb
narou-3.1.4 lib/commandline.rb
narou-3.1.3 lib/commandline.rb
narou-3.1.2 lib/commandline.rb
narou-3.1.1 lib/commandline.rb
narou-3.0.5.1 lib/commandline.rb
narou-3.0.5 lib/commandline.rb
narou-3.0.4 lib/commandline.rb
narou-3.0.3 lib/commandline.rb
narou-3.0.2 lib/commandline.rb
narou-3.0.1 lib/commandline.rb
narou-3.0.0 lib/commandline.rb