~ubuntu-branches/ubuntu/vivid/ruby-cri/vivid

« back to all changes in this revision

Viewing changes to lib/cri/command.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:
251
251
        run_this(opts_and_args, parent_opts)
252
252
      else
253
253
        # Handle options
254
 
        self.handle_options(opts_before_subcmd)
 
254
        handle_options(opts_before_subcmd)
255
255
 
256
256
        # Get command
257
257
        if subcmd_name.nil?
282
282
      # Parse
283
283
      parser = Cri::OptionParser.new(
284
284
        opts_and_args, self.global_option_definitions)
285
 
      self.handle_parser_errors_while { parser.run }
 
285
      handle_parser_errors_while { parser.run }
286
286
      local_opts  = parser.options
287
287
      global_opts = parent_opts.merge(parser.options)
288
288
      args = parser.arguments
289
289
 
290
290
      # Handle options
291
 
      self.handle_options(local_opts)
 
291
      handle_options(local_opts)
292
292
 
293
293
      # Execute
294
294
      if self.block.nil?
299
299
    end
300
300
 
301
301
    # @return [String] The help text for this command
 
302
    #
 
303
    # @option params [Boolean] :verbose true if the help output should be
 
304
    #   verbose, false otherwise.
302
305
    def help(params={})
303
 
      is_verbose = params.fetch(:verbose, false)
304
 
 
305
 
      text = ''
306
 
 
307
 
      # Append name and summary
308
 
      if summary
309
 
        text << "name".formatted_as_title << "\n"
310
 
        text << "    #{name.formatted_as_command} - #{summary}" << "\n"
311
 
        unless aliases.empty?
312
 
          text << "    aliases: " << aliases.map { |a| a.formatted_as_command }.join(' ') << "\n"
313
 
        end
314
 
      end
315
 
 
316
 
      # Append usage
317
 
      if usage
318
 
        path = [ self.supercommand ]
319
 
        path.unshift(path[0].supercommand) until path[0].nil?
320
 
        formatted_usage = usage.gsub(/^([^\s]+)/) { |m| m.formatted_as_command }
321
 
        full_usage = path[1..-1].map { |c| c.name.formatted_as_command + ' ' }.join + formatted_usage
322
 
 
323
 
        text << "\n"
324
 
        text << "usage".formatted_as_title << "\n"
325
 
        text << full_usage.wrap_and_indent(78, 4) << "\n"
326
 
      end
327
 
 
328
 
      # Append long description
329
 
      if description
330
 
        text << "\n"
331
 
        text << "description".formatted_as_title << "\n"
332
 
        text << description.wrap_and_indent(78, 4) + "\n"
333
 
      end
334
 
 
335
 
      # Append subcommands
336
 
      unless self.subcommands.empty?
337
 
        text << "\n"
338
 
        text << (self.supercommand ? 'subcommands' : 'commands').formatted_as_title
339
 
        text << "\n"
340
 
 
341
 
        shown_subcommands = self.subcommands.select { |c| !c.hidden? || is_verbose }
342
 
        length = shown_subcommands.map { |c| c.name.formatted_as_command.size }.max
343
 
 
344
 
        # Command
345
 
        shown_subcommands.sort_by { |cmd| cmd.name }.each do |cmd|
346
 
          text << sprintf("    %-#{length+4}s %s\n",
347
 
            cmd.name.formatted_as_command,
348
 
            cmd.summary)
349
 
        end
350
 
 
351
 
        # Hidden notice
352
 
        if !is_verbose
353
 
          diff = self.subcommands.size - shown_subcommands.size
354
 
          case diff
355
 
          when 0
356
 
          when 1
357
 
            text << "    (1 hidden command omitted; show it with --verbose)\n"
358
 
          else
359
 
            text << "    (#{diff} hidden commands omitted; show them with --verbose)\n"
360
 
          end
361
 
        end
362
 
      end
363
 
 
364
 
      # Append options
365
 
      groups = { 'options' => self.option_definitions }
366
 
      if self.supercommand
367
 
        groups["options for #{self.supercommand.name}"] = self.supercommand.global_option_definitions
368
 
      end
369
 
      length = groups.values.inject(&:+).map { |o| o[:long].to_s.size }.max
370
 
      groups.keys.sort.each do |name|
371
 
        defs = groups[name]
372
 
        unless defs.empty?
373
 
          text << "\n"
374
 
          text << "#{name}".formatted_as_title
375
 
          text << "\n"
376
 
          ordered_defs = defs.sort_by { |x| x[:short] || x[:long] }
377
 
          ordered_defs.each do |opt_def|
378
 
            text << sprintf(
379
 
              "    %-2s %-#{length+6}s",
380
 
              opt_def[:short] ? ('-' + opt_def[:short]) : '',
381
 
              opt_def[:long] ? ('--' + opt_def[:long]) : '').formatted_as_option
382
 
 
383
 
            text << opt_def[:desc] << "\n"
384
 
          end
385
 
        end
386
 
      end
387
 
 
388
 
      text
 
306
      HelpRenderer.new(self, params).render
389
307
    end
390
308
 
391
309
    # Compares this command's name to the other given command's name.
 
310
    #
 
311
    # @param [Cri::Command] other The command to compare with
 
312
    #
 
313
    # @return [-1, 0, 1] The result of the comparison between names
 
314
    #
 
315
    # @see Object<=>
392
316
    def <=>(other)
393
317
      self.name <=> other.name
394
318
    end
395
319
 
396
 
  protected
 
320
  private
397
321
 
398
322
    def handle_options(opts)
399
323
      opts.each_pair do |key, value|
408
332
      delegate = Cri::Command::OptionParserPartitioningDelegate.new
409
333
      parser = Cri::OptionParser.new(opts_and_args, global_option_definitions)
410
334
      parser.delegate = delegate
411
 
      self.handle_parser_errors_while { parser.run }
 
335
      handle_parser_errors_while { parser.run }
412
336
      parser
413
337
 
414
338
      # Extract