Class: Boson::OptionCommand

Inherits:
Object
  • Object
show all
Extended by:
API
Includes:
API
Defined in:
lib/boson/option_command.rb

Overview

A class used by Scientist to wrap around Command objects. It's main purpose is to parse a command's global and local options. As the names imply, global options are available to all commands while local options are specific to a command. When passing options to commands, global ones must be passed first, then local ones. Also, options must all be passed either before or after arguments.

Basic Global Options

Any command with options comes with basic global options. For example '-h' on an option command prints help. If a global option conflicts with a local option, the local option takes precedence.

Defined Under Namespace

Modules: API Classes: CommandArgumentError

Constant Summary

BASIC_OPTIONS =

default global options

{
  :help=>{:type=>:boolean, :desc=>"Display a command's help"},
}

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from API

default_options, option_parser

Constructor Details

#initialize(cmd) ⇒ OptionCommand

Returns a new instance of OptionCommand



37
38
39
# File 'lib/boson/option_command.rb', line 37

def initialize(cmd)
  @command = cmd
end

Instance Attribute Details

#commandObject

Returns the value of attribute command



36
37
38
# File 'lib/boson/option_command.rb', line 36

def command
  @command
end

Class Method Details

.default_option_parserObject



24
25
26
# File 'lib/boson/option_command.rb', line 24

def default_option_parser
  @default_option_parser ||= OptionParser.new default_options
end

Instance Method Details

#add_default_args(args, obj) ⇒ Object

Adds default args as original method would



99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
# File 'lib/boson/option_command.rb', line 99

def add_default_args(args, obj)
  if @command.args && args.size < @command.arg_size - 1
    # leave off last arg since its an option
    @command.args.slice(0..-2).each_with_index {|arr,i|
      next if args.size >= i + 1 # only fill in once args run out
      break if arr.size != 2 # a default arg value must exist
      begin
        args[i] = @command.file_parsed_args ? obj.instance_eval(arr[1]) : arr[1]
      rescue Exception
        raise Scientist::Error, "Unable to set default argument at " +
          "position #{i+1}.\nReason: #{$!.message}"
      end
    }
  end
end

#check_argument_size(args) ⇒ Object

raises CommandArgumentError if argument size is incorrect for given args



88
89
90
91
92
93
94
95
96
# File 'lib/boson/option_command.rb', line 88

def check_argument_size(args)
  if @command.numerical_arg_size? && args.size != @command.arg_size
    command_size, args_size = args.size > @command.arg_size ?
      [@command.arg_size, args.size] :
      [@command.arg_size - 1, args.size - 1]
    raise CommandArgumentError,
      "wrong number of arguments (#{args_size} for #{command_size})"
  end
end

#modify_args(args) ⇒ Object

modifies args for edge cases



79
80
81
82
83
84
85
# File 'lib/boson/option_command.rb', line 79

def modify_args(args)
  if @command.default_option && @command.numerical_arg_size? &&
    @command.arg_size <= 1 &&
    !args[0].is_a?(Hash) && args[0].to_s[/./] != '-' && !args.join.empty?
      args[0] = "--#{@command.default_option}=#{args[0]}"
  end
end

#parse(args) ⇒ Object

Parses arguments and returns global options, local options and leftover arguments.



43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/boson/option_command.rb', line 43

def parse(args)
  if args.size == 1 && args[0].is_a?(String)
    args = Shellwords.shellwords(args[0]) if !Boson.in_shell
    global_opt, parsed_options, args = parse_options args
  # last string argument interpreted as args + options
  elsif args.size > 1 && args[-1].is_a?(String)
    temp_args = Boson.in_shell ? args : Shellwords.shellwords(args.pop)
    global_opt, parsed_options, new_args = parse_options temp_args
    Boson.in_shell ? args = new_args : args += new_args
  # add default options
  elsif @command.options.nil? || @command.options.empty? ||
    (@command.numerical_arg_size? && args.size <= (@command.arg_size - 1).abs) ||
    (@command.has_splat_args? && !args[-1].is_a?(Hash))
      global_opt, parsed_options = parse_options([])[0,2]
  # merge default options with given hash of options
  elsif (@command.has_splat_args? || (args.size == @command.arg_size)) &&
    args[-1].is_a?(Hash)
      global_opt, parsed_options = parse_options([])[0,2]
      parsed_options.merge!(args.pop)
  end
  [global_opt || {}, parsed_options, args]
end

#parse_global_options(args) ⇒ Object



66
67
68
# File 'lib/boson/option_command.rb', line 66

def parse_global_options(args)
  option_parser.parse args
end