Class: Fig::Command::Options::Parser

Inherits:
Object
  • Object
show all
Defined in:
lib/fig/command/options/parser.rb

Overview

Command-line processing.

Constant Summary collapse

SHORT_USAGE =

This class knows way too much about how OptionParser works.

<<-'END_SHORT_USAGE'
Short usage summary (use --help-long for everything):

Running under Fig:
fig [...] [DESCRIPTOR] [-- COMMAND]
fig [...] [DESCRIPTOR] --command-extra-args VALUES
fig [...] [DESCRIPTOR] --run-command-statement

Publishing packages:
fig {--publish | --publish-local} [--force] DESCRIPTOR [...]

Querying:
fig {-g | --get} VARIABLE                                  [DESCRIPTOR] [...]
fig --list-dependencies [--list-tree] [--list-all-configs] [DESCRIPTOR] [...]
fig --list-variables [--list-tree] [--list-all-configs]    [DESCRIPTOR] [...]

Standard options (represented as "[...]" above):
    [--update | --update-if-missing]
    [--set    VARIABLE=VALUE]
    [--append VARIABLE=VALUE]
    [--resource PATH]      [--archive  PATH]
    [--include DESCRIPTOR] [--include-file PATH:CONFIG]
    [--override DESCRIPTOR]
    [--file PATH] [--no-file]

(--options for full option list; --help-long for everything)
END_SHORT_USAGE
FULL_USAGE =
<<-'END_FULL_USAGE'
Running under Fig:

fig [...] [DESCRIPTOR] [-- COMMAND]
fig [...] [DESCRIPTOR] --command-extra-args VALUES
fig [...] [DESCRIPTOR] --run-command-statement

Publishing packages:

fig {--publish | --publish-local} [--force] DESCRIPTOR [...]

Local repository maintenance:

fig --clean DESCRIPTOR [...]

Querying:

fig {--list-local | --list-remote}                                      [...]
fig {-g | --get} VARIABLE                                  [DESCRIPTOR] [...]
fig --list-dependencies [...list options...]               [DESCRIPTOR] [...]
fig --list-variables    [...list options...]               [DESCRIPTOR] [...]
fig --list-configs                                         [DESCRIPTOR] [...]
fig --source-package FILE                                  [DESCRIPTOR] [...]
fig {-T | --dump-package-definition-text}                  [DESCRIPTOR] [...]
fig --dump-package-definition-parsed                       [DESCRIPTOR] [...]
fig --dump-package-definition-for-command-line             [DESCRIPTOR] [...]

List options (represented as "[...list options...]" above):

    [--list-tree | --graphviz]
    [--list-all-configs]

Standard options (represented as "[...]" above):

    [-u | --update | -m | --update-if-missing]
    --update-lock-response {wait | fail | ignore}

    [{-s | --set}    VARIABLE=VALUE]
    [{-p | --append} VARIABLE=VALUE]

    [--resource       PATH]
    [--archive        PATH]
    [{-i | --include} DESCRIPTOR]
    [--include-file   PATH:CONFIG]
    [--override       DESCRIPTOR]

    [-R | --suppress-retrieves] [--suppress-cleanup-of-retrieves]
    [--suppress-all-includes] [--suppress-cross-package-includes]

    [--file PATH] [--no-file]
    [{-c | --config} CONFIG]

    [-l | --login]

    [--log-level LEVEL] [--log-config PATH]
    [--figrc PATH]      [--no-figrc]

    [--suppress-vcs-comments-in-published-packages]
    [--suppress-warning-include-statement-missing-version]
    [--suppress-warning-unused-retrieve]

Information:

fig --help
fig --help-long
fig --options
fig {-v | --version | --version-plain}


A DESCRIPTOR looks like <package name>[/<version>][:<config>] e.g. "foo",
"foo/1.2.3", and "foo/1.2.3:default". Whether ":<config>" and "/<version>" are
required or allowed is dependent upon what your are doing.

Environment variables:

