Sha256: 55aae4cc04eb1c5d89ee6d3903581aaa14cb084cd3908b68120de9e24b171533

Contents?: true

Size: 1.88 KB

Versions: 18

Compression:

Stored size: 1.88 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", :local)
    (default_arguments_list["default_args.#{cmd}"] || "").split
  end

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

Version data entries

18 entries across 18 versions & 1 rubygems

Version Path
narou-2.6.1 lib/commandline.rb
narou-2.6.0 lib/commandline.rb
narou-2.5.2 lib/commandline.rb
narou-2.5.1 lib/commandline.rb
narou-2.4.2 lib/commandline.rb
narou-2.4.1 lib/commandline.rb
narou-2.4.0 lib/commandline.rb
narou-2.3.3 lib/commandline.rb
narou-2.3.2 lib/commandline.rb
narou-2.3.1 lib/commandline.rb
narou-2.3.0 lib/commandline.rb
narou-2.3.0.pre.test1 lib/commandline.rb
narou-2.2.0 lib/commandline.rb
narou-2.1.1.pre.test1 lib/commandline.rb
narou-2.1.0 lib/commandline.rb
narou-2.0.2 lib/commandline.rb
narou-2.0.1 lib/commandline.rb
narou-2.0.0 lib/commandline.rb