Class: Console::MasterCommand

Inherits:
Object
  • Object
show all
Defined in:
lib/more/facets/command.rb

Overview

Here is an example of usage:

# General Options

module GeneralOptions
  attr_accessor :dryrun ; alias_accessor :n, :noharm, :dryrun
  attr_accessor :quiet  ; alias_accessor :q, :quiet
  attr_accessor :force
  attr_accessor :trace
end

# Build Subcommand

class BuildCommand < Console::Command
  include GeneralOptions

  # metadata files
  attr_accessor  :file     ; alias_accessor :f, :file
  attr_accessor  :manifest ; alias_accessor :m, :manifest

  def call
    # do stuf here
  end
end

# Box Master Command

class BoxCommand < Console::MasterCommand
  subcommand :build,     BuildCommand
end

BoxCommand.start

Defined Under Namespace

Modules: UniversalOptions

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.option_arity(arity_hash = nil) ⇒ Object



97
98
99
100
101
102
# File 'lib/more/facets/command.rb', line 97

def self.option_arity(arity_hash=nil)
  if arity_hash
    (@option_arity ||= {}).merge!(arity_hash)
  end
  @option_arity
end

.start(line = nil) ⇒ Object



106
107
108
109
110
111
112
113
114
115
116
117
# File 'lib/more/facets/command.rb', line 106

def self.start(line=nil)
  cargs = Console::Arguments.new(line || ARGV, option_arity)
  pre = cargs.preoptions
  cmd, argv  = *cargs.subcommand
  args, opts = *argv

  if is_a?(UniversalOptions)
    new(pre, opts).call(cmd, args, opts)
  else
    new(pre).call(cmd, args, opts)
  end
end

.subcommand(name, command_class, options = nil) ⇒ Object



121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
# File 'lib/more/facets/command.rb', line 121

def self.subcommand(name, command_class, options=nil)
  options ||= {}
  if options[:no_merge]
    file, line = __FILE__, __LINE__+1
    code = %{
      def #{name}(args, opts)
        #{command_class}.new(args, opts).call
      end
    }
  else
    file, line = __FILE__, __LINE__+1
    code = %{
      def #{name}(args, opts)
        opts.merge(master_options)
        #{command_class}.new(args, opts).call
      end
    }
  end
  class_eval(code, file, line)
end

Instance Method Details

#call(cmd, args, opts) ⇒ Object



173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
# File 'lib/more/facets/command.rb', line 173

def call(cmd, args, opts)
  cmd = :default if cmd.nil?
  begin
    subcommand = method(cmd)
    parameters = [args, opts]
  rescue NameError
    subcommand = method(:subcommand_missing)
    parameters = [cmd, args, opts]
  end
  if subcommand.arity < 0
    subcommand.call(*parameters[0..subcommand.arity])
  else
    subcommand.call(*parameters[0,subcommand.arity])
  end
end

#defaultObject



193
# File 'lib/more/facets/command.rb', line 193

def default ; help ; end

#helpObject



191
# File 'lib/more/facets/command.rb', line 191

def help; end