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

Class Method Summary collapse

Instance Method Summary collapse

Methods included from BaseBuilder

#evaluate!, #method_missing

Constructor Details

#initialize(command) ⇒ BaseCommandBuilder

Returns a new instance of BaseCommandBuilder.



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

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.



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

def entity
  @entity
end

Class Method Details

.create_conversionsObject



79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
# File 'lib/choosy/dsl/base_command_builder.rb', line 79

def self.create_conversions
  Choosy::Converter::CONVERSIONS.keys.each do |method|
    next if method == :boolean || method == :bool

    self.class_eval <<-EOF
      def #{method}(sym, desc, config=nil, &block)
        simple_option(sym, desc, true, :one, :#{method}, nil, config, &block)
      end

      def #{method}s(sym, desc, config=nil, &block)
        simple_option(sym, desc, true, :many, :#{method}, nil, config, &block)
      end

      def #{method}_(sym, desc, config=nil, &block)
        simple_option(sym, desc, false, :one, :#{method}, nil, config, &block)
      end

      def #{method}s_(sym, desc, config=nil, &block)
        simple_option(sym, desc, false, :many, :#{method}, nil, config, &block)
      end
    EOF
  end
end

Instance Method Details

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



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

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_



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

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



119
120
121
# File 'lib/choosy/dsl/base_command_builder.rb', line 119

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



123
124
125
# File 'lib/choosy/dsl/base_command_builder.rb', line 123

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



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

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



129
130
131
132
133
134
135
136
137
138
139
140
141
# File 'lib/choosy/dsl/base_command_builder.rb', line 129

def help(msg=nil, &block)
  h = OptionBuilder.new(OptionBuilder::HELP)
  h.short '-h'
  h.long '--help'
  msg ||= "Show this help message"
  h.desc msg

  h.validate do
    raise Choosy::HelpCalled.new(:help_option)
  end 

  evaluate_option_builder!(h, &block)
end

#option(arg, &block) ⇒ Object

Options



53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
# File 'lib/choosy/dsl/base_command_builder.rb', line 53

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



47
48
49
# File 'lib/choosy/dsl/base_command_builder.rb', line 47

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

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



24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/choosy/dsl/base_command_builder.rb', line 24

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



20
21
22
# File 'lib/choosy/dsl/base_command_builder.rb', line 20

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

#version(msg, &block) ⇒ Object



143
144
145
146
147
148
149
150
151
152
153
154
155
156
# File 'lib/choosy/dsl/base_command_builder.rb', line 143

def version(msg, &block)
  v = OptionBuilder.new(OptionBuilder::VERSION)
  v.long '--version'
  v.desc "The version number"
  # TODO: research how this should be refactored, used with manpage
  # v.default msg
  v.cast :boolean

  v.validate do
    raise Choosy::VersionCalled.new(msg)
  end

  evaluate_option_builder!(v, &block)
end