Class: Boson::OptionCommand
- Inherits:
-
Object
- Object
- Boson::OptionCommand
- 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 collapse
- BASIC_OPTIONS =
default global options
{ :help=>{:type=>:boolean, :desc=>"Display a command's help"}, }
Instance Attribute Summary collapse
-
#command ⇒ Object
Returns the value of attribute command.
Class Method Summary collapse
Instance Method Summary collapse
-
#add_default_args(args, obj) ⇒ Object
Adds default args as original method would.
-
#check_argument_size(args) ⇒ Object
raises CommandArgumentError if argument size is incorrect for given args.
-
#initialize(cmd) ⇒ OptionCommand
constructor
A new instance of OptionCommand.
-
#modify_args(args) ⇒ Object
modifies args for edge cases.
-
#parse(args) ⇒ Object
Parses arguments and returns global options, local options and leftover arguments.
- #parse_global_options(args) ⇒ Object
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
#command ⇒ Object
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_parser ⇒ Object
24 25 26 |
# File 'lib/boson/option_command.rb', line 24 def default_option_parser @default_option_parser ||= OptionParser.new end |
Instance Method Details
#add_default_args(args, obj) ⇒ Object
Adds default args as original method would
98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 |
# File 'lib/boson/option_command.rb', line 98 def add_default_args(args, obj) if @command.args && args.size < @command.args.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
87 88 89 90 91 92 93 94 95 |
# File 'lib/boson/option_command.rb', line 87 def check_argument_size(args) if args.size != @command.arg_size && !@command.has_splat_args? 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
78 79 80 81 82 83 84 |
# File 'lib/boson/option_command.rb', line 78 def modify_args(args) if @command.default_option && @command.arg_size <= 1 && !@command.has_splat_args? && !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 |
# File 'lib/boson/option_command.rb', line 43 def parse(args) if args.size == 1 && args[0].is_a?(String) global_opt, , args = Shellwords.shellwords(args[0]) # 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, , new_args = temp_args Boson.in_shell ? args = new_args : args += new_args # add default options elsif @command..nil? || @command..empty? || (!@command.has_splat_args? && args.size <= (@command.arg_size - 1).abs) || (@command.has_splat_args? && !args[-1].is_a?(Hash)) global_opt, = ([])[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, = ([])[0,2] .merge!(args.pop) end [global_opt || {}, , args] end |
#parse_global_options(args) ⇒ Object
65 66 67 |
# File 'lib/boson/option_command.rb', line 65 def (args) option_parser.parse args end |