Class: Choosy::DSL::BaseCommandBuilder

Inherits:
Object
  • Object
show all
Includes:
BaseBuilder
Defined in:
lib/choosy/dsl/base_command_builder.rb

Direct Known Subclasses

CommandBuilder, SuperCommandBuilder

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from BaseBuilder

#evaluate!, #method_missing

Constructor Details

#initialize(command) ⇒ BaseCommandBuilder

Returns a new instance of BaseCommandBuilder.



7
8
9
# File 'lib/choosy/dsl/base_command_builder.rb', line 7

def initialize(command)
  @entity = command
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method in the class Choosy::DSL::BaseBuilder

Instance Attribute Details

#entityObject (readonly)

Returns the value of attribute entity.



5
6
7
# File 'lib/choosy/dsl/base_command_builder.rb', line 5

def entity
  @entity
end

Instance Method Details

#boolean(sym, desc, config = nil, &block) ⇒ Object Also known as: bool



100
101
102
# File 'lib/choosy/dsl/base_command_builder.rb', line 100

def boolean(sym, desc, config=nil, &block)
  simple_option(sym, desc, true, :zero, :boolean, nil, config, &block)
end

#boolean_(sym, desc, config = nil, &block) ⇒ Object Also known as: bool_



103
104
105
# File 'lib/choosy/dsl/base_command_builder.rb', line 103

def boolean_(sym, desc, config=nil, &block)
  simple_option(sym, desc, false, :zero, :boolean, nil, config, &block)
end

#enum(sym, allowed, desc, config = nil, &block) ⇒ Object



109
110
111
# File 'lib/choosy/dsl/base_command_builder.rb', line 109

def enum(sym, allowed, desc, config=nil, &block)
  simple_option(sym, desc, true, :one, :symbol, allowed, config, &block)
end

#enum_(sym, allowed, desc, config = nil, &block) ⇒ Object



113
114
115
# File 'lib/choosy/dsl/base_command_builder.rb', line 113

def enum_(sym, allowed, desc, config=nil, &block)
  simple_option(sym, desc, false, :one, :symbol, allowed, config, &block)
end

#heading(msg, *styles, &block) ⇒ Object Also known as: section

Formatting



33
34
35
36
# File 'lib/choosy/dsl/base_command_builder.rb', line 33

def heading(msg, *styles, &block)
  @entity.listing << Choosy::Printing::FormattingElement.new(:header, msg, styles)
  evaluate!(&block) if block_given?
end

#help(msg = nil, &block) ⇒ Object

Additional helpers



119
120
121
122
123
124
125
126
127
128
129
130
131
132
# File 'lib/choosy/dsl/base_command_builder.rb', line 119

def help(msg=nil, &block)
  msg ||= "Show this help message"

  h = OptionBuilder.new(OptionBuilder::HELP) do
    short '-h'
    long '--help'
    desc msg
    validate do
      raise Choosy::HelpCalled.new(:help)
    end 
  end

  evaluate_option_builder!(h, &block)
end

#no_color(msg, &block) ⇒ Object



152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
# File 'lib/choosy/dsl/base_command_builder.rb', line 152

def no_color(msg, &block)
  msg ||= "Disable the color"
  cmd = entity

  n = OptionBuilder.new(:no_color) do
    long "--no-color"
    desc msg
    cast :boolean
    validate do |args, options|
      if cmd.printer.respond_to?(:color)
        cmd.printer.color.disable!
      end
    end
  end

  evaluate_option_builder!(n, &block)
end

#option(arg, &block) ⇒ Object

Options



46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/choosy/dsl/base_command_builder.rb', line 46

def option(arg, &block)
  raise Choosy::ConfigurationError.new("The option name was nil") if arg.nil?
  
  builder = nil

  if arg.is_a?(Hash)
    raise Choosy::ConfigurationError.new("Malformed option hash") if arg.count != 1
    name = arg.keys[0]
    builder = OptionBuilder.new(name)

    to_process = arg[name]
    if to_process.is_a?(Array)
      builder.depends_on to_process
    elsif to_process.is_a?(Hash)
      builder.from_hash to_process
    else
      raise Choosy::ConfigurationError.new("Unable to process option hash")
    end
  else
    builder = OptionBuilder.new(arg)
    raise Choosy::ConfigurationError.new("No configuration block was given") if !block_given?
  end

  evaluate_option_builder!(builder, &block)
end

#para(msg = nil, *styles) ⇒ Object



40
41
42
# File 'lib/choosy/dsl/base_command_builder.rb', line 40

def para(msg=nil, *styles)
  @entity.listing << Choosy::Printing::FormattingElement.new(:para, msg, styles)
end

#printer(kind, options = {}) ⇒ Object



17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/choosy/dsl/base_command_builder.rb', line 17

def printer(kind, options={})
  @entity.printer =  if kind == :standard
                        Choosy::Printing::HelpPrinter.new(options)
                      elsif kind == :erb
                        Choosy::Printing::ERBPrinter.new(options)
                      elsif kind == :manpage
                        Choosy::Printing::ManpagePrinter.new(options)
                      elsif kind.respond_to?(:print!)
                        kind
                      else
                        raise Choosy::ConfigurationError.new("Unknown printing method for help: #{kind}")
                      end
end

#summary(msg) ⇒ Object

Generic setup



13
14
15
# File 'lib/choosy/dsl/base_command_builder.rb', line 13

def summary(msg)
  @entity.summary = msg
end

#version(msg, &block) ⇒ Object



134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
# File 'lib/choosy/dsl/base_command_builder.rb', line 134

def version(msg, &block)
  v = OptionBuilder.new(OptionBuilder::VERSION) do
    long '--version'
    desc "The version number"
    cast :boolean
    validate do
      raise Choosy::VersionCalled.new(msg)
    end
  end

  option_eigenclass = class << v.entity; self; end
  option_eigenclass.send :define_method, :version do
    msg
  end

  evaluate_option_builder!(v, &block)
end