Class: Fig::Command::Options::Parser
- Inherits:
-
Object
- Object
- Fig::Command::Options::Parser
- 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.
"Short usage summary (use --help-long for everything):\n\nRunning under Fig:\nfig [...] [DESCRIPTOR] [-- COMMAND]\nfig [...] [DESCRIPTOR] --command-extra-args VALUES\nfig [...] [DESCRIPTOR] --run-command-statement\n\nPublishing packages:\nfig {--publish | --publish-local} [--force] DESCRIPTOR [...]\n\nQuerying:\nfig {-g | --get} VARIABLE [DESCRIPTOR] [...]\nfig --list-dependencies [--list-tree] [--list-all-configs] [DESCRIPTOR] [...]\nfig --list-variables [--list-tree] [--list-all-configs] [DESCRIPTOR] [...]\n\nStandard options (represented as \"[...]\" above):\n[--update | --update-if-missing]\n[--set VARIABLE=VALUE] [--add VARIABLE=VALUE]\n[--resource PATH] [--archive PATH]\n[--include DESCRIPTOR] [--include-file PATH:CONFIG]\n[--override DESCRIPTOR]\n[--file PATH] [--no-file]\n\n(--options for full option list; --help-long for everything)\n"
- FULL_USAGE =
"Running under Fig:\n\nfig [...] [DESCRIPTOR] [-- COMMAND]\nfig [...] [DESCRIPTOR] --command-extra-args VALUES\nfig [...] [DESCRIPTOR] --run-command-statement\n\nPublishing packages:\n\nfig {--publish | --publish-local} [--force] DESCRIPTOR [...]\n\nLocal repository maintenance:\n\nfig --clean DESCRIPTOR [...]\n\nQuerying:\n\nfig {--list-local | --list-remote} [...]\nfig {-g | --get} VARIABLE [DESCRIPTOR] [...]\nfig --list-dependencies [...list options...] [DESCRIPTOR] [...]\nfig --list-variables [...list options...] [DESCRIPTOR] [...]\nfig --list-configs [DESCRIPTOR] [...]\nfig --source-package FILE [DESCRIPTOR] [...]\nfig {-T | --dump-package-definition-text} [DESCRIPTOR] [...]\nfig --dump-package-definition-parsed [DESCRIPTOR] [...]\nfig --dump-package-definition-for-command-line [DESCRIPTOR] [...]\n\nList options (represented as \"[...list options...]\" above):\n\n[--list-tree | --graphviz]\n[--list-all-configs]\n\nStandard options (represented as \"[...]\" above):\n\n[-u | --update | -m | --update-if-missing]\n--update-lock-response {wait | fail | ignore}\n\n[{-s | --set} VARIABLE=VALUE]\n[{-p | --add | --append} VARIABLE=VALUE]\n\n[--resource PATH]\n[--archive PATH]\n[{-i | --include} DESCRIPTOR]\n[--include-file PATH:CONFIG]\n[--override DESCRIPTOR]\n\n[-R | --suppress-retrieves] [--suppress-cleanup-of-retrieves]\n[--suppress-all-includes] [--suppress-cross-package-includes]\n\n[--file PATH] [--no-file]\n[{-c | --config} CONFIG]\n\n[-l | --login]\n\n[--log-level LEVEL] [--log-config PATH | --log-to-stdout]\n[--figrc PATH] [--no-figrc] [--no-remote-figrc]\n\n[--suppress-vcs-comments-in-published-packages]\n[--suppress-warning-include-statement-missing-version]\n[--suppress-warning-unused-retrieve]\n\nInformation:\n\nfig --help\nfig --help-long\nfig --options\nfig {-v | --version | --version-plain}\n\n\nA DESCRIPTOR looks like <package name>[/<version>][:<config>] e.g. \"foo\",\n\"foo/1.2.3\", and \"foo/1.2.3:default\". Whether \":<config>\" and \"/<version>\" are\nrequired or allowed is dependent upon what your are doing.\n\nEnvironment variables:\n\nFIG_REMOTE_URL location of remote repository, required for remote\noperations\nFIG_HOME path to local repository, defaults to $HOME/.fighome\nFIG_SVN_EXECUTABLE path to svn executable, set to empty string to suppress\nuse of Subversion\nFIG_GIT_EXECUTABLE path to git executable, set to empty string to suppress\nuse of Git\n"
Instance Method Summary collapse
- #add_argument_description(options, description) ⇒ Object
- #full_help ⇒ Object
-
#initialize ⇒ Parser
constructor
A new instance of Parser.
- #on(*arguments, &block) ⇒ Object
- #on_head(*arguments, &block) ⇒ Object
- #on_tail(*arguments, &block) ⇒ Object
- #options_message ⇒ Object
- #parse!(argv) ⇒ Object
- #raise_invalid_argument(option, value, description = nil) ⇒ Object
- #separator(string) ⇒ Object
- #short_help ⇒ Object
Constructor Details
#initialize ⇒ Parser
Returns a new instance of Parser.
124 125 126 127 128 129 130 |
# File 'lib/fig/command/options/parser.rb', line 124 def initialize() @switches = {} @argument_description = {} @parser = OptionParser.new @parser. = "#{FULL_USAGE}\nAll options:\n\n" end |
Instance Method Details
#add_argument_description(options, description) ⇒ Object
132 133 134 135 136 137 138 139 140 141 142 143 144 |
# File 'lib/fig/command/options/parser.rb', line 132 def add_argument_description(, description) if .is_a? Array .each do |option| @argument_description[option] = description end else @argument_description[] = description end return end |
#full_help ⇒ Object
186 187 188 |
# File 'lib/fig/command/options/parser.rb', line 186 def full_help() return @parser.help end |
#on(*arguments, &block) ⇒ Object
156 157 158 159 160 161 162 163 164 |
# File 'lib/fig/command/options/parser.rb', line 156 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
146 147 148 149 150 151 152 153 154 |
# File 'lib/fig/command/options/parser.rb', line 146 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
172 173 174 175 176 177 178 179 180 |
# File 'lib/fig/command/options/parser.rb', line 172 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_message ⇒ Object
190 191 192 |
# File 'lib/fig/command/options/parser.rb', line 190 def () return @parser.summarize('') end |
#parse!(argv) ⇒ Object
194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 |
# File 'lib/fig/command/options/parser.rb', line 194 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
212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 |
# File 'lib/fig/command/options/parser.rb', line 212 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 |
#separator(string) ⇒ Object
166 167 168 169 170 |
# File 'lib/fig/command/options/parser.rb', line 166 def separator(string) @parser.separator string return end |
#short_help ⇒ Object
182 183 184 |
# File 'lib/fig/command/options/parser.rb', line 182 def short_help() return SHORT_USAGE end |