Class Boson::OptionCommand

  1. lib/boson/option_command.rb
Parent: Object

A class used by Scientist to wrap around Command objects. It’s main purpose is to parse a command’s global options (basic, render and pipe types) 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. For more about pipe and render options see Pipe and View respectively.

Basic Global Options

Any command with options comes with basic global options. For example ’-hv’ on an option command prints a help summarizing global and local options. Another basic global option is —pretend. This option displays what global options have been parsed and the actual arguments to be passed to a command if executed. For example:

# Define this command in a library
options :level=>:numeric, :verbose=>:boolean
def foo(*args)
  args
end

irb>> foo 'testin -p -l=1'
Arguments: ["testin", {:level=>1}]
Global options: {:pretend=>true}

If a global option conflicts with a local option, the local option takes precedence. You can get around this by passing global options after a ’-’. For example, if the global option -f (—fields) conflicts with a local -f (—force):

foo 'arg1 -v -f - -f=f1,f2'
# is the same as
foo 'arg1 -v --fields=f1,f2 -f'

Toggling Views With the Basic Global Option —render

One of the more important global options is —render. This option toggles the rendering of a command’s output done with View and Hirb.

Here’s a simple example of toggling Hirb’s table view:

# Defined in a library file:
#@options {}
def list(options={})
  [1,2,3]
end

Using it in irb:
>> list
=> [1,2,3]
>> list '-r'  # or list --render
+-------+
| value |
+-------+
| 1     |
| 2     |
| 3     |
+-------+
3 rows in set
=> true

Methods

public class

  1. new

public instance

  1. parse

Classes and Modules

Class Boson::OptionCommand::CommandArgumentError

Attributes

command [RW]

Public class methods

new (cmd)
[show source]
# File lib/boson/option_command.rb, line 106
    def initialize(cmd)
      @command = cmd
    end

Public instance methods

parse (args)

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

[show source]
# File lib/boson/option_command.rb, line 111
    def parse(args)
      if args.size == 1 && args[0].is_a?(String)
        global_opt, parsed_options, args = parse_options Shellwords.shellwords(args[0])
      # last string argument interpreted as args + options
      elsif args.size > 1 && args[-1].is_a?(String)
        temp_args = Runner.in_shell? ? args : Shellwords.shellwords(args.pop)
        global_opt, parsed_options, new_args = parse_options temp_args
        Runner.in_shell? ? args = new_args : args += new_args
      # add default options
      elsif @command.options.to_s.empty? || (!@command.has_splat_args? &&
        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