Module: Clamp::Subcommand::Declaration

Included in:
Command
Defined in:
lib/clamp/subcommand/declaration.rb

Instance Method Summary collapse

Instance Method Details

#default_subcommand(*args, &block) ⇒ Object



53
54
55
56
57
58
59
60
61
62
# File 'lib/clamp/subcommand/declaration.rb', line 53

def default_subcommand(*args, &block)
  if args.empty?
    @default_subcommand
  else
    $stderr.puts "WARNING: Clamp default_subcommand syntax has changed; check the README."
    $stderr.puts "  (from #{caller.first})"
    self.default_subcommand = args.first
    subcommand(*args, &block)
  end
end

#default_subcommand=(name) ⇒ Object



46
47
48
49
50
51
# File 'lib/clamp/subcommand/declaration.rb', line 46

def default_subcommand=(name)
  if has_subcommands?
    raise Clamp::DeclarationError, "default_subcommand must be defined before subcommands"
  end
  @default_subcommand = name
end

#find_subcommand(name) ⇒ Object



34
35
36
# File 'lib/clamp/subcommand/declaration.rb', line 34

def find_subcommand(name)
  recognised_subcommands.find { |sc| sc.is_called?(name) }
end

#has_subcommands?Boolean

Returns:

  • (Boolean)


30
31
32
# File 'lib/clamp/subcommand/declaration.rb', line 30

def has_subcommands?
  !recognised_subcommands.empty?
end

#inheritable_attributesObject



42
43
44
# File 'lib/clamp/subcommand/declaration.rb', line 42

def inheritable_attributes
  recognised_options + parameters_before_subcommand
end

#parameters_before_subcommandObject



38
39
40
# File 'lib/clamp/subcommand/declaration.rb', line 38

def parameters_before_subcommand
  parameters.take_while { |p| p != @subcommand_parameter }
end

#recognised_subcommandsObject



9
10
11
# File 'lib/clamp/subcommand/declaration.rb', line 9

def recognised_subcommands
  @recognised_subcommands ||= []
end

#subcommand(name, description, subcommand_class = self, &block) ⇒ Object



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/clamp/subcommand/declaration.rb', line 13

def subcommand(name, description, subcommand_class = self, &block)
  unless has_subcommands?
    @subcommand_parameter = if @default_subcommand
      parameter "[SUBCOMMAND]", "subcommand", :attribute_name => :subcommand_name, :default => @default_subcommand
    else
      parameter "SUBCOMMAND", "subcommand", :attribute_name => :subcommand_name, :required => false
    end
    remove_method :default_subcommand_name
    parameter "[ARG] ...", "subcommand arguments", :attribute_name => :subcommand_arguments
  end
  if block
    # generate a anonymous sub-class
    subcommand_class = Class.new(subcommand_class, &block)
  end
  recognised_subcommands << Subcommand::Definition.new(name, description, subcommand_class)
end