Class: GemInstaller::ArgParser

Inherits:
Object
  • Object
show all
Defined in:
lib/geminstaller/arg_parser.rb

Constant Summary collapse

VALID_GEMINSTALLER_OUTPUT_FLAGS =
[:none,:error,:install,:info,:commandecho,:debug,:all]
VALID_RUBYGEMS_OUTPUT_FLAGS =
[:none,:stdout,:stderr,:all]

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#options=(value) ⇒ Object (writeonly)

Sets the attribute options

Parameters:

  • value

    the value to set the attribute options to.



4
5
6
# File 'lib/geminstaller/arg_parser.rb', line 4

def options=(value)
  @options = value
end

#outputObject (readonly)

Returns the value of attribute output.



3
4
5
# File 'lib/geminstaller/arg_parser.rb', line 3

def output
  @output
end

Instance Method Details

#parse(args = []) ⇒ Object



9
10
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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
# File 'lib/geminstaller/arg_parser.rb', line 9

def parse(args = [])
  raise GemInstaller::GemInstallerError.new("Args must be passed as an array.") unless args.nil? or args.respond_to? :shift
  args = ARGV if args.nil? || args == []
  # check to see if args is an array of (nothing but) gems here...  if so, set args as [@options[:gems] and return

  raise GemInstaller::GemInstallerError.new("Error: An array of options to be populated must be injected prior to calling GemInstaller::ArgParser.parse") unless @options
  @options[:exceptions] = false
  @options[:redirect_stderr_to_stdout] = false
  @options[:silent] = false
  @options[:sudo] = false
  @options[:geminstaller_output] = [:error,:install,:info]
  @options[:rubygems_output] = [:stderr]
  @output = ""
  @unparsed_geminstaller_output_flags = nil
  @unparsed_rubygems_output_flags = nil
  opts = OptionParser.new do |opts|
    opts.banner = "Usage: geminstaller [options]"

    opts.separator ""

    config_msg =                         "Comma-delimited path(s) to GemInstaller config file(s)."
    exceptions_msg =                     "Raise any exceptions, rather than just printing them and exiting\n" +
    "                                     with a non-zero return code."
    redirect_stderr_to_stdout_msg =      "Redirect all STDERR output to STDOUT.  Useful to get all output when\n" +
    "                                     invoking GemInstaller via system()."
    geminstaller_output_msg =            "Comma-delimited list of output types to show from GemInstaller.\n" +
    "                                       Examples:\n" + 
    "                                         --gall\n" +
    "                                         --geminstaller-output=error,install,commandecho\n" +
    "                                       Default: error,install,info\n" +
    "                                       Valid types:\n" +
    "                                         - none:        print only fatal errors\n" +
    "                                         - error:       print error messages\n" +
    "                                         - install:     print install messages\n" +
    "                                         - info:        print informational messages\n" +
    "                                         - commandecho: print rubygems commands as they are invoked\n" +
    "                                         - debug:       print debug messages\n" +
    "                                         - all:         print all messages"
    help_msg =                           "Show this message."
    print_rogue_gems_msg =               "Print a report of all locally installed gems which are not specified\n" +
    "                                     in the geminstaller config file."
    rubygems_output_msg =                "Comma-delimited list of output types to show from internal:\n" +
    "                                       RubyGems command invocation.\n" +
    "                                       Examples:\n" + 
    "                                         --rall\n" +
    "                                         --rubygems-output=stderr\n" +
    "                                       Default: stderr\n" +
    "                                       Valid types:\n" +
    "                                         - none:        print no output\n" +
    "                                         - stdout:      print standard output stream\n" +
    "                                         - stderr:      print standard error stream\n" +
    "                                         - all:         print all output"
    sudo_msg =                           "Perform all gem operations under sudo (as root).  Will only work on\n" +
    "                                     correctly configured, supported systems.  See docs for more info."
    silent_msg =                         "Suppress all output except fatal exceptions, and output from\n" +
    "                                     rogue-gems option."
    version_msg =                        "Show GemInstaller version and exit."

    opts.on("-cCONFIGPATHS", "--config=CONFIGPATHS", String, config_msg) do |config_paths|
      @options[:config_paths] = config_paths
    end

    opts.on("-e", "--exceptions", exceptions_msg) do
      @options[:exceptions] = true
    end

    opts.on("-d", "--redirect-stderr-to-stdout", redirect_stderr_to_stdout_msg) do
      @options[:redirect_stderr_to_stdout] = true
    end

    opts.on("-gTYPES", "--geminstaller-output=TYPES", String, geminstaller_output_msg) do |geminstaller_output_flags|
      @unparsed_geminstaller_output_flags = geminstaller_output_flags
    end

    opts.on("-h", "--help", help_msg) do
      @output = opts.to_s
    end

    opts.on("-p", "--print-rogue-gems", print_rogue_gems_msg) do
      @options[:print_rogue_gems] = true
    end

    opts.on("-rTYPES", "--rubygems-output=TYPES", String, rubygems_output_msg) do |rubygems_output_flags|
      @unparsed_rubygems_output_flags = rubygems_output_flags
    end

    opts.on("-s", "--sudo", sudo_msg) do
      @options[:sudo] = true
    end

    opts.on("-t", "--silent", silent_msg) do
      @options[:silent] = true
    end

    opts.on("-v", "--version", version_msg) do
      @output = "#{GemInstaller::version}\n"
    end
  end

  begin
    opts.parse!(args)
  rescue(OptionParser::InvalidOption)
    @output << opts.to_s
    return 1
  end
  
  if @options[:silent] and (@unparsed_geminstaller_output_flags or @unparsed_rubygems_output_flags)
    @output = "The rubygems-output or geminstaller-output option cannot be specified if the silent option is true."
    return 1
  end

  if (@options[:sudo])
    @output = "The sudo option is not (yet) supported when invoking GemInstaller programatically.  It is only supported when using the command line 'geminstaller' executable.  See the docs for more info."
    return 1
  end
  
  # TODO: remove duplication
  if @unparsed_geminstaller_output_flags
    flags = @unparsed_geminstaller_output_flags.split(',') 
    flags.delete_if {|flag| flag == nil or flag == ''}
    flags.map! {|flag| flag.downcase}
    flags.sort!
    flags.uniq!
    flags.map! {|flag| flag.to_sym}
    geminstaller_output_valid = true
    flags.each do |flag|
      unless VALID_GEMINSTALLER_OUTPUT_FLAGS.include?(flag)
        @output = "Invalid geminstaller-output flag: #{flag}\n" 
        geminstaller_output_valid = false
      end
    end
    @options[:geminstaller_output] = flags if geminstaller_output_valid
  end

  if @unparsed_rubygems_output_flags
    flags = @unparsed_rubygems_output_flags.split(',') 
    flags.delete_if {|flag| flag == nil or flag == ''}
    flags.map! {|flag| flag.downcase}
    flags.sort!
    flags.uniq!
    flags.map! {|flag| flag.to_sym}
    rubygems_output_valid = true
    flags.each do |flag|
      unless VALID_RUBYGEMS_OUTPUT_FLAGS.include?(flag)
        @output = "Invalid rubygems-output flag: #{flag}\n" 
        rubygems_output_valid = false
      end
    end
    @options[:rubygems_output] = flags if rubygems_output_valid
  end

  # nil out @output if there was no output
  @output = nil if @output == ""
  return 0
  
end