Module: Clamp::Subcommand::Declaration
- Included in:
- Command
- Defined in:
- lib/clamp/subcommand/declaration.rb
Overview
Subcommand declaration methods.
Instance Method Summary collapse
- #default_subcommand(*args, &block) ⇒ Object
- #default_subcommand=(name) ⇒ Object
- #find_subcommand(name) ⇒ Object
- #find_subcommand_class(*names) ⇒ Object
- #has_subcommands? ⇒ Boolean
- #inheritable_attributes ⇒ Object
- #recognised_subcommands ⇒ Object
- #subcommand(name, description, subcommand_class = self, &block) ⇒ Object
Instance Method Details
#default_subcommand(*args, &block) ⇒ Object
48 49 50 51 52 53 54 55 56 57 |
# File 'lib/clamp/subcommand/declaration.rb', line 48 def default_subcommand(*args, &block) if args.empty? @default_subcommand ||= false else $stderr.puts "WARNING: Clamp default_subcommand syntax has changed; check the README." $stderr.puts " (from #{caller(1..1).first})" self.default_subcommand = args.first subcommand(*args, &block) end end |
#default_subcommand=(name) ⇒ Object
43 44 45 46 |
# File 'lib/clamp/subcommand/declaration.rb', line 43 def default_subcommand=(name) raise Clamp::DeclarationError, "default_subcommand must be defined before subcommands" if has_subcommands? @default_subcommand = name end |
#find_subcommand(name) ⇒ Object
27 28 29 |
# File 'lib/clamp/subcommand/declaration.rb', line 27 def find_subcommand(name) recognised_subcommands.find { |sc| sc.is_called?(name) } end |
#find_subcommand_class(*names) ⇒ Object
31 32 33 34 35 36 37 |
# File 'lib/clamp/subcommand/declaration.rb', line 31 def find_subcommand_class(*names) names.inject(self) do |command_class, name| return nil unless command_class subcommand = command_class.find_subcommand(name) subcommand.subcommand_class if subcommand end end |
#has_subcommands? ⇒ Boolean
23 24 25 |
# File 'lib/clamp/subcommand/declaration.rb', line 23 def has_subcommands? !recognised_subcommands.empty? end |
#inheritable_attributes ⇒ Object
39 40 41 |
# File 'lib/clamp/subcommand/declaration.rb', line 39 def inheritable_attributes + inheritable_parameters end |
#recognised_subcommands ⇒ Object
13 14 15 |
# File 'lib/clamp/subcommand/declaration.rb', line 13 def recognised_subcommands @recognised_subcommands ||= [] end |
#subcommand(name, description, subcommand_class = self, &block) ⇒ Object
17 18 19 20 21 |
# File 'lib/clamp/subcommand/declaration.rb', line 17 def subcommand(name, description, subcommand_class = self, &block) subcommand_class = Class.new(subcommand_class, &block) if block declare_subcommand_parameters unless has_subcommands? recognised_subcommands << Subcommand::Definition.new(name, description, subcommand_class) end |