Sha256: 20c5ff3865d9d0277b22f1fdf00a38c55befffcf752c2951a1400ea4690086ba

Contents?: true

Size: 1.53 KB

Versions: 8

Compression:

Stored size: 1.53 KB

Contents

module GLI
  module Commands
    module HelpModules
      class OptionsFormatter
        def initialize(flags_and_switches)
          @flags_and_switches = flags_and_switches
        end

        def format
          list_formatter = ListFormatter.new(@flags_and_switches.values.sort { |a,b| 
            a.name.to_s <=> b.name.to_s 
          }.map { |option|
            if option.respond_to? :argument_name
              [option_names_for_help_string(option,option.argument_name),description_with_default(option)]
            else
              [option_names_for_help_string(option),description_with_default(option)]
            end
          })
          stringio = StringIO.new
          list_formatter.output(stringio)
          stringio.string
        end

      private

        def description_with_default(option)
          if option.kind_of? Flag
            String(option.description) + " (default: #{option.default_value || 'none'})"
          else
            String(option.description)
          end
        end

        def option_names_for_help_string(option,arg_name=nil)
          names = [option.name,Array(option.aliases)].flatten
          names = names.map { |name| CommandLineOption.name_as_string(name,option.kind_of?(Switch) ? option.negatable? : false) }
          if arg_name.nil?
            names.join(', ')
          else
            if names[-1] =~ /^--/
              names.join(', ') + "=#{arg_name}"
            else
              names.join(', ') + " #{arg_name}"
            end
          end
        end
      end
    end
  end
end

Version data entries

8 entries across 8 versions & 1 rubygems

Version Path
gli-2.1.0 lib/gli/commands/help_modules/options_formatter.rb
gli-2.0.0 lib/gli/commands/help_modules/options_formatter.rb
gli-2.0.0.rc8 lib/gli/commands/help_modules/options_formatter.rb
gli-2.0.0.rc7 lib/gli/commands/help_modules/options_formatter.rb
gli-2.0.0.rc6 lib/gli/commands/help_modules/options_formatter.rb
gli-2.0.0.rc5 lib/gli/commands/help_modules/options_formatter.rb
gli-2.0.0.rc4 lib/gli/commands/help_modules/options_formatter.rb
gli-2.0.0.rc3 lib/gli/commands/help_modules/options_formatter.rb