Class: Morpheus::Cli::OptionParser

Inherits:
OptionParser
  • Object
show all
Defined in:
lib/morpheus/cli/option_parser.rb

Overview

an enhanced OptionParser Modifications include

  • footer property to compliment banner with footer=“Get details about a thing by ID.”

  • hidden options with add_hidden_option “–not-in-help”

  • errors raised from parse! will have a reference to the parser itself.

    this is useful so you can you print the banner (usage) message in your error handling
    

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

Returns the value of attribute footer.



14
15
16
# File 'lib/morpheus/cli/option_parser.rb', line 14

def footer
  @footer
end

#hidden_optionsObject

Array of option names to keep out of help message



17
18
19
# File 'lib/morpheus/cli/option_parser.rb', line 17

def hidden_options
  @hidden_options
end

Instance Method Details

#add_hidden_option(opt_name) ⇒ Object



69
70
71
72
73
74
75
76
77
78
# File 'lib/morpheus/cli/option_parser.rb', line 69

def add_hidden_option(opt_name)
  opt_array = [opt_name].flatten.compact
  @hidden_options ||= []
  opt_array.each do |val|
    if !@hidden_options.include?(val)
      @hidden_options << val
    end
  end
  @hidden_options
end

#full_help_message(opts = {}) ⇒ Object



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
# File 'lib/morpheus/cli/option_parser.rb', line 25

def full_help_message(opts={})
  out = ""
  #out << original_to_s
  if banner
    out << "#{banner}".sub(/\n?\z/, "\n")
  end
  if !self.to_a.empty?
    #out << "Options:\n"
    # the default way..
    # out << summarize().join("")

    # super hacky, should be examining the option, not the fully formatted description
    my_summaries = summarize()
    summarize().each do |opt_description|
      is_hidden = (@hidden_options || []).find { |hidden_switch|
        # opt_description.include?("--#{hidden_switch}")
        if hidden_switch.start_with?("-")
          opt_description.to_s.strip.start_with?("#{hidden_switch} ")
        else
          opt_description.to_s.strip.start_with?("--#{hidden_switch} ")
        end
      }
      if is_hidden
        if opts[:show_hidden_options]
          # out << opt_description + " (hidden)"
          out << opt_description
        else
          # hidden
        end
      else
        out << opt_description
      end
    end
  end
  if footer
    # nice_footer = footer.split("\n").collect {|line| "#{summary_indent}#{line}" }.join("\n")
    nice_footer = footer
    out << "\n"
    out << "#{nice_footer}".sub(/\n?\z/, "\n")
    # out << "\n"
  end
  out
end

#original_to_sObject



19
# File 'lib/morpheus/cli/option_parser.rb', line 19

alias :original_to_s :to_s

#parse!(*args) ⇒ Object

this needs mods too, but we dont use it… def parse end



84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
# File 'lib/morpheus/cli/option_parser.rb', line 84

def parse!(*args)
  # it is actually # def parse(argv = default_argv, into: nil)
  argv = [args].flatten() # args[0].flatten
  #help_wanted = argv.find {|arg| arg == "--help" || arg ==  "-h" }
  help_wanted = (argv.last == "--help" || argv.last ==  "-h") ? argv.last : nil
  begin
    return super(*args)
  rescue OptionParser::ParseError => e
    # last arg is --help
    # maybe they just got the Try --help message and its on the end
    # so strip all option arguments to avoid OptionParser::InvalidOption, etc.
    # this is not ideal, it means you cannot pass these strings as the last argument to your command.
    if help_wanted
      argv = argv.reject {|arg| arg =~ /^\-+/ }
      argv << help_wanted
      return super(argv)
    else
      e.optparse = self
      raise e
    end
    
  end
end

#to_sObject



21
22
23
# File 'lib/morpheus/cli/option_parser.rb', line 21

def to_s
  full_help_message
end