~ubuntu-branches/debian/sid/ruby-cri/sid

« back to all changes in this revision

Viewing changes to lib/cri/command_dsl.rb

  • Committer: Package Import Robot
  • Author(s): Cédric Boutillier
  • Date: 2014-04-14 15:15:48 UTC
  • mfrom: (1.1.3)
  • Revision ID: package-import@ubuntu.com-20140414151548-xni482c106hiik3q
Tags: 2.6.0-1
* Imported Upstream version 2.6.0
* Add asciidoctor to Build-Depends, now that the README file has a .adoc
  extension
* Add privacy-breach.patch to remove remote badges from the README file

Show diffs side-by-side

added added

removed removed

Lines of Context:
6
6
  # commands.
7
7
  class CommandDSL
8
8
 
 
9
    # Creates a new DSL, intended to be used for building a single command. A
 
10
    # {CommandDSL} instance is not reusable; create a new instance if you want
 
11
    # to build another command.
 
12
    #
9
13
    # @param [Cri::Command, nil] command The command to modify, or nil if a
10
14
    #   new command should be created
11
15
    def initialize(command=nil)
101
105
    # @option params [:forbidden, :required, :optional] :argument Whether the
102
106
    #   argument is forbidden, required or optional
103
107
    #
 
108
    # @option params [Boolean] :multiple Whether or not the option should
 
109
    #   be multi-valued
 
110
    #
104
111
    # @return [void]
105
112
    def option(short, long, desc, params={}, &block)
106
 
      requiredness = params[:argument] || :forbidden
107
 
      self.add_option(short, long, desc, requiredness, block)
 
113
      requiredness = params.fetch(:argument, :forbidden)
 
114
      multiple = params.fetch(:multiple, false)
 
115
 
 
116
      if short.nil? && long.nil?
 
117
        raise ArgumentError, "short and long options cannot both be nil"
 
118
      end
 
119
 
 
120
      @command.option_definitions << {
 
121
        :short    => short.nil? ? nil : short.to_s,
 
122
        :long     => long.nil? ? nil : long.to_s,
 
123
        :desc     => desc,
 
124
        :argument => requiredness,
 
125
        :multiple => multiple,
 
126
        :block    => block,
 
127
      }
108
128
    end
109
129
    alias_method :opt, :option
110
130
 
117
137
    #
118
138
    # @param [String] desc The option description
119
139
    #
 
140
    # @option params [Boolean] :multiple Whether or not the option should
 
141
    #   be multi-valued
 
142
    #
120
143
    # @return [void]
121
144
    #
122
145
    # @see {#option}
123
 
    def required(short, long, desc, &block)
124
 
      self.add_option(short, long, desc, :required, block)
 
146
    def required(short, long, desc, params={}, &block)
 
147
      params = params.merge(:argument => :required)
 
148
      self.option(short, long, desc, params, &block)
125
149
    end
126
150
 
127
151
    # Adds a new option with a forbidden argument to the command. If a block
133
157
    #
134
158
    # @param [String] desc The option description
135
159
    #
 
160
    # @option params [Boolean] :multiple Whether or not the option should
 
161
    #   be multi-valued
 
162
    #
136
163
    # @return [void]
137
164
    #
138
165
    # @see {#option}
139
 
    def flag(short, long, desc, &block)
140
 
      self.add_option(short, long, desc, :forbidden, block)
 
166
    def flag(short, long, desc, params={}, &block)
 
167
      params = params.merge(:argument => :forbidden)
 
168
      self.option(short, long, desc, params, &block)
141
169
    end
142
170
    alias_method :forbidden, :flag
143
171
 
150
178
    #
151
179
    # @param [String] desc The option description
152
180
    #
 
181
    # @option params [Boolean] :multiple Whether or not the option should
 
182
    #   be multi-valued
 
183
    #
153
184
    # @return [void]
154
185
    #
155
186
    # @see {#option}
156
 
    def optional(short, long, desc, &block)
157
 
      self.add_option(short, long, desc, :optional, block)
 
187
    def optional(short, long, desc, params={}, &block)
 
188
      params = params.merge(:argument => :optional)
 
189
      self.option(short, long, desc, params, &block)
158
190
    end
159
191
 
160
192
    # Sets the run block to the given block. The given block should have two
162
194
    # Calling this will override existing run block or runner declarations
163
195
    # (using {#run} and {#runner}, respectively).
164
196
    #
 
197
    # @yieldparam [Hash<Symbol,Object>] opts A map of option names, as defined
 
198
    #   in the option definitions, onto strings (when single-valued) or arrays
 
199
    #   (when multi-valued)
 
200
    #
 
201
    # @yieldparam [Array<String>] args A list of arguments
 
202
    #
165
203
    # @return [void]
166
204
    def run(&block)
167
 
      unless block.arity != 2 || block.arity != 3
 
205
      unless [2, 3].include?(block.arity)
168
206
        raise ArgumentError,
169
207
          "The block given to Cri::Command#run expects two or three args"
170
208
      end
185
223
        klass.new(opts, args, cmd).call
186
224
      end
187
225
    end
188
 
 
189
 
  protected
190
 
 
191
 
    def add_option(short, long, desc, argument, block)
192
 
      if short.nil? && long.nil?
193
 
        raise ArgumentError, "short and long options cannot both be nil"
194
 
      end
195
 
 
196
 
      @command.option_definitions << {
197
 
        :short    => short.nil? ? nil : short.to_s,
198
 
        :long     => long.nil? ? nil : long.to_s,
199
 
        :desc     => desc,
200
 
        :argument => argument,
201
 
        :block    => block }
202
 
    end
203
 
 
204
226
  end
205
 
 
206
227
end