Sha256: 848b9e8be7877ced2b0e7953818241df4f660701cc4a962353cde39945952a02

Contents?: true

Size: 1.61 KB

Versions: 14

Compression:

Stored size: 1.61 KB

Contents

module GLI
  module Commands
    module HelpModules
      class OptionsFormatter
        def initialize(flags_and_switches,sorter,wrapper_class)
          @flags_and_switches = sorter.call(flags_and_switches)
          @wrapper_class = wrapper_class
        end

        def format
          list_formatter = ListFormatter.new(@flags_and_switches.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
          },@wrapper_class)
          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.safe_default_value || 'none'})"
          else
            String(option.description) +  (option.default_value ?  " (default: enabled)" : "")
          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

14 entries across 14 versions & 2 rubygems

Version Path
gli_aziz_light-2.8.1 lib/gli/commands/help_modules/options_formatter.rb
gli-2.8.1 lib/gli/commands/help_modules/options_formatter.rb
gli-2.8.0 lib/gli/commands/help_modules/options_formatter.rb
gli-2.7.0 lib/gli/commands/help_modules/options_formatter.rb
gli-2.6.2 lib/gli/commands/help_modules/options_formatter.rb
gli-2.6.1 lib/gli/commands/help_modules/options_formatter.rb
gli-2.6.0 lib/gli/commands/help_modules/options_formatter.rb
gli-2.6.0.rc1 lib/gli/commands/help_modules/options_formatter.rb
gli-2.5.6 lib/gli/commands/help_modules/options_formatter.rb
gli-2.5.5 lib/gli/commands/help_modules/options_formatter.rb
gli-2.5.4 lib/gli/commands/help_modules/options_formatter.rb
gli-2.5.3 lib/gli/commands/help_modules/options_formatter.rb
gli-2.5.2 lib/gli/commands/help_modules/options_formatter.rb
gli-2.5.1 lib/gli/commands/help_modules/options_formatter.rb