Class: Af::OptionParser::Helper

Inherits:
Object
  • Object
show all
Defined in:
lib/fiksu-af/option_parser/helper.rb

Instance Method Summary collapse

Constructor Details

#initialize(af_opt_class_path) ⇒ Helper

Returns a new instance of Helper.



3
4
5
6
7
8
# File 'lib/fiksu-af/option_parser/helper.rb', line 3

def initialize(af_opt_class_path)
  option_finder = OptionFinder.new(af_opt_class_path)
  @options = option_finder.all_options
  @grouped_options = @options.group_by{|option| option.option_group_name || :basic}
  @groups = option_finder.all_option_groups.sort{|a,b| (a.priority || 50) <=> (b.priority || 50)}
end

Instance Method Details

#help(command_line_usage, show_hidden = false) ⇒ Object

Prints to stdout application usage and all command line options.



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
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
78
# File 'lib/fiksu-af/option_parser/helper.rb', line 11

def help(command_line_usage, show_hidden = false)
  # Print usage.
  puts(command_line_usage)

  # Array of strings to be printed to stdout.
  output = []

  # Iterate through all command groups  sorted by priority.
  @groups.each do |group|
    options_in_group = @grouped_options[group.group_name]
    unless options_in_group.blank?
      if group.hidden == true && show_hidden == false
        # skipping hidden groups
      else
        output << "#{group.group_name}: #{group.title}"
        unless group.description.blank?
          output << " " + group.description.split("\n").map(&:strip).join("\n ")
        end

        rows = []

        # Iterate trhough all commands in this group.
        options_in_group.sort{|a,b| a.long_name <=> b.long_name}.each do |option|
          if option.hidden == true && show_hidden == false
            # skipping hidden commands
          else
            columns = []
            switches = "#{option.long_name}"
            if (option.short_name)
              switches << " | #{option.short_name}"
            end
            unless (option.requirements == ::Af::OptionParser::GetOptions::NO_ARGUMENT)
              if (option.argument_note)
                switches << " #{option.argument_note}"
              elsif (option.option_type)
                note = OptionType.find_by_short_name(option.option_type).try(:argument_note)
                switches << " #{note}" if note
              end
            end
            columns << switches
            notes = []
            unless (option.requirements == ::Af::OptionParser::GetOptions::NO_ARGUMENT)
              if option.default_value.present?
                if option.default_value.is_a? Array
                  notes << "(default: #{option.default_value.join(',')})"
                elsif option.default_value.is_a? Hash
                  notes << "(default: #{option.default_value.map{|k,v| k.to_s + '=>' + v.to_s}.join(',')}"
                else
                  notes << "(default: #{option.default_value})"
                end
              end
            end
            notes << (option.note || "")
            notes << "(choices: #{option.choices.map(&:to_s).join(', ')})" unless option.choices.blank?
            if option.environment_variable
              notes << " [env: #{option.environment_variable}]"
            end
            columns << notes.join(' ')
            rows << columns
          end
        end
        output += Columnizer.new.columnized(rows)
      end
    end
  end

  puts output.join("\n")
end