Class: Console::MasterCommand
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
- .option_arity(arity_hash = nil) ⇒ Object
- .start(line = nil) ⇒ Object
- .subcommand(name, command_class, options = nil) ⇒ Object
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. 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, =nil) ||= {} if [: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 |
#default ⇒ Object
193 |
# File 'lib/more/facets/command.rb', line 193 def default ; help ; end |
#help ⇒ Object
191 |
# File 'lib/more/facets/command.rb', line 191 def help; end |