FIG_REMOTE_URL     location of remote repository, required for remote
                   operations
FIG_HOME           path to local repository, defaults to $HOME/.fighome
FIG_SVN_EXECUTABLE path to svn executable, set to empty string to suppress
                   use of Subversion
FIG_GIT_EXECUTABLE path to git executable, set to empty string to suppress
                   use of Git
END_FULL_USAGE

Instance Method Summary collapse

Constructor Details

#initializeParser

Returns a new instance of Parser.



125
126
127
128
129
130
131
# File 'lib/fig/command/options/parser.rb', line 125

def initialize()
  @switches             = {}
  @argument_description = {}
  @parser               = OptionParser.new

  @parser.banner = "#{FULL_USAGE}\nAll options:\n\n"
end

Instance Method Details

#add_argument_description(options, description) ⇒ Object



133
134
135
136
137
138
139
140
141
142
143
144
145
# File 'lib/fig/command/options/parser.rb', line 133

def add_argument_description(options, description)
  if options.is_a? Array
    options.each do
      |option|

      @argument_description[option] = description
    end
  else
    @argument_description[options] = description
  end

  return
end

#full_helpObject



181
182
183
# File 'lib/fig/command/options/parser.rb', line 181

def full_help()
  return @parser.help
end

#on(*arguments, &block) ⇒ Object



157
158
159
160
161
162
163
164
165
# File 'lib/fig/command/options/parser.rb', line 157

def on(*arguments, &block)
  switch_array = make_switch_array(arguments, block)

  return if not switch_array

  @parser.top.append(*switch_array)

  return
end

#on_head(*arguments, &block) ⇒ Object



147
148
149
150
151
152
153
154
155
# File 'lib/fig/command/options/parser.rb', line 147

def on_head(*arguments, &block)
  switch_array = make_switch_array(arguments, block)

  return if not switch_array

  @parser.top.prepend(*switch_array)

  return
end

#on_tail(*arguments, &block) ⇒ Object



167
168
169
170
171
172
173
174
175
# File 'lib/fig/command/options/parser.rb', line 167

def on_tail(*arguments, &block)
  switch_array = make_switch_array(arguments, block)

  return if not switch_array

  @parser.base.append(*switch_array)

  return
end

#options_messageObject



185
186
187
# File 'lib/fig/command/options/parser.rb', line 185

def options_message()
  return @parser.summarize('')
end

#parse!(argv) ⇒ Object



189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
# File 'lib/fig/command/options/parser.rb', line 189

def parse!(argv)
  begin
    @parser.parse!(argv)
  rescue OptionParser::InvalidArgument => error
    raise_invalid_argument(error.args[0], error.args[1])
  rescue OptionParser::MissingArgument => error
    raise_missing_argument(error.args[0])
  rescue OptionParser::InvalidOption => error
    raise Fig::Command::OptionError.new(
      "Unknown option #{error.args[0]}.\n\n#{SHORT_USAGE}"
    )
  rescue OptionParser::ParseError => error
    raise Fig::Command::OptionError.new(error.to_s)
  end

  return
end

#raise_invalid_argument(option, value, description = nil) ⇒ Object



207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
# File 'lib/fig/command/options/parser.rb', line 207

def raise_invalid_argument(option, value, description = nil)
  # *sigh* OptionParser does not raise MissingArgument for the case of an
  # option with a required value being followed by another option.  It
  # assigns the next option as the value instead.  E.g. for
  #
  #    fig --set --get FOO
  #
  # it assigns "--get" as the value of the "--set" option.
  if @switches.has_key? value
    raise_missing_argument(option)
  end

  description ||= @argument_description[option]
  if description.nil?
    description = ''
  else
    description = ' ' + description
  end

  raise Fig::Command::OptionError.new(
    %Q<Invalid value for #{option}: "#{value}"#{description}>
  )
end

#short_helpObject



177
178
179
# File 'lib/fig/command/options/parser.rb', line 177

def short_help()
  return SHORT_USAGE
end