Class: Boson::Command

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

Overview

A command starts with the functionality of a ruby method and adds benefits with options, etc.

Defined Under Namespace

Modules: API, APIClassMethods

Constant Summary collapse

ATTRIBUTES =

Attributes that are defined as accessors

[:name, :lib, :alias, :desc, :options, :args, :config]
INIT_ATTRIBUTES =

Attributes that can be passed in at initialization

[:alias, :desc, :options, :default_option, :option_command]

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from APIClassMethods

create, find, library_attributes, new_attributes

Methods included from API

#after_initialize, #basic_usage, #full_name, #option_command?

Constructor Details

#initialize(attributes) ⇒ Command

Takes a hash of attributes which map to instance variables and values. :name and :lib are required keys.

Parameters:

  • attributes (Hash)

Options Hash (attributes):

  • :desc (String)

    Description that shows up once in command listings

  • :alias (String)

    Alternative name for command

  • :options (Hash)

    Options passed to OptionParser

  • :args (Array, Integer, String)

    Should only be set if not automatically set. This attribute is only important for commands that have options. Its value can be an array, a number representing the number of arguments or ‘*’ if the command has a variable number of arguments.

  • :default_option (String)

    Only for an option command that has one or zero arguments. This treats the given option as an optional first argument. Example:

    # For a command with default option 'query' and options --query and -v
    'some -v'   -> '--query=some -v'
    '-v'        -> '-v'
    
  • :config (Hash)

    used by third party libraries to get and set custom command attributes.

  • :option_command (Boolean)

    Wraps a command with an OptionCommand object i.e. allow commands to have options.



65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
# File 'lib/boson/command.rb', line 65

def initialize(attributes)
  hash = attributes.dup
  @name = hash.delete(:name) or raise ArgumentError
  @lib = hash.delete(:lib) or raise ArgumentError
  # since MethodInspector scrapes arguments from file by default
  @file_parsed_args = true
  INIT_ATTRIBUTES.each do |e|
    instance_variable_set("@#{e}", hash.delete(e)) if hash.key?(e)
  end

  after_initialize(hash)

  if (args = hash.delete(:args))
    if args.is_a?(Array)
      @args = args
    elsif args.to_s[/^\d+/]
      @arg_size = args.to_i
    elsif args == '*'
      @args = [['*args']]
    end
  end
  @config = Util.recursive_hash_merge hash, hash.delete(:config) || {}
end

Instance Attribute Details

#file_parsed_argsObject (readonly)

Returns the value of attribute file_parsed_args.



40
41
42
# File 'lib/boson/command.rb', line 40

def file_parsed_args
  @file_parsed_args
end

Class Method Details

.usage(command) ⇒ Object

One line usage for a command if it exists



31
32
33
# File 'lib/boson/command.rb', line 31

def self.usage(command)
  (cmd = find(command)) ? "#{command} #{cmd.usage}" : ''
end

Instance Method Details

#arg_sizeObject

Number of arguments



152
153
154
155
156
157
# File 'lib/boson/command.rb', line 152

def arg_size
  unless instance_variable_defined?("@arg_size")
    @arg_size = args ? args.size : nil
  end
  @arg_size
end

#args(lib = library) ⇒ Object



120
121
122
# File 'lib/boson/command.rb', line 120

def args(lib=library)
  @args
end

#configObject

until @config is consistent in index + actual loading



130
131
132
# File 'lib/boson/command.rb', line 130

def config
  @config ||= {}
end

#has_splat_args?Boolean

Indicates if any arg has a splat

Returns:

  • (Boolean)


135
136
137
# File 'lib/boson/command.rb', line 135

def has_splat_args?
  !!(args && @args[-1] && @args[-1][0][/^\*/])
end

#incorrect_arg_size?(args) ⇒ Boolean

Determines if incorrect # of args given i.e. too little or too much

Returns:

  • (Boolean)


145
146
147
148
149
# File 'lib/boson/command.rb', line 145

def incorrect_arg_size?(args)
  return false if has_splat_args?
  required_arg_size = @args.take_while {|e| e[1].nil? }.size
  args.size < required_arg_size || args.size > required_arg_size
end

#libraryObject

Library object a command belongs to.



116
117
118
# File 'lib/boson/command.rb', line 116

def library
  @library ||= Boson.library(@lib)
end

#numerical_arg_size?Boolean

Indicates if arg size can handle a numerical comparison

Returns:

  • (Boolean)


140
141
142
# File 'lib/boson/command.rb', line 140

def numerical_arg_size?
  !has_splat_args? && arg_size
end

#option_parserObject

Option parser for command as defined by @options.



125
126
127
# File 'lib/boson/command.rb', line 125

def option_parser
  @option_parser ||= OptionParser.new(@options || {})
end