rubygems-update 2.6.0 → 3.5.4

Sign up to get free protection for your applications and to get access to all the features.
Files changed (812) hide show
  1. checksums.yaml +5 -5
  2. data/CHANGELOG.md +5941 -0
  3. data/CODE_OF_CONDUCT.md +76 -35
  4. data/CONTRIBUTING.md +231 -0
  5. data/MAINTAINERS.txt +8 -0
  6. data/Manifest.txt +439 -216
  7. data/POLICIES.md +135 -0
  8. data/README.md +112 -0
  9. data/UPGRADING.md +15 -0
  10. data/bundler/CHANGELOG.md +4753 -0
  11. data/bundler/LICENSE.md +22 -0
  12. data/bundler/README.md +58 -0
  13. data/bundler/UPGRADING.md +222 -0
  14. data/bundler/bundler.gemspec +46 -0
  15. data/bundler/exe/bundle +29 -0
  16. data/bundler/exe/bundler +4 -0
  17. data/bundler/lib/bundler/.document +1 -0
  18. data/bundler/lib/bundler/build_metadata.rb +45 -0
  19. data/bundler/lib/bundler/capistrano.rb +22 -0
  20. data/bundler/lib/bundler/checksum.rb +254 -0
  21. data/bundler/lib/bundler/ci_detector.rb +75 -0
  22. data/bundler/lib/bundler/cli/add.rb +47 -0
  23. data/bundler/lib/bundler/cli/binstubs.rb +57 -0
  24. data/bundler/lib/bundler/cli/cache.rb +43 -0
  25. data/bundler/lib/bundler/cli/check.rb +40 -0
  26. data/bundler/lib/bundler/cli/clean.rb +25 -0
  27. data/bundler/lib/bundler/cli/common.rb +138 -0
  28. data/bundler/lib/bundler/cli/config.rb +204 -0
  29. data/bundler/lib/bundler/cli/console.rb +44 -0
  30. data/bundler/lib/bundler/cli/doctor.rb +157 -0
  31. data/bundler/lib/bundler/cli/exec.rb +88 -0
  32. data/bundler/lib/bundler/cli/fund.rb +36 -0
  33. data/bundler/lib/bundler/cli/gem.rb +471 -0
  34. data/bundler/lib/bundler/cli/info.rb +83 -0
  35. data/bundler/lib/bundler/cli/init.rb +51 -0
  36. data/bundler/lib/bundler/cli/inject.rb +60 -0
  37. data/bundler/lib/bundler/cli/install.rb +190 -0
  38. data/bundler/lib/bundler/cli/issue.rb +41 -0
  39. data/bundler/lib/bundler/cli/list.rb +66 -0
  40. data/bundler/lib/bundler/cli/lock.rb +73 -0
  41. data/bundler/lib/bundler/cli/open.rb +29 -0
  42. data/bundler/lib/bundler/cli/outdated.rb +297 -0
  43. data/bundler/lib/bundler/cli/platform.rb +48 -0
  44. data/bundler/lib/bundler/cli/plugin.rb +34 -0
  45. data/bundler/lib/bundler/cli/pristine.rb +60 -0
  46. data/bundler/lib/bundler/cli/remove.rb +17 -0
  47. data/bundler/lib/bundler/cli/show.rb +75 -0
  48. data/bundler/lib/bundler/cli/update.rb +123 -0
  49. data/bundler/lib/bundler/cli/viz.rb +31 -0
  50. data/bundler/lib/bundler/cli.rb +846 -0
  51. data/bundler/lib/bundler/compact_index_client/cache.rb +121 -0
  52. data/bundler/lib/bundler/compact_index_client/cache_file.rb +153 -0
  53. data/bundler/lib/bundler/compact_index_client/gem_parser.rb +32 -0
  54. data/bundler/lib/bundler/compact_index_client/updater.rb +115 -0
  55. data/bundler/lib/bundler/compact_index_client.rb +126 -0
  56. data/bundler/lib/bundler/constants.rb +7 -0
  57. data/bundler/lib/bundler/current_ruby.rb +92 -0
  58. data/bundler/lib/bundler/definition.rb +1015 -0
  59. data/bundler/lib/bundler/dependency.rb +105 -0
  60. data/bundler/lib/bundler/deployment.rb +69 -0
  61. data/bundler/lib/bundler/deprecate.rb +44 -0
  62. data/bundler/lib/bundler/digest.rb +71 -0
  63. data/bundler/lib/bundler/dsl.rb +604 -0
  64. data/bundler/lib/bundler/endpoint_specification.rb +147 -0
  65. data/bundler/lib/bundler/env.rb +148 -0
  66. data/bundler/lib/bundler/environment_preserver.rb +86 -0
  67. data/bundler/lib/bundler/errors.rb +233 -0
  68. data/bundler/lib/bundler/feature_flag.rb +53 -0
  69. data/bundler/lib/bundler/fetcher/base.rb +52 -0
  70. data/bundler/lib/bundler/fetcher/compact_index.rb +129 -0
  71. data/bundler/lib/bundler/fetcher/dependency.rb +78 -0
  72. data/bundler/lib/bundler/fetcher/downloader.rb +91 -0
  73. data/bundler/lib/bundler/fetcher/gem_remote_fetcher.rb +16 -0
  74. data/bundler/lib/bundler/fetcher/index.rb +25 -0
  75. data/bundler/lib/bundler/fetcher.rb +324 -0
  76. data/bundler/lib/bundler/force_platform.rb +18 -0
  77. data/bundler/lib/bundler/friendly_errors.rb +126 -0
  78. data/bundler/lib/bundler/gem_helper.rb +237 -0
  79. data/bundler/lib/bundler/gem_helpers.rb +127 -0
  80. data/bundler/lib/bundler/gem_tasks.rb +7 -0
  81. data/bundler/lib/bundler/gem_version_promoter.rb +145 -0
  82. data/bundler/lib/bundler/graph.rb +152 -0
  83. data/bundler/lib/bundler/index.rb +205 -0
  84. data/bundler/lib/bundler/injector.rb +287 -0
  85. data/bundler/lib/bundler/inline.rb +73 -0
  86. data/bundler/lib/bundler/installer/gem_installer.rb +84 -0
  87. data/bundler/lib/bundler/installer/parallel_installer.rb +202 -0
  88. data/bundler/lib/bundler/installer/standalone.rb +116 -0
  89. data/bundler/lib/bundler/installer.rb +267 -0
  90. data/bundler/lib/bundler/lazy_specification.rb +172 -0
  91. data/bundler/lib/bundler/lockfile_generator.rb +104 -0
  92. data/bundler/lib/bundler/lockfile_parser.rb +289 -0
  93. data/bundler/lib/bundler/man/.document +1 -0
  94. data/bundler/lib/bundler/man/bundle-add.1 +59 -0
  95. data/bundler/lib/bundler/man/bundle-add.1.ronn +58 -0
  96. data/bundler/lib/bundler/man/bundle-binstubs.1 +30 -0
  97. data/bundler/lib/bundler/man/bundle-binstubs.1.ronn +41 -0
  98. data/bundler/lib/bundler/man/bundle-cache.1 +40 -0
  99. data/bundler/lib/bundler/man/bundle-cache.1.ronn +79 -0
  100. data/bundler/lib/bundler/man/bundle-check.1 +22 -0
  101. data/bundler/lib/bundler/man/bundle-check.1.ronn +26 -0
  102. data/bundler/lib/bundler/man/bundle-clean.1 +17 -0
  103. data/bundler/lib/bundler/man/bundle-clean.1.ronn +18 -0
  104. data/bundler/lib/bundler/man/bundle-config.1 +321 -0
  105. data/bundler/lib/bundler/man/bundle-config.1.ronn +411 -0
  106. data/bundler/lib/bundler/man/bundle-console.1 +35 -0
  107. data/bundler/lib/bundler/man/bundle-console.1.ronn +44 -0
  108. data/bundler/lib/bundler/man/bundle-doctor.1 +30 -0
  109. data/bundler/lib/bundler/man/bundle-doctor.1.ronn +33 -0
  110. data/bundler/lib/bundler/man/bundle-exec.1 +104 -0
  111. data/bundler/lib/bundler/man/bundle-exec.1.ronn +151 -0
  112. data/bundler/lib/bundler/man/bundle-gem.1 +69 -0
  113. data/bundler/lib/bundler/man/bundle-gem.1.ronn +117 -0
  114. data/bundler/lib/bundler/man/bundle-help.1 +9 -0
  115. data/bundler/lib/bundler/man/bundle-help.1.ronn +12 -0
  116. data/bundler/lib/bundler/man/bundle-info.1 +14 -0
  117. data/bundler/lib/bundler/man/bundle-info.1.ronn +17 -0
  118. data/bundler/lib/bundler/man/bundle-init.1 +20 -0
  119. data/bundler/lib/bundler/man/bundle-init.1.ronn +31 -0
  120. data/bundler/lib/bundler/man/bundle-inject.1 +23 -0
  121. data/bundler/lib/bundler/man/bundle-inject.1.ronn +24 -0
  122. data/bundler/lib/bundler/man/bundle-install.1 +215 -0
  123. data/bundler/lib/bundler/man/bundle-install.1.ronn +383 -0
  124. data/bundler/lib/bundler/man/bundle-list.1 +35 -0
  125. data/bundler/lib/bundler/man/bundle-list.1.ronn +33 -0
  126. data/bundler/lib/bundler/man/bundle-lock.1 +60 -0
  127. data/bundler/lib/bundler/man/bundle-lock.1.ronn +94 -0
  128. data/bundler/lib/bundler/man/bundle-open.1 +32 -0
  129. data/bundler/lib/bundler/man/bundle-open.1.ronn +27 -0
  130. data/bundler/lib/bundler/man/bundle-outdated.1 +100 -0
  131. data/bundler/lib/bundler/man/bundle-outdated.1.ronn +106 -0
  132. data/bundler/lib/bundler/man/bundle-platform.1 +49 -0
  133. data/bundler/lib/bundler/man/bundle-platform.1.ronn +49 -0
  134. data/bundler/lib/bundler/man/bundle-plugin.1 +55 -0
  135. data/bundler/lib/bundler/man/bundle-plugin.1.ronn +59 -0
  136. data/bundler/lib/bundler/man/bundle-pristine.1 +23 -0
  137. data/bundler/lib/bundler/man/bundle-pristine.1.ronn +34 -0
  138. data/bundler/lib/bundler/man/bundle-remove.1 +21 -0
  139. data/bundler/lib/bundler/man/bundle-remove.1.ronn +23 -0
  140. data/bundler/lib/bundler/man/bundle-show.1 +16 -0
  141. data/bundler/lib/bundler/man/bundle-show.1.ronn +21 -0
  142. data/bundler/lib/bundler/man/bundle-update.1 +275 -0
  143. data/bundler/lib/bundler/man/bundle-update.1.ronn +351 -0
  144. data/bundler/lib/bundler/man/bundle-version.1 +22 -0
  145. data/bundler/lib/bundler/man/bundle-version.1.ronn +24 -0
  146. data/bundler/lib/bundler/man/bundle-viz.1 +30 -0
  147. data/bundler/lib/bundler/man/bundle-viz.1.ronn +32 -0
  148. data/bundler/lib/bundler/man/bundle.1 +102 -0
  149. data/bundler/lib/bundler/man/bundle.1.ronn +116 -0
  150. data/bundler/lib/bundler/man/gemfile.5 +470 -0
  151. data/bundler/lib/bundler/man/gemfile.5.ronn +552 -0
  152. data/bundler/lib/bundler/man/index.txt +29 -0
  153. data/bundler/lib/bundler/match_metadata.rb +17 -0
  154. data/bundler/lib/bundler/match_platform.rb +23 -0
  155. data/bundler/lib/bundler/match_remote_metadata.rb +29 -0
  156. data/bundler/lib/bundler/mirror.rb +221 -0
  157. data/bundler/lib/bundler/plugin/api/source.rb +321 -0
  158. data/bundler/lib/bundler/plugin/api.rb +81 -0
  159. data/bundler/lib/bundler/plugin/dsl.rb +53 -0
  160. data/bundler/lib/bundler/plugin/events.rb +61 -0
  161. data/bundler/lib/bundler/plugin/index.rb +193 -0
  162. data/bundler/lib/bundler/plugin/installer/git.rb +34 -0
  163. data/bundler/lib/bundler/plugin/installer/rubygems.rb +19 -0
  164. data/bundler/lib/bundler/plugin/installer.rb +112 -0
  165. data/bundler/lib/bundler/plugin/source_list.rb +31 -0
  166. data/bundler/lib/bundler/plugin.rb +359 -0
  167. data/bundler/lib/bundler/process_lock.rb +24 -0
  168. data/bundler/lib/bundler/remote_specification.rb +121 -0
  169. data/bundler/lib/bundler/resolver/base.rb +107 -0
  170. data/bundler/lib/bundler/resolver/candidate.rb +94 -0
  171. data/bundler/lib/bundler/resolver/incompatibility.rb +15 -0
  172. data/bundler/lib/bundler/resolver/package.rb +77 -0
  173. data/bundler/lib/bundler/resolver/root.rb +25 -0
  174. data/bundler/lib/bundler/resolver/spec_group.rb +79 -0
  175. data/bundler/lib/bundler/resolver.rb +462 -0
  176. data/bundler/lib/bundler/retry.rb +66 -0
  177. data/bundler/lib/bundler/ruby_dsl.rb +53 -0
  178. data/bundler/lib/bundler/ruby_version.rb +131 -0
  179. data/bundler/lib/bundler/rubygems_ext.rb +328 -0
  180. data/bundler/lib/bundler/rubygems_gem_installer.rb +137 -0
  181. data/bundler/lib/bundler/rubygems_integration.rb +493 -0
  182. data/bundler/lib/bundler/runtime.rb +307 -0
  183. data/bundler/lib/bundler/safe_marshal.rb +31 -0
  184. data/bundler/lib/bundler/self_manager.rb +186 -0
  185. data/bundler/lib/bundler/settings/validator.rb +102 -0
  186. data/bundler/lib/bundler/settings.rb +588 -0
  187. data/bundler/lib/bundler/setup.rb +33 -0
  188. data/bundler/lib/bundler/shared_helpers.rb +380 -0
  189. data/bundler/lib/bundler/similarity_detector.rb +63 -0
  190. data/bundler/lib/bundler/source/gemspec.rb +18 -0
  191. data/bundler/lib/bundler/source/git/git_proxy.rb +455 -0
  192. data/bundler/lib/bundler/source/git.rb +380 -0
  193. data/bundler/lib/bundler/source/metadata.rb +62 -0
  194. data/bundler/lib/bundler/source/path/installer.rb +53 -0
  195. data/bundler/lib/bundler/source/path.rb +261 -0
  196. data/bundler/lib/bundler/source/rubygems/remote.rb +68 -0
  197. data/bundler/lib/bundler/source/rubygems.rb +516 -0
  198. data/bundler/lib/bundler/source/rubygems_aggregate.rb +68 -0
  199. data/bundler/lib/bundler/source.rb +116 -0
  200. data/bundler/lib/bundler/source_list.rb +227 -0
  201. data/bundler/lib/bundler/source_map.rb +71 -0
  202. data/bundler/lib/bundler/spec_set.rb +296 -0
  203. data/bundler/lib/bundler/stub_specification.rb +119 -0
  204. data/bundler/lib/bundler/templates/.document +1 -0
  205. data/bundler/lib/bundler/templates/Executable +27 -0
  206. data/bundler/lib/bundler/templates/Executable.bundler +109 -0
  207. data/bundler/lib/bundler/templates/Executable.standalone +14 -0
  208. data/bundler/lib/bundler/templates/Gemfile +5 -0
  209. data/bundler/lib/bundler/templates/newgem/CHANGELOG.md.tt +5 -0
  210. data/bundler/lib/bundler/templates/newgem/CODE_OF_CONDUCT.md.tt +84 -0
  211. data/bundler/lib/bundler/templates/newgem/Cargo.toml.tt +7 -0
  212. data/bundler/lib/bundler/templates/newgem/Gemfile.tt +26 -0
  213. data/bundler/lib/bundler/templates/newgem/LICENSE.txt.tt +21 -0
  214. data/bundler/lib/bundler/templates/newgem/README.md.tt +45 -0
  215. data/bundler/lib/bundler/templates/newgem/Rakefile.tt +67 -0
  216. data/bundler/lib/bundler/templates/newgem/bin/console.tt +11 -0
  217. data/bundler/lib/bundler/templates/newgem/bin/setup.tt +8 -0
  218. data/bundler/lib/bundler/templates/newgem/circleci/config.yml.tt +25 -0
  219. data/bundler/lib/bundler/templates/newgem/exe/newgem.tt +3 -0
  220. data/bundler/lib/bundler/templates/newgem/ext/newgem/Cargo.toml.tt +15 -0
  221. data/bundler/lib/bundler/templates/newgem/ext/newgem/extconf-c.rb.tt +10 -0
  222. data/bundler/lib/bundler/templates/newgem/ext/newgem/extconf-rust.rb.tt +6 -0
  223. data/bundler/lib/bundler/templates/newgem/ext/newgem/newgem.c.tt +9 -0
  224. data/bundler/lib/bundler/templates/newgem/ext/newgem/newgem.h.tt +6 -0
  225. data/bundler/lib/bundler/templates/newgem/ext/newgem/src/lib.rs.tt +12 -0
  226. data/bundler/lib/bundler/templates/newgem/github/workflows/main.yml.tt +37 -0
  227. data/bundler/lib/bundler/templates/newgem/gitignore.tt +23 -0
  228. data/bundler/lib/bundler/templates/newgem/gitlab-ci.yml.tt +18 -0
  229. data/bundler/lib/bundler/templates/newgem/lib/newgem/version.rb.tt +9 -0
  230. data/bundler/lib/bundler/templates/newgem/lib/newgem.rb.tt +15 -0
  231. data/bundler/lib/bundler/templates/newgem/newgem.gemspec.tt +51 -0
  232. data/bundler/lib/bundler/templates/newgem/rspec.tt +3 -0
  233. data/bundler/lib/bundler/templates/newgem/rubocop.yml.tt +13 -0
  234. data/bundler/lib/bundler/templates/newgem/sig/newgem.rbs.tt +8 -0
  235. data/bundler/lib/bundler/templates/newgem/spec/newgem_spec.rb.tt +11 -0
  236. data/bundler/lib/bundler/templates/newgem/spec/spec_helper.rb.tt +15 -0
  237. data/bundler/lib/bundler/templates/newgem/standard.yml.tt +3 -0
  238. data/bundler/lib/bundler/templates/newgem/test/minitest/test_helper.rb.tt +6 -0
  239. data/bundler/lib/bundler/templates/newgem/test/minitest/test_newgem.rb.tt +13 -0
  240. data/bundler/lib/bundler/templates/newgem/test/test-unit/newgem_test.rb.tt +15 -0
  241. data/bundler/lib/bundler/templates/newgem/test/test-unit/test_helper.rb.tt +6 -0
  242. data/bundler/lib/bundler/ui/rg_proxy.rb +19 -0
  243. data/bundler/lib/bundler/ui/shell.rb +165 -0
  244. data/bundler/lib/bundler/ui/silent.rb +85 -0
  245. data/bundler/lib/bundler/ui.rb +9 -0
  246. data/bundler/lib/bundler/uri_credentials_filter.rb +43 -0
  247. data/bundler/lib/bundler/uri_normalizer.rb +23 -0
  248. data/bundler/lib/bundler/vendor/.document +1 -0
  249. data/bundler/lib/bundler/vendor/connection_pool/.document +1 -0
  250. data/bundler/lib/bundler/vendor/connection_pool/LICENSE +20 -0
  251. data/bundler/lib/bundler/vendor/connection_pool/lib/connection_pool/timed_stack.rb +174 -0
  252. data/bundler/lib/bundler/vendor/connection_pool/lib/connection_pool/version.rb +3 -0
  253. data/bundler/lib/bundler/vendor/connection_pool/lib/connection_pool/wrapper.rb +56 -0
  254. data/bundler/lib/bundler/vendor/connection_pool/lib/connection_pool.rb +175 -0
  255. data/bundler/lib/bundler/vendor/fileutils/.document +1 -0
  256. data/bundler/lib/bundler/vendor/fileutils/LICENSE.txt +22 -0
  257. data/bundler/lib/bundler/vendor/fileutils/lib/fileutils.rb +2694 -0
  258. data/bundler/lib/bundler/vendor/net-http-persistent/.document +1 -0
  259. data/bundler/lib/bundler/vendor/net-http-persistent/README.rdoc +82 -0
  260. data/bundler/lib/bundler/vendor/net-http-persistent/lib/net/http/persistent/connection.rb +41 -0
  261. data/bundler/lib/bundler/vendor/net-http-persistent/lib/net/http/persistent/pool.rb +65 -0
  262. data/bundler/lib/bundler/vendor/net-http-persistent/lib/net/http/persistent/timed_stack_multi.rb +79 -0
  263. data/bundler/lib/bundler/vendor/net-http-persistent/lib/net/http/persistent.rb +1073 -0
  264. data/bundler/lib/bundler/vendor/pub_grub/.document +1 -0
  265. data/bundler/lib/bundler/vendor/pub_grub/LICENSE.txt +21 -0
  266. data/bundler/lib/bundler/vendor/pub_grub/lib/pub_grub/assignment.rb +20 -0
  267. data/bundler/lib/bundler/vendor/pub_grub/lib/pub_grub/basic_package_source.rb +189 -0
  268. data/bundler/lib/bundler/vendor/pub_grub/lib/pub_grub/failure_writer.rb +182 -0
  269. data/bundler/lib/bundler/vendor/pub_grub/lib/pub_grub/incompatibility.rb +150 -0
  270. data/bundler/lib/bundler/vendor/pub_grub/lib/pub_grub/package.rb +43 -0
  271. data/bundler/lib/bundler/vendor/pub_grub/lib/pub_grub/partial_solution.rb +121 -0
  272. data/bundler/lib/bundler/vendor/pub_grub/lib/pub_grub/rubygems.rb +45 -0
  273. data/bundler/lib/bundler/vendor/pub_grub/lib/pub_grub/solve_failure.rb +19 -0
  274. data/bundler/lib/bundler/vendor/pub_grub/lib/pub_grub/static_package_source.rb +60 -0
  275. data/bundler/lib/bundler/vendor/pub_grub/lib/pub_grub/term.rb +105 -0
  276. data/bundler/lib/bundler/vendor/pub_grub/lib/pub_grub/version.rb +3 -0
  277. data/bundler/lib/bundler/vendor/pub_grub/lib/pub_grub/version_constraint.rb +129 -0
  278. data/bundler/lib/bundler/vendor/pub_grub/lib/pub_grub/version_range.rb +411 -0
  279. data/bundler/lib/bundler/vendor/pub_grub/lib/pub_grub/version_solver.rb +248 -0
  280. data/bundler/lib/bundler/vendor/pub_grub/lib/pub_grub/version_union.rb +178 -0
  281. data/bundler/lib/bundler/vendor/pub_grub/lib/pub_grub.rb +31 -0
  282. data/bundler/lib/bundler/vendor/thor/.document +1 -0
  283. data/bundler/lib/bundler/vendor/thor/LICENSE.md +20 -0
  284. data/bundler/lib/bundler/vendor/thor/lib/thor/actions/create_file.rb +105 -0
  285. data/bundler/lib/bundler/vendor/thor/lib/thor/actions/create_link.rb +61 -0
  286. data/bundler/lib/bundler/vendor/thor/lib/thor/actions/directory.rb +108 -0
  287. data/bundler/lib/bundler/vendor/thor/lib/thor/actions/empty_directory.rb +143 -0
  288. data/bundler/lib/bundler/vendor/thor/lib/thor/actions/file_manipulation.rb +373 -0
  289. data/bundler/lib/bundler/vendor/thor/lib/thor/actions/inject_into_file.rb +130 -0
  290. data/bundler/lib/bundler/vendor/thor/lib/thor/actions.rb +340 -0
  291. data/bundler/lib/bundler/vendor/thor/lib/thor/base.rb +825 -0
  292. data/bundler/lib/bundler/vendor/thor/lib/thor/command.rb +151 -0
  293. data/bundler/lib/bundler/vendor/thor/lib/thor/core_ext/hash_with_indifferent_access.rb +107 -0
  294. data/bundler/lib/bundler/vendor/thor/lib/thor/error.rb +106 -0
  295. data/bundler/lib/bundler/vendor/thor/lib/thor/group.rb +281 -0
  296. data/bundler/lib/bundler/vendor/thor/lib/thor/invocation.rb +178 -0
  297. data/bundler/lib/bundler/vendor/thor/lib/thor/line_editor/basic.rb +37 -0
  298. data/bundler/lib/bundler/vendor/thor/lib/thor/line_editor/readline.rb +88 -0
  299. data/bundler/lib/bundler/vendor/thor/lib/thor/line_editor.rb +17 -0
  300. data/bundler/lib/bundler/vendor/thor/lib/thor/nested_context.rb +29 -0
  301. data/bundler/lib/bundler/vendor/thor/lib/thor/parser/argument.rb +89 -0
  302. data/bundler/lib/bundler/vendor/thor/lib/thor/parser/arguments.rb +195 -0
  303. data/bundler/lib/bundler/vendor/thor/lib/thor/parser/option.rb +178 -0
  304. data/bundler/lib/bundler/vendor/thor/lib/thor/parser/options.rb +293 -0
  305. data/bundler/lib/bundler/vendor/thor/lib/thor/parser.rb +4 -0
  306. data/bundler/lib/bundler/vendor/thor/lib/thor/rake_compat.rb +72 -0
  307. data/bundler/lib/bundler/vendor/thor/lib/thor/runner.rb +335 -0
  308. data/bundler/lib/bundler/vendor/thor/lib/thor/shell/basic.rb +388 -0
  309. data/bundler/lib/bundler/vendor/thor/lib/thor/shell/color.rb +112 -0
  310. data/bundler/lib/bundler/vendor/thor/lib/thor/shell/column_printer.rb +29 -0
  311. data/bundler/lib/bundler/vendor/thor/lib/thor/shell/html.rb +81 -0
  312. data/bundler/lib/bundler/vendor/thor/lib/thor/shell/table_printer.rb +134 -0
  313. data/bundler/lib/bundler/vendor/thor/lib/thor/shell/terminal.rb +42 -0
  314. data/bundler/lib/bundler/vendor/thor/lib/thor/shell/wrapped_printer.rb +38 -0
  315. data/bundler/lib/bundler/vendor/thor/lib/thor/shell.rb +81 -0
  316. data/bundler/lib/bundler/vendor/thor/lib/thor/util.rb +285 -0
  317. data/bundler/lib/bundler/vendor/thor/lib/thor/version.rb +3 -0
  318. data/bundler/lib/bundler/vendor/thor/lib/thor.rb +663 -0
  319. data/bundler/lib/bundler/vendor/tsort/.document +1 -0
  320. data/bundler/lib/bundler/vendor/tsort/LICENSE.txt +22 -0
  321. data/bundler/lib/bundler/vendor/tsort/lib/tsort.rb +455 -0
  322. data/bundler/lib/bundler/vendor/uri/.document +1 -0
  323. data/bundler/lib/bundler/vendor/uri/LICENSE.txt +22 -0
  324. data/bundler/lib/bundler/vendor/uri/lib/uri/common.rb +853 -0
  325. data/bundler/lib/bundler/vendor/uri/lib/uri/file.rb +100 -0
  326. data/bundler/lib/bundler/vendor/uri/lib/uri/ftp.rb +267 -0
  327. data/bundler/lib/bundler/vendor/uri/lib/uri/generic.rb +1588 -0
  328. data/bundler/lib/bundler/vendor/uri/lib/uri/http.rb +125 -0
  329. data/bundler/lib/bundler/vendor/uri/lib/uri/https.rb +23 -0
  330. data/bundler/lib/bundler/vendor/uri/lib/uri/ldap.rb +261 -0
  331. data/bundler/lib/bundler/vendor/uri/lib/uri/ldaps.rb +22 -0
  332. data/bundler/lib/bundler/vendor/uri/lib/uri/mailto.rb +293 -0
  333. data/bundler/lib/bundler/vendor/uri/lib/uri/rfc2396_parser.rb +539 -0
  334. data/bundler/lib/bundler/vendor/uri/lib/uri/rfc3986_parser.rb +183 -0
  335. data/bundler/lib/bundler/vendor/uri/lib/uri/version.rb +6 -0
  336. data/bundler/lib/bundler/vendor/uri/lib/uri/ws.rb +83 -0
  337. data/bundler/lib/bundler/vendor/uri/lib/uri/wss.rb +23 -0
  338. data/bundler/lib/bundler/vendor/uri/lib/uri.rb +104 -0
  339. data/bundler/lib/bundler/vendored_fileutils.rb +4 -0
  340. data/bundler/lib/bundler/vendored_net_http.rb +8 -0
  341. data/bundler/lib/bundler/vendored_persistent.rb +11 -0
  342. data/bundler/lib/bundler/vendored_pub_grub.rb +4 -0
  343. data/bundler/lib/bundler/vendored_thor.rb +8 -0
  344. data/bundler/lib/bundler/vendored_timeout.rb +8 -0
  345. data/bundler/lib/bundler/vendored_tsort.rb +4 -0
  346. data/bundler/lib/bundler/vendored_uri.rb +4 -0
  347. data/bundler/lib/bundler/version.rb +13 -0
  348. data/bundler/lib/bundler/vlad.rb +17 -0
  349. data/bundler/lib/bundler/worker.rb +117 -0
  350. data/bundler/lib/bundler/yaml_serializer.rb +93 -0
  351. data/bundler/lib/bundler.rb +663 -0
  352. data/{test/rubygems/bogussources.rb → exe/gem} +5 -2
  353. data/exe/update_rubygems +38 -0
  354. data/hide_lib_for_update/note.txt +0 -4
  355. data/lib/rubygems/available_set.rb +12 -12
  356. data/lib/rubygems/basic_specification.rb +82 -59
  357. data/lib/rubygems/bundler_version_finder.rb +77 -0
  358. data/lib/rubygems/ci_detector.rb +75 -0
  359. data/lib/rubygems/command.rb +144 -71
  360. data/lib/rubygems/command_manager.rb +64 -27
  361. data/lib/rubygems/commands/build_command.rb +88 -17
  362. data/lib/rubygems/commands/cert_command.rb +131 -83
  363. data/lib/rubygems/commands/check_command.rb +30 -27
  364. data/lib/rubygems/commands/cleanup_command.rb +57 -40
  365. data/lib/rubygems/commands/contents_command.rb +37 -39
  366. data/lib/rubygems/commands/dependency_command.rb +53 -65
  367. data/lib/rubygems/commands/environment_command.rb +32 -16
  368. data/lib/rubygems/commands/exec_command.rb +249 -0
  369. data/lib/rubygems/commands/fetch_command.rb +36 -19
  370. data/lib/rubygems/commands/generate_index_command.rb +40 -74
  371. data/lib/rubygems/commands/help_command.rb +22 -22
  372. data/lib/rubygems/commands/info_command.rb +38 -0
  373. data/lib/rubygems/commands/install_command.rb +67 -143
  374. data/lib/rubygems/commands/list_command.rb +10 -9
  375. data/lib/rubygems/commands/lock_command.rb +12 -14
  376. data/lib/rubygems/commands/mirror_command.rb +4 -4
  377. data/lib/rubygems/commands/open_command.rb +28 -26
  378. data/lib/rubygems/commands/outdated_command.rb +6 -6
  379. data/lib/rubygems/commands/owner_command.rb +51 -27
  380. data/lib/rubygems/commands/pristine_command.rb +99 -71
  381. data/lib/rubygems/commands/push_command.rb +53 -46
  382. data/lib/rubygems/commands/query_command.rb +21 -328
  383. data/lib/rubygems/commands/rdoc_command.rb +33 -33
  384. data/lib/rubygems/commands/search_command.rb +9 -9
  385. data/lib/rubygems/commands/server_command.rb +15 -76
  386. data/lib/rubygems/commands/setup_command.rb +370 -186
  387. data/lib/rubygems/commands/signin_command.rb +34 -0
  388. data/lib/rubygems/commands/signout_command.rb +32 -0
  389. data/lib/rubygems/commands/sources_command.rb +57 -41
  390. data/lib/rubygems/commands/specification_command.rb +38 -28
  391. data/lib/rubygems/commands/stale_command.rb +6 -5
  392. data/lib/rubygems/commands/uninstall_command.rb +96 -62
  393. data/lib/rubygems/commands/unpack_command.rb +44 -53
  394. data/lib/rubygems/commands/update_command.rb +153 -99
  395. data/lib/rubygems/commands/which_command.rb +14 -17
  396. data/lib/rubygems/commands/yank_command.rb +28 -32
  397. data/lib/rubygems/compatibility.rb +13 -32
  398. data/lib/rubygems/config_file.rb +214 -119
  399. data/lib/rubygems/core_ext/kernel_gem.rb +10 -16
  400. data/lib/rubygems/core_ext/kernel_require.rb +92 -90
  401. data/lib/rubygems/core_ext/kernel_warn.rb +49 -0
  402. data/lib/rubygems/core_ext/tcpsocket_init.rb +54 -0
  403. data/lib/rubygems/defaults.rb +184 -54
  404. data/lib/rubygems/dependency.rb +75 -62
  405. data/lib/rubygems/dependency_installer.rb +74 -230
  406. data/lib/rubygems/dependency_list.rb +32 -33
  407. data/lib/rubygems/deprecate.rb +113 -17
  408. data/lib/rubygems/doctor.rb +31 -31
  409. data/lib/rubygems/errors.rb +51 -13
  410. data/lib/rubygems/exceptions.rb +65 -35
  411. data/lib/rubygems/ext/build_error.rb +3 -1
  412. data/lib/rubygems/ext/builder.rb +103 -77
  413. data/lib/rubygems/ext/cargo_builder/link_flag_converter.rb +27 -0
  414. data/lib/rubygems/ext/cargo_builder.rb +360 -0
  415. data/lib/rubygems/ext/cmake_builder.rb +6 -7
  416. data/lib/rubygems/ext/configure_builder.rb +6 -9
  417. data/lib/rubygems/ext/ext_conf_builder.rb +40 -61
  418. data/lib/rubygems/ext/rake_builder.rb +18 -21
  419. data/lib/rubygems/ext.rb +8 -7
  420. data/lib/rubygems/gem_runner.rb +19 -21
  421. data/lib/rubygems/gemcutter_utilities/webauthn_listener/response.rb +163 -0
  422. data/lib/rubygems/gemcutter_utilities/webauthn_listener.rb +105 -0
  423. data/lib/rubygems/gemcutter_utilities/webauthn_poller.rb +78 -0
  424. data/lib/rubygems/gemcutter_utilities.rb +246 -43
  425. data/lib/rubygems/install_default_message.rb +3 -3
  426. data/lib/rubygems/install_message.rb +3 -3
  427. data/lib/rubygems/install_update_options.rb +104 -98
  428. data/lib/rubygems/installer.rb +407 -225
  429. data/lib/rubygems/installer_uninstaller_utils.rb +27 -0
  430. data/lib/rubygems/local_remote_options.rb +29 -32
  431. data/lib/rubygems/name_tuple.rb +18 -23
  432. data/lib/rubygems/net/http.rb +3 -0
  433. data/lib/rubygems/net-http/.document +1 -0
  434. data/lib/rubygems/net-http/LICENSE.txt +22 -0
  435. data/lib/rubygems/net-http/lib/net/http/backward.rb +40 -0
  436. data/lib/rubygems/net-http/lib/net/http/exceptions.rb +34 -0
  437. data/lib/rubygems/net-http/lib/net/http/generic_request.rb +414 -0
  438. data/lib/rubygems/net-http/lib/net/http/header.rb +981 -0
  439. data/lib/rubygems/net-http/lib/net/http/proxy_delta.rb +17 -0
  440. data/lib/rubygems/net-http/lib/net/http/request.rb +88 -0
  441. data/lib/rubygems/net-http/lib/net/http/requests.rb +425 -0
  442. data/lib/rubygems/net-http/lib/net/http/response.rb +738 -0
  443. data/lib/rubygems/net-http/lib/net/http/responses.rb +1174 -0
  444. data/lib/rubygems/net-http/lib/net/http/status.rb +84 -0
  445. data/lib/rubygems/net-http/lib/net/http.rb +2496 -0
  446. data/lib/rubygems/net-http/lib/net/https.rb +23 -0
  447. data/lib/rubygems/net-protocol/.document +1 -0
  448. data/lib/rubygems/net-protocol/LICENSE.txt +22 -0
  449. data/lib/rubygems/net-protocol/lib/net/protocol.rb +544 -0
  450. data/lib/rubygems/openssl.rb +7 -0
  451. data/lib/rubygems/optparse/.document +1 -0
  452. data/lib/rubygems/optparse/COPYING +56 -0
  453. data/lib/rubygems/optparse/lib/optionparser.rb +2 -0
  454. data/lib/rubygems/optparse/lib/optparse/ac.rb +54 -0
  455. data/lib/rubygems/optparse/lib/optparse/date.rb +18 -0
  456. data/lib/rubygems/optparse/lib/optparse/kwargs.rb +22 -0
  457. data/lib/rubygems/optparse/lib/optparse/shellwords.rb +7 -0
  458. data/lib/rubygems/optparse/lib/optparse/time.rb +11 -0
  459. data/lib/rubygems/optparse/lib/optparse/uri.rb +7 -0
  460. data/lib/rubygems/optparse/lib/optparse/version.rb +71 -0
  461. data/lib/rubygems/optparse/lib/optparse.rb +2330 -0
  462. data/lib/rubygems/optparse.rb +3 -0
  463. data/lib/rubygems/package/digest_io.rb +5 -7
  464. data/lib/rubygems/package/file_source.rb +6 -8
  465. data/lib/rubygems/package/io_source.rb +6 -4
  466. data/lib/rubygems/package/old.rb +19 -28
  467. data/lib/rubygems/package/source.rb +1 -1
  468. data/lib/rubygems/package/tar_header.rb +114 -93
  469. data/lib/rubygems/package/tar_reader/entry.rb +116 -22
  470. data/lib/rubygems/package/tar_reader.rb +18 -40
  471. data/lib/rubygems/package/tar_writer.rb +42 -56
  472. data/lib/rubygems/package.rb +254 -144
  473. data/lib/rubygems/package_task.rb +8 -14
  474. data/lib/rubygems/path_support.rb +21 -16
  475. data/lib/rubygems/platform.rb +135 -74
  476. data/lib/rubygems/psych_tree.rb +3 -2
  477. data/lib/rubygems/query_utils.rb +349 -0
  478. data/lib/rubygems/rdoc.rb +4 -326
  479. data/lib/rubygems/remote_fetcher.rb +99 -181
  480. data/lib/rubygems/request/connection_pools.rb +31 -24
  481. data/lib/rubygems/request/http_pool.rb +6 -7
  482. data/lib/rubygems/request/https_pool.rb +2 -3
  483. data/lib/rubygems/request.rb +107 -57
  484. data/lib/rubygems/request_set/gem_dependency_api.rb +182 -190
  485. data/lib/rubygems/request_set/lockfile/parser.rb +42 -52
  486. data/lib/rubygems/request_set/lockfile/tokenizer.rb +34 -24
  487. data/lib/rubygems/request_set/lockfile.rb +32 -35
  488. data/lib/rubygems/request_set.rb +115 -71
  489. data/lib/rubygems/requirement.rb +77 -68
  490. data/lib/rubygems/resolv/.document +1 -0
  491. data/lib/rubygems/resolv/LICENSE.txt +22 -0
  492. data/lib/rubygems/resolv/lib/resolv.rb +3387 -0
  493. data/lib/rubygems/resolver/activation_request.rb +34 -61
  494. data/lib/rubygems/resolver/api_set/gem_parser.rb +24 -0
  495. data/lib/rubygems/resolver/api_set.rb +40 -32
  496. data/lib/rubygems/resolver/api_specification.rb +37 -18
  497. data/lib/rubygems/resolver/best_set.rb +15 -17
  498. data/lib/rubygems/resolver/composed_set.rb +9 -11
  499. data/lib/rubygems/resolver/conflict.rb +19 -33
  500. data/lib/rubygems/resolver/current_set.rb +2 -4
  501. data/lib/rubygems/resolver/dependency_request.rb +8 -9
  502. data/lib/rubygems/resolver/git_set.rb +8 -10
  503. data/lib/rubygems/resolver/git_specification.rb +11 -13
  504. data/lib/rubygems/resolver/index_set.rb +13 -15
  505. data/lib/rubygems/resolver/index_specification.rb +42 -11
  506. data/lib/rubygems/resolver/installed_specification.rb +9 -11
  507. data/lib/rubygems/resolver/installer_set.rb +93 -47
  508. data/lib/rubygems/resolver/local_specification.rb +5 -7
  509. data/lib/rubygems/resolver/lock_set.rb +13 -15
  510. data/lib/rubygems/resolver/lock_specification.rb +13 -15
  511. data/lib/rubygems/resolver/molinillo/.document +1 -0
  512. data/lib/rubygems/resolver/molinillo/LICENSE +9 -0
  513. data/lib/rubygems/resolver/molinillo/lib/molinillo/delegates/resolution_state.rb +57 -0
  514. data/lib/rubygems/resolver/molinillo/lib/molinillo/delegates/specification_provider.rb +88 -0
  515. data/lib/rubygems/resolver/molinillo/lib/molinillo/dependency_graph/action.rb +36 -0
  516. data/lib/rubygems/resolver/molinillo/lib/molinillo/dependency_graph/add_edge_no_circular.rb +66 -0
  517. data/lib/rubygems/resolver/molinillo/lib/molinillo/dependency_graph/add_vertex.rb +62 -0
  518. data/lib/rubygems/resolver/molinillo/lib/molinillo/dependency_graph/delete_edge.rb +63 -0
  519. data/lib/rubygems/resolver/molinillo/lib/molinillo/dependency_graph/detach_vertex_named.rb +61 -0
  520. data/lib/rubygems/resolver/molinillo/lib/molinillo/dependency_graph/log.rb +126 -0
  521. data/lib/rubygems/resolver/molinillo/lib/molinillo/dependency_graph/set_payload.rb +46 -0
  522. data/lib/rubygems/resolver/molinillo/lib/molinillo/dependency_graph/tag.rb +36 -0
  523. data/lib/rubygems/resolver/molinillo/lib/molinillo/dependency_graph/vertex.rb +164 -0
  524. data/lib/rubygems/resolver/molinillo/lib/molinillo/dependency_graph.rb +110 -146
  525. data/lib/rubygems/resolver/molinillo/lib/molinillo/errors.rb +83 -9
  526. data/lib/rubygems/resolver/molinillo/lib/molinillo/gem_metadata.rb +2 -1
  527. data/lib/rubygems/resolver/molinillo/lib/molinillo/modules/specification_provider.rb +13 -1
  528. data/lib/rubygems/resolver/molinillo/lib/molinillo/modules/ui.rb +3 -1
  529. data/lib/rubygems/resolver/molinillo/lib/molinillo/resolution.rb +552 -172
  530. data/lib/rubygems/resolver/molinillo/lib/molinillo/resolver.rb +3 -2
  531. data/lib/rubygems/resolver/molinillo/lib/molinillo/state.rb +12 -6
  532. data/lib/rubygems/resolver/molinillo/lib/molinillo.rb +6 -5
  533. data/lib/rubygems/resolver/molinillo.rb +2 -1
  534. data/lib/rubygems/resolver/requirement_list.rb +2 -2
  535. data/lib/rubygems/resolver/set.rb +3 -5
  536. data/lib/rubygems/resolver/source_set.rb +6 -7
  537. data/lib/rubygems/resolver/spec_specification.rb +16 -4
  538. data/lib/rubygems/resolver/specification.rb +25 -10
  539. data/lib/rubygems/resolver/stats.rb +1 -0
  540. data/lib/rubygems/resolver/vendor_set.rb +6 -8
  541. data/lib/rubygems/resolver/vendor_specification.rb +6 -8
  542. data/lib/rubygems/resolver.rb +121 -72
  543. data/lib/rubygems/s3_uri_signer.rb +177 -0
  544. data/lib/rubygems/safe_marshal/elements.rb +146 -0
  545. data/lib/rubygems/safe_marshal/reader.rb +308 -0
  546. data/lib/rubygems/safe_marshal/visitors/stream_printer.rb +31 -0
  547. data/lib/rubygems/safe_marshal/visitors/to_ruby.rb +415 -0
  548. data/lib/rubygems/safe_marshal/visitors/visitor.rb +74 -0
  549. data/lib/rubygems/safe_marshal.rb +74 -0
  550. data/lib/rubygems/safe_yaml.rb +36 -0
  551. data/lib/rubygems/security/policies.rb +49 -51
  552. data/lib/rubygems/security/policy.rb +46 -53
  553. data/lib/rubygems/security/signer.rb +86 -29
  554. data/lib/rubygems/security/trust_dir.rb +22 -24
  555. data/lib/rubygems/security.rb +101 -82
  556. data/lib/rubygems/security_option.rb +43 -0
  557. data/lib/rubygems/shellwords.rb +3 -0
  558. data/lib/rubygems/source/git.rb +40 -42
  559. data/lib/rubygems/source/installed.rb +5 -9
  560. data/lib/rubygems/source/local.rb +30 -33
  561. data/lib/rubygems/source/lock.rb +10 -10
  562. data/lib/rubygems/source/specific_file.rb +7 -9
  563. data/lib/rubygems/source/vendor.rb +3 -7
  564. data/lib/rubygems/source.rb +78 -68
  565. data/lib/rubygems/source_list.rb +11 -15
  566. data/lib/rubygems/spec_fetcher.rb +71 -83
  567. data/lib/rubygems/specification.rb +833 -1115
  568. data/lib/rubygems/specification_policy.rb +538 -0
  569. data/lib/rubygems/ssl_certs/rubygems.org/GlobalSignRootCA.pem +21 -0
  570. data/lib/rubygems/ssl_certs/rubygems.org/GlobalSignRootCA_R3.pem +21 -0
  571. data/lib/rubygems/stub_specification.rb +55 -57
  572. data/lib/rubygems/text.rb +39 -27
  573. data/lib/rubygems/timeout/.document +1 -0
  574. data/lib/rubygems/timeout/LICENSE.txt +22 -0
  575. data/lib/rubygems/timeout/lib/timeout.rb +199 -0
  576. data/lib/rubygems/timeout.rb +3 -0
  577. data/lib/rubygems/tsort/.document +1 -0
  578. data/lib/rubygems/tsort/LICENSE.txt +22 -0
  579. data/lib/rubygems/tsort/lib/tsort.rb +455 -0
  580. data/lib/rubygems/tsort.rb +3 -0
  581. data/lib/rubygems/uninstaller.rb +150 -87
  582. data/lib/rubygems/unknown_command_spell_checker.rb +21 -0
  583. data/lib/rubygems/update_suggestion.rb +56 -0
  584. data/lib/rubygems/uri.rb +126 -0
  585. data/lib/rubygems/uri_formatter.rb +4 -7
  586. data/lib/rubygems/user_interaction.rb +92 -146
  587. data/lib/rubygems/util/licenses.rb +712 -317
  588. data/lib/rubygems/util/list.rb +5 -2
  589. data/lib/rubygems/util.rb +58 -75
  590. data/lib/rubygems/validator.rb +26 -48
  591. data/lib/rubygems/version.rb +106 -56
  592. data/lib/rubygems/version_option.rb +22 -14
  593. data/lib/rubygems/yaml_serializer.rb +93 -0
  594. data/lib/rubygems.rb +453 -358
  595. data/rubygems-update.gemspec +38 -0
  596. data/setup.rb +13 -27
  597. metadata +511 -363
  598. data/.autotest +0 -71
  599. data/.document +0 -5
  600. data/.travis.yml +0 -46
  601. data/CONTRIBUTING.rdoc +0 -129
  602. data/CVE-2013-4287.txt +0 -35
  603. data/CVE-2013-4363.txt +0 -45
  604. data/CVE-2015-3900.txt +0 -40
  605. data/History.txt +0 -3094
  606. data/MAINTAINERS.md +0 -5
  607. data/POLICIES.rdoc +0 -61
  608. data/README.rdoc +0 -54
  609. data/Rakefile +0 -449
  610. data/UPGRADING.rdoc +0 -92
  611. data/appveyor.yml +0 -36
  612. data/bin/gem +0 -25
  613. data/bin/update_rubygems +0 -37
  614. data/lib/gauntlet_rubygems.rb +0 -51
  615. data/lib/rubygems/indexer.rb +0 -434
  616. data/lib/rubygems/installer_test_case.rb +0 -194
  617. data/lib/rubygems/mock_gem_ui.rb +0 -89
  618. data/lib/rubygems/package/tar_test_case.rb +0 -147
  619. data/lib/rubygems/psych_additions.rb +0 -10
  620. data/lib/rubygems/server.rb +0 -869
  621. data/lib/rubygems/source_local.rb +0 -6
  622. data/lib/rubygems/source_specific_file.rb +0 -5
  623. data/lib/rubygems/ssl_certs/AddTrustExternalCARoot-2048.pem +0 -25
  624. data/lib/rubygems/ssl_certs/AddTrustExternalCARoot.pem +0 -32
  625. data/lib/rubygems/ssl_certs/Class3PublicPrimaryCertificationAuthority.pem +0 -14
  626. data/lib/rubygems/ssl_certs/DigiCertHighAssuranceEVRootCA.pem +0 -23
  627. data/lib/rubygems/ssl_certs/EntrustnetSecureServerCertificationAuthority.pem +0 -28
  628. data/lib/rubygems/ssl_certs/GeoTrustGlobalCA.pem +0 -20
  629. data/lib/rubygems/syck_hack.rb +0 -77
  630. data/lib/rubygems/test_case.rb +0 -1496
  631. data/lib/rubygems/test_utilities.rb +0 -384
  632. data/lib/ubygems.rb +0 -11
  633. data/test/rubygems/alternate_cert.pem +0 -18
  634. data/test/rubygems/alternate_cert_32.pem +0 -18
  635. data/test/rubygems/alternate_key.pem +0 -27
  636. data/test/rubygems/bad_rake.rb +0 -2
  637. data/test/rubygems/ca_cert.pem +0 -68
  638. data/test/rubygems/child_cert.pem +0 -18
  639. data/test/rubygems/child_cert_32.pem +0 -18
  640. data/test/rubygems/child_key.pem +0 -27
  641. data/test/rubygems/client.pem +0 -49
  642. data/test/rubygems/data/gem-private_key.pem +0 -27
  643. data/test/rubygems/data/gem-public_cert.pem +0 -20
  644. data/test/rubygems/data/null-type.gemspec.rz +0 -0
  645. data/test/rubygems/encrypted_private_key.pem +0 -30
  646. data/test/rubygems/expired_cert.pem +0 -18
  647. data/test/rubygems/fake_certlib/openssl.rb +0 -8
  648. data/test/rubygems/fix_openssl_warnings.rb +0 -13
  649. data/test/rubygems/foo/discover.rb +0 -1
  650. data/test/rubygems/future_cert.pem +0 -18
  651. data/test/rubygems/future_cert_32.pem +0 -18
  652. data/test/rubygems/good_rake.rb +0 -2
  653. data/test/rubygems/grandchild_cert.pem +0 -18
  654. data/test/rubygems/grandchild_cert_32.pem +0 -18
  655. data/test/rubygems/grandchild_key.pem +0 -27
  656. data/test/rubygems/invalid_client.pem +0 -49
  657. data/test/rubygems/invalid_issuer_cert.pem +0 -18
  658. data/test/rubygems/invalid_issuer_cert_32.pem +0 -18
  659. data/test/rubygems/invalid_key.pem +0 -27
  660. data/test/rubygems/invalid_signer_cert.pem +0 -18
  661. data/test/rubygems/invalid_signer_cert_32.pem +0 -18
  662. data/test/rubygems/invalidchild_cert.pem +0 -18
  663. data/test/rubygems/invalidchild_cert_32.pem +0 -18
  664. data/test/rubygems/invalidchild_key.pem +0 -27
  665. data/test/rubygems/plugin/exception/rubygems_plugin.rb +0 -3
  666. data/test/rubygems/plugin/load/rubygems_plugin.rb +0 -4
  667. data/test/rubygems/plugin/standarderror/rubygems_plugin.rb +0 -3
  668. data/test/rubygems/private_key.pem +0 -27
  669. data/test/rubygems/public_cert.pem +0 -18
  670. data/test/rubygems/public_cert_32.pem +0 -18
  671. data/test/rubygems/public_key.pem +0 -9
  672. data/test/rubygems/rubygems/commands/crash_command.rb +0 -6
  673. data/test/rubygems/rubygems_plugin.rb +0 -22
  674. data/test/rubygems/sff/discover.rb +0 -1
  675. data/test/rubygems/simple_gem.rb +0 -67
  676. data/test/rubygems/specifications/bar-0.0.2.gemspec +0 -9
  677. data/test/rubygems/specifications/foo-0.0.1-x86-mswin32.gemspec +0 -0
  678. data/test/rubygems/ssl_cert.pem +0 -19
  679. data/test/rubygems/ssl_key.pem +0 -15
  680. data/test/rubygems/test_bundled_ca.rb +0 -59
  681. data/test/rubygems/test_config.rb +0 -24
  682. data/test/rubygems/test_deprecate.rb +0 -77
  683. data/test/rubygems/test_gem.rb +0 -1679
  684. data/test/rubygems/test_gem_available_set.rb +0 -130
  685. data/test/rubygems/test_gem_command.rb +0 -248
  686. data/test/rubygems/test_gem_command_manager.rb +0 -264
  687. data/test/rubygems/test_gem_commands_build_command.rb +0 -121
  688. data/test/rubygems/test_gem_commands_cert_command.rb +0 -671
  689. data/test/rubygems/test_gem_commands_check_command.rb +0 -69
  690. data/test/rubygems/test_gem_commands_cleanup_command.rb +0 -197
  691. data/test/rubygems/test_gem_commands_contents_command.rb +0 -240
  692. data/test/rubygems/test_gem_commands_dependency_command.rb +0 -230
  693. data/test/rubygems/test_gem_commands_environment_command.rb +0 -154
  694. data/test/rubygems/test_gem_commands_fetch_command.rb +0 -127
  695. data/test/rubygems/test_gem_commands_generate_index_command.rb +0 -51
  696. data/test/rubygems/test_gem_commands_help_command.rb +0 -75
  697. data/test/rubygems/test_gem_commands_install_command.rb +0 -987
  698. data/test/rubygems/test_gem_commands_list_command.rb +0 -34
  699. data/test/rubygems/test_gem_commands_lock_command.rb +0 -69
  700. data/test/rubygems/test_gem_commands_mirror.rb +0 -20
  701. data/test/rubygems/test_gem_commands_open_command.rb +0 -70
  702. data/test/rubygems/test_gem_commands_outdated_command.rb +0 -33
  703. data/test/rubygems/test_gem_commands_owner_command.rb +0 -205
  704. data/test/rubygems/test_gem_commands_pristine_command.rb +0 -491
  705. data/test/rubygems/test_gem_commands_push_command.rb +0 -330
  706. data/test/rubygems/test_gem_commands_query_command.rb +0 -657
  707. data/test/rubygems/test_gem_commands_search_command.rb +0 -18
  708. data/test/rubygems/test_gem_commands_server_command.rb +0 -60
  709. data/test/rubygems/test_gem_commands_setup_command.rb +0 -136
  710. data/test/rubygems/test_gem_commands_sources_command.rb +0 -249
  711. data/test/rubygems/test_gem_commands_specification_command.rb +0 -251
  712. data/test/rubygems/test_gem_commands_stale_command.rb +0 -43
  713. data/test/rubygems/test_gem_commands_uninstall_command.rb +0 -283
  714. data/test/rubygems/test_gem_commands_unpack_command.rb +0 -209
  715. data/test/rubygems/test_gem_commands_update_command.rb +0 -514
  716. data/test/rubygems/test_gem_commands_which_command.rb +0 -87
  717. data/test/rubygems/test_gem_commands_yank_command.rb +0 -100
  718. data/test/rubygems/test_gem_config_file.rb +0 -491
  719. data/test/rubygems/test_gem_dependency.rb +0 -362
  720. data/test/rubygems/test_gem_dependency_installer.rb +0 -1235
  721. data/test/rubygems/test_gem_dependency_list.rb +0 -260
  722. data/test/rubygems/test_gem_dependency_resolution_error.rb +0 -29
  723. data/test/rubygems/test_gem_doctor.rb +0 -169
  724. data/test/rubygems/test_gem_ext_builder.rb +0 -341
  725. data/test/rubygems/test_gem_ext_cmake_builder.rb +0 -87
  726. data/test/rubygems/test_gem_ext_configure_builder.rb +0 -87
  727. data/test/rubygems/test_gem_ext_ext_conf_builder.rb +0 -211
  728. data/test/rubygems/test_gem_ext_rake_builder.rb +0 -65
  729. data/test/rubygems/test_gem_gem_runner.rb +0 -69
  730. data/test/rubygems/test_gem_gemcutter_utilities.rb +0 -235
  731. data/test/rubygems/test_gem_impossible_dependencies_error.rb +0 -62
  732. data/test/rubygems/test_gem_indexer.rb +0 -367
  733. data/test/rubygems/test_gem_install_update_options.rb +0 -197
  734. data/test/rubygems/test_gem_installer.rb +0 -1552
  735. data/test/rubygems/test_gem_local_remote_options.rb +0 -134
  736. data/test/rubygems/test_gem_name_tuple.rb +0 -45
  737. data/test/rubygems/test_gem_package.rb +0 -876
  738. data/test/rubygems/test_gem_package_old.rb +0 -90
  739. data/test/rubygems/test_gem_package_tar_header.rb +0 -147
  740. data/test/rubygems/test_gem_package_tar_reader.rb +0 -90
  741. data/test/rubygems/test_gem_package_tar_reader_entry.rb +0 -142
  742. data/test/rubygems/test_gem_package_tar_writer.rb +0 -264
  743. data/test/rubygems/test_gem_package_task.rb +0 -84
  744. data/test/rubygems/test_gem_path_support.rb +0 -121
  745. data/test/rubygems/test_gem_platform.rb +0 -308
  746. data/test/rubygems/test_gem_rdoc.rb +0 -270
  747. data/test/rubygems/test_gem_remote_fetcher.rb +0 -1051
  748. data/test/rubygems/test_gem_request.rb +0 -363
  749. data/test/rubygems/test_gem_request_connection_pools.rb +0 -130
  750. data/test/rubygems/test_gem_request_set.rb +0 -595
  751. data/test/rubygems/test_gem_request_set_gem_dependency_api.rb +0 -831
  752. data/test/rubygems/test_gem_request_set_lockfile.rb +0 -470
  753. data/test/rubygems/test_gem_request_set_lockfile_parser.rb +0 -549
  754. data/test/rubygems/test_gem_request_set_lockfile_tokenizer.rb +0 -306
  755. data/test/rubygems/test_gem_requirement.rb +0 -387
  756. data/test/rubygems/test_gem_resolver.rb +0 -715
  757. data/test/rubygems/test_gem_resolver_activation_request.rb +0 -74
  758. data/test/rubygems/test_gem_resolver_api_set.rb +0 -209
  759. data/test/rubygems/test_gem_resolver_api_specification.rb +0 -145
  760. data/test/rubygems/test_gem_resolver_best_set.rb +0 -138
  761. data/test/rubygems/test_gem_resolver_composed_set.rb +0 -46
  762. data/test/rubygems/test_gem_resolver_conflict.rb +0 -88
  763. data/test/rubygems/test_gem_resolver_dependency_request.rb +0 -85
  764. data/test/rubygems/test_gem_resolver_git_set.rb +0 -190
  765. data/test/rubygems/test_gem_resolver_git_specification.rb +0 -114
  766. data/test/rubygems/test_gem_resolver_index_set.rb +0 -90
  767. data/test/rubygems/test_gem_resolver_index_specification.rb +0 -90
  768. data/test/rubygems/test_gem_resolver_installed_specification.rb +0 -50
  769. data/test/rubygems/test_gem_resolver_installer_set.rb +0 -258
  770. data/test/rubygems/test_gem_resolver_local_specification.rb +0 -46
  771. data/test/rubygems/test_gem_resolver_lock_set.rb +0 -64
  772. data/test/rubygems/test_gem_resolver_lock_specification.rb +0 -100
  773. data/test/rubygems/test_gem_resolver_requirement_list.rb +0 -21
  774. data/test/rubygems/test_gem_resolver_specification.rb +0 -65
  775. data/test/rubygems/test_gem_resolver_vendor_set.rb +0 -84
  776. data/test/rubygems/test_gem_resolver_vendor_specification.rb +0 -84
  777. data/test/rubygems/test_gem_security.rb +0 -307
  778. data/test/rubygems/test_gem_security_policy.rb +0 -541
  779. data/test/rubygems/test_gem_security_signer.rb +0 -209
  780. data/test/rubygems/test_gem_security_trust_dir.rb +0 -101
  781. data/test/rubygems/test_gem_server.rb +0 -409
  782. data/test/rubygems/test_gem_silent_ui.rb +0 -117
  783. data/test/rubygems/test_gem_source.rb +0 -242
  784. data/test/rubygems/test_gem_source_fetch_problem.rb +0 -20
  785. data/test/rubygems/test_gem_source_git.rb +0 -309
  786. data/test/rubygems/test_gem_source_installed.rb +0 -37
  787. data/test/rubygems/test_gem_source_list.rb +0 -118
  788. data/test/rubygems/test_gem_source_local.rb +0 -107
  789. data/test/rubygems/test_gem_source_lock.rb +0 -115
  790. data/test/rubygems/test_gem_source_specific_file.rb +0 -76
  791. data/test/rubygems/test_gem_source_vendor.rb +0 -32
  792. data/test/rubygems/test_gem_spec_fetcher.rb +0 -311
  793. data/test/rubygems/test_gem_specification.rb +0 -3519
  794. data/test/rubygems/test_gem_stream_ui.rb +0 -239
  795. data/test/rubygems/test_gem_stub_specification.rb +0 -217
  796. data/test/rubygems/test_gem_text.rb +0 -77
  797. data/test/rubygems/test_gem_uninstaller.rb +0 -485
  798. data/test/rubygems/test_gem_unsatisfiable_dependency_error.rb +0 -33
  799. data/test/rubygems/test_gem_uri_formatter.rb +0 -29
  800. data/test/rubygems/test_gem_util.rb +0 -40
  801. data/test/rubygems/test_gem_validator.rb +0 -46
  802. data/test/rubygems/test_gem_version.rb +0 -222
  803. data/test/rubygems/test_gem_version_option.rb +0 -152
  804. data/test/rubygems/test_kernel.rb +0 -86
  805. data/test/rubygems/test_require.rb +0 -322
  806. data/test/rubygems/wrong_key_cert.pem +0 -18
  807. data/test/rubygems/wrong_key_cert_32.pem +0 -18
  808. data/util/CL2notes +0 -56
  809. data/util/create_certs.rb +0 -157
  810. data/util/create_encrypted_key.rb +0 -17
  811. data/util/generate_spdx_license_list.rb +0 -51
  812. data/util/update_bundled_ca_certificates.rb +0 -118
@@ -0,0 +1,2330 @@
1
+ # frozen_string_literal: true
2
+ #
3
+ # optparse.rb - command-line option analysis with the Gem::OptionParser class.
4
+ #
5
+ # Author:: Nobu Nakada
6
+ # Documentation:: Nobu Nakada and Gavin Sinclair.
7
+ #
8
+ # See Gem::OptionParser for documentation.
9
+ #
10
+
11
+
12
+ #--
13
+ # == Developer Documentation (not for RDoc output)
14
+ #
15
+ # === Class tree
16
+ #
17
+ # - Gem::OptionParser:: front end
18
+ # - Gem::OptionParser::Switch:: each switches
19
+ # - Gem::OptionParser::List:: options list
20
+ # - Gem::OptionParser::ParseError:: errors on parsing
21
+ # - Gem::OptionParser::AmbiguousOption
22
+ # - Gem::OptionParser::NeedlessArgument
23
+ # - Gem::OptionParser::MissingArgument
24
+ # - Gem::OptionParser::InvalidOption
25
+ # - Gem::OptionParser::InvalidArgument
26
+ # - Gem::OptionParser::AmbiguousArgument
27
+ #
28
+ # === Object relationship diagram
29
+ #
30
+ # +--------------+
31
+ # | Gem::OptionParser |<>-----+
32
+ # +--------------+ | +--------+
33
+ # | ,-| Switch |
34
+ # on_head -------->+---------------+ / +--------+
35
+ # accept/reject -->| List |<|>-
36
+ # | |<|>- +----------+
37
+ # on ------------->+---------------+ `-| argument |
38
+ # : : | class |
39
+ # +---------------+ |==========|
40
+ # on_tail -------->| | |pattern |
41
+ # +---------------+ |----------|
42
+ # Gem::OptionParser.accept ->| DefaultList | |converter |
43
+ # reject |(shared between| +----------+
44
+ # | all instances)|
45
+ # +---------------+
46
+ #
47
+ #++
48
+ #
49
+ # == Gem::OptionParser
50
+ #
51
+ # === New to +Gem::OptionParser+?
52
+ #
53
+ # See the {Tutorial}[optparse/tutorial.rdoc].
54
+ #
55
+ # === Introduction
56
+ #
57
+ # Gem::OptionParser is a class for command-line option analysis. It is much more
58
+ # advanced, yet also easier to use, than GetoptLong, and is a more Ruby-oriented
59
+ # solution.
60
+ #
61
+ # === Features
62
+ #
63
+ # 1. The argument specification and the code to handle it are written in the
64
+ # same place.
65
+ # 2. It can output an option summary; you don't need to maintain this string
66
+ # separately.
67
+ # 3. Optional and mandatory arguments are specified very gracefully.
68
+ # 4. Arguments can be automatically converted to a specified class.
69
+ # 5. Arguments can be restricted to a certain set.
70
+ #
71
+ # All of these features are demonstrated in the examples below. See
72
+ # #make_switch for full documentation.
73
+ #
74
+ # === Minimal example
75
+ #
76
+ # require 'rubygems/optparse/lib/optparse'
77
+ #
78
+ # options = {}
79
+ # Gem::OptionParser.new do |parser|
80
+ # parser.banner = "Usage: example.rb [options]"
81
+ #
82
+ # parser.on("-v", "--[no-]verbose", "Run verbosely") do |v|
83
+ # options[:verbose] = v
84
+ # end
85
+ # end.parse!
86
+ #
87
+ # p options
88
+ # p ARGV
89
+ #
90
+ # === Generating Help
91
+ #
92
+ # Gem::OptionParser can be used to automatically generate help for the commands you
93
+ # write:
94
+ #
95
+ # require 'rubygems/optparse/lib/optparse'
96
+ #
97
+ # Options = Struct.new(:name)
98
+ #
99
+ # class Parser
100
+ # def self.parse(options)
101
+ # args = Options.new("world")
102
+ #
103
+ # opt_parser = Gem::OptionParser.new do |parser|
104
+ # parser.banner = "Usage: example.rb [options]"
105
+ #
106
+ # parser.on("-nNAME", "--name=NAME", "Name to say hello to") do |n|
107
+ # args.name = n
108
+ # end
109
+ #
110
+ # parser.on("-h", "--help", "Prints this help") do
111
+ # puts parser
112
+ # exit
113
+ # end
114
+ # end
115
+ #
116
+ # opt_parser.parse!(options)
117
+ # return args
118
+ # end
119
+ # end
120
+ # options = Parser.parse %w[--help]
121
+ #
122
+ # #=>
123
+ # # Usage: example.rb [options]
124
+ # # -n, --name=NAME Name to say hello to
125
+ # # -h, --help Prints this help
126
+ #
127
+ # === Required Arguments
128
+ #
129
+ # For options that require an argument, option specification strings may include an
130
+ # option name in all caps. If an option is used without the required argument,
131
+ # an exception will be raised.
132
+ #
133
+ # require 'rubygems/optparse/lib/optparse'
134
+ #
135
+ # options = {}
136
+ # Gem::OptionParser.new do |parser|
137
+ # parser.on("-r", "--require LIBRARY",
138
+ # "Require the LIBRARY before executing your script") do |lib|
139
+ # puts "You required #{lib}!"
140
+ # end
141
+ # end.parse!
142
+ #
143
+ # Used:
144
+ #
145
+ # $ ruby optparse-test.rb -r
146
+ # optparse-test.rb:9:in `<main>': missing argument: -r (Gem::OptionParser::MissingArgument)
147
+ # $ ruby optparse-test.rb -r my-library
148
+ # You required my-library!
149
+ #
150
+ # === Type Coercion
151
+ #
152
+ # Gem::OptionParser supports the ability to coerce command line arguments
153
+ # into objects for us.
154
+ #
155
+ # Gem::OptionParser comes with a few ready-to-use kinds of type
156
+ # coercion. They are:
157
+ #
158
+ # - Date -- Anything accepted by +Date.parse+ (need to require +optparse/date+)
159
+ # - DateTime -- Anything accepted by +DateTime.parse+ (need to require +optparse/date+)
160
+ # - Time -- Anything accepted by +Time.httpdate+ or +Time.parse+ (need to require +optparse/time+)
161
+ # - URI -- Anything accepted by +URI.parse+ (need to require +optparse/uri+)
162
+ # - Shellwords -- Anything accepted by +Shellwords.shellwords+ (need to require +optparse/shellwords+)
163
+ # - String -- Any non-empty string
164
+ # - Integer -- Any integer. Will convert octal. (e.g. 124, -3, 040)
165
+ # - Float -- Any float. (e.g. 10, 3.14, -100E+13)
166
+ # - Numeric -- Any integer, float, or rational (1, 3.4, 1/3)
167
+ # - DecimalInteger -- Like +Integer+, but no octal format.
168
+ # - OctalInteger -- Like +Integer+, but no decimal format.
169
+ # - DecimalNumeric -- Decimal integer or float.
170
+ # - TrueClass -- Accepts '+, yes, true, -, no, false' and
171
+ # defaults as +true+
172
+ # - FalseClass -- Same as +TrueClass+, but defaults to +false+
173
+ # - Array -- Strings separated by ',' (e.g. 1,2,3)
174
+ # - Regexp -- Regular expressions. Also includes options.
175
+ #
176
+ # We can also add our own coercions, which we will cover below.
177
+ #
178
+ # ==== Using Built-in Conversions
179
+ #
180
+ # As an example, the built-in +Time+ conversion is used. The other built-in
181
+ # conversions behave in the same way.
182
+ # Gem::OptionParser will attempt to parse the argument
183
+ # as a +Time+. If it succeeds, that time will be passed to the
184
+ # handler block. Otherwise, an exception will be raised.
185
+ #
186
+ # require 'rubygems/optparse/lib/optparse'
187
+ # require 'rubygems/optparse/lib/optparse/time'
188
+ # Gem::OptionParser.new do |parser|
189
+ # parser.on("-t", "--time [TIME]", Time, "Begin execution at given time") do |time|
190
+ # p time
191
+ # end
192
+ # end.parse!
193
+ #
194
+ # Used:
195
+ #
196
+ # $ ruby optparse-test.rb -t nonsense
197
+ # ... invalid argument: -t nonsense (Gem::OptionParser::InvalidArgument)
198
+ # $ ruby optparse-test.rb -t 10-11-12
199
+ # 2010-11-12 00:00:00 -0500
200
+ # $ ruby optparse-test.rb -t 9:30
201
+ # 2014-08-13 09:30:00 -0400
202
+ #
203
+ # ==== Creating Custom Conversions
204
+ #
205
+ # The +accept+ method on Gem::OptionParser may be used to create converters.
206
+ # It specifies which conversion block to call whenever a class is specified.
207
+ # The example below uses it to fetch a +User+ object before the +on+ handler receives it.
208
+ #
209
+ # require 'rubygems/optparse/lib/optparse'
210
+ #
211
+ # User = Struct.new(:id, :name)
212
+ #
213
+ # def find_user id
214
+ # not_found = ->{ raise "No User Found for id #{id}" }
215
+ # [ User.new(1, "Sam"),
216
+ # User.new(2, "Gandalf") ].find(not_found) do |u|
217
+ # u.id == id
218
+ # end
219
+ # end
220
+ #
221
+ # op = Gem::OptionParser.new
222
+ # op.accept(User) do |user_id|
223
+ # find_user user_id.to_i
224
+ # end
225
+ #
226
+ # op.on("--user ID", User) do |user|
227
+ # puts user
228
+ # end
229
+ #
230
+ # op.parse!
231
+ #
232
+ # Used:
233
+ #
234
+ # $ ruby optparse-test.rb --user 1
235
+ # #<struct User id=1, name="Sam">
236
+ # $ ruby optparse-test.rb --user 2
237
+ # #<struct User id=2, name="Gandalf">
238
+ # $ ruby optparse-test.rb --user 3
239
+ # optparse-test.rb:15:in `block in find_user': No User Found for id 3 (RuntimeError)
240
+ #
241
+ # === Store options to a Hash
242
+ #
243
+ # The +into+ option of +order+, +parse+ and so on methods stores command line options into a Hash.
244
+ #
245
+ # require 'rubygems/optparse/lib/optparse'
246
+ #
247
+ # options = {}
248
+ # Gem::OptionParser.new do |parser|
249
+ # parser.on('-a')
250
+ # parser.on('-b NUM', Integer)
251
+ # parser.on('-v', '--verbose')
252
+ # end.parse!(into: options)
253
+ #
254
+ # p options
255
+ #
256
+ # Used:
257
+ #
258
+ # $ ruby optparse-test.rb -a
259
+ # {:a=>true}
260
+ # $ ruby optparse-test.rb -a -v
261
+ # {:a=>true, :verbose=>true}
262
+ # $ ruby optparse-test.rb -a -b 100
263
+ # {:a=>true, :b=>100}
264
+ #
265
+ # === Complete example
266
+ #
267
+ # The following example is a complete Ruby program. You can run it and see the
268
+ # effect of specifying various options. This is probably the best way to learn
269
+ # the features of +optparse+.
270
+ #
271
+ # require 'rubygems/optparse/lib/optparse'
272
+ # require 'rubygems/optparse/lib/optparse/time'
273
+ # require 'ostruct'
274
+ # require 'pp'
275
+ #
276
+ # class OptparseExample
277
+ # Version = '1.0.0'
278
+ #
279
+ # CODES = %w[iso-2022-jp shift_jis euc-jp utf8 binary]
280
+ # CODE_ALIASES = { "jis" => "iso-2022-jp", "sjis" => "shift_jis" }
281
+ #
282
+ # class ScriptOptions
283
+ # attr_accessor :library, :inplace, :encoding, :transfer_type,
284
+ # :verbose, :extension, :delay, :time, :record_separator,
285
+ # :list
286
+ #
287
+ # def initialize
288
+ # self.library = []
289
+ # self.inplace = false
290
+ # self.encoding = "utf8"
291
+ # self.transfer_type = :auto
292
+ # self.verbose = false
293
+ # end
294
+ #
295
+ # def define_options(parser)
296
+ # parser.banner = "Usage: example.rb [options]"
297
+ # parser.separator ""
298
+ # parser.separator "Specific options:"
299
+ #
300
+ # # add additional options
301
+ # perform_inplace_option(parser)
302
+ # delay_execution_option(parser)
303
+ # execute_at_time_option(parser)
304
+ # specify_record_separator_option(parser)
305
+ # list_example_option(parser)
306
+ # specify_encoding_option(parser)
307
+ # optional_option_argument_with_keyword_completion_option(parser)
308
+ # boolean_verbose_option(parser)
309
+ #
310
+ # parser.separator ""
311
+ # parser.separator "Common options:"
312
+ # # No argument, shows at tail. This will print an options summary.
313
+ # # Try it and see!
314
+ # parser.on_tail("-h", "--help", "Show this message") do
315
+ # puts parser
316
+ # exit
317
+ # end
318
+ # # Another typical switch to print the version.
319
+ # parser.on_tail("--version", "Show version") do
320
+ # puts Version
321
+ # exit
322
+ # end
323
+ # end
324
+ #
325
+ # def perform_inplace_option(parser)
326
+ # # Specifies an optional option argument
327
+ # parser.on("-i", "--inplace [EXTENSION]",
328
+ # "Edit ARGV files in place",
329
+ # "(make backup if EXTENSION supplied)") do |ext|
330
+ # self.inplace = true
331
+ # self.extension = ext || ''
332
+ # self.extension.sub!(/\A\.?(?=.)/, ".") # Ensure extension begins with dot.
333
+ # end
334
+ # end
335
+ #
336
+ # def delay_execution_option(parser)
337
+ # # Cast 'delay' argument to a Float.
338
+ # parser.on("--delay N", Float, "Delay N seconds before executing") do |n|
339
+ # self.delay = n
340
+ # end
341
+ # end
342
+ #
343
+ # def execute_at_time_option(parser)
344
+ # # Cast 'time' argument to a Time object.
345
+ # parser.on("-t", "--time [TIME]", Time, "Begin execution at given time") do |time|
346
+ # self.time = time
347
+ # end
348
+ # end
349
+ #
350
+ # def specify_record_separator_option(parser)
351
+ # # Cast to octal integer.
352
+ # parser.on("-F", "--irs [OCTAL]", Gem::OptionParser::OctalInteger,
353
+ # "Specify record separator (default \\0)") do |rs|
354
+ # self.record_separator = rs
355
+ # end
356
+ # end
357
+ #
358
+ # def list_example_option(parser)
359
+ # # List of arguments.
360
+ # parser.on("--list x,y,z", Array, "Example 'list' of arguments") do |list|
361
+ # self.list = list
362
+ # end
363
+ # end
364
+ #
365
+ # def specify_encoding_option(parser)
366
+ # # Keyword completion. We are specifying a specific set of arguments (CODES
367
+ # # and CODE_ALIASES - notice the latter is a Hash), and the user may provide
368
+ # # the shortest unambiguous text.
369
+ # code_list = (CODE_ALIASES.keys + CODES).join(', ')
370
+ # parser.on("--code CODE", CODES, CODE_ALIASES, "Select encoding",
371
+ # "(#{code_list})") do |encoding|
372
+ # self.encoding = encoding
373
+ # end
374
+ # end
375
+ #
376
+ # def optional_option_argument_with_keyword_completion_option(parser)
377
+ # # Optional '--type' option argument with keyword completion.
378
+ # parser.on("--type [TYPE]", [:text, :binary, :auto],
379
+ # "Select transfer type (text, binary, auto)") do |t|
380
+ # self.transfer_type = t
381
+ # end
382
+ # end
383
+ #
384
+ # def boolean_verbose_option(parser)
385
+ # # Boolean switch.
386
+ # parser.on("-v", "--[no-]verbose", "Run verbosely") do |v|
387
+ # self.verbose = v
388
+ # end
389
+ # end
390
+ # end
391
+ #
392
+ # #
393
+ # # Return a structure describing the options.
394
+ # #
395
+ # def parse(args)
396
+ # # The options specified on the command line will be collected in
397
+ # # *options*.
398
+ #
399
+ # @options = ScriptOptions.new
400
+ # @args = Gem::OptionParser.new do |parser|
401
+ # @options.define_options(parser)
402
+ # parser.parse!(args)
403
+ # end
404
+ # @options
405
+ # end
406
+ #
407
+ # attr_reader :parser, :options
408
+ # end # class OptparseExample
409
+ #
410
+ # example = OptparseExample.new
411
+ # options = example.parse(ARGV)
412
+ # pp options # example.options
413
+ # pp ARGV
414
+ #
415
+ # === Shell Completion
416
+ #
417
+ # For modern shells (e.g. bash, zsh, etc.), you can use shell
418
+ # completion for command line options.
419
+ #
420
+ # === Further documentation
421
+ #
422
+ # The above examples, along with the accompanying
423
+ # {Tutorial}[optparse/tutorial.rdoc],
424
+ # should be enough to learn how to use this class.
425
+ # If you have any questions, file a ticket at http://bugs.ruby-lang.org.
426
+ #
427
+ class Gem::OptionParser
428
+ Gem::OptionParser::Version = "0.4.0"
429
+
430
+ # :stopdoc:
431
+ NoArgument = [NO_ARGUMENT = :NONE, nil].freeze
432
+ RequiredArgument = [REQUIRED_ARGUMENT = :REQUIRED, true].freeze
433
+ OptionalArgument = [OPTIONAL_ARGUMENT = :OPTIONAL, false].freeze
434
+ # :startdoc:
435
+
436
+ #
437
+ # Keyword completion module. This allows partial arguments to be specified
438
+ # and resolved against a list of acceptable values.
439
+ #
440
+ module Completion
441
+ def self.regexp(key, icase)
442
+ Regexp.new('\A' + Regexp.quote(key).gsub(/\w+\b/, '\&\w*'), icase)
443
+ end
444
+
445
+ def self.candidate(key, icase = false, pat = nil, &block)
446
+ pat ||= Completion.regexp(key, icase)
447
+ candidates = []
448
+ block.call do |k, *v|
449
+ (if Regexp === k
450
+ kn = ""
451
+ k === key
452
+ else
453
+ kn = defined?(k.id2name) ? k.id2name : k
454
+ pat === kn
455
+ end) or next
456
+ v << k if v.empty?
457
+ candidates << [k, v, kn]
458
+ end
459
+ candidates
460
+ end
461
+
462
+ def candidate(key, icase = false, pat = nil)
463
+ Completion.candidate(key, icase, pat, &method(:each))
464
+ end
465
+
466
+ public
467
+ def complete(key, icase = false, pat = nil)
468
+ candidates = candidate(key, icase, pat, &method(:each)).sort_by {|k, v, kn| kn.size}
469
+ if candidates.size == 1
470
+ canon, sw, * = candidates[0]
471
+ elsif candidates.size > 1
472
+ canon, sw, cn = candidates.shift
473
+ candidates.each do |k, v, kn|
474
+ next if sw == v
475
+ if String === cn and String === kn
476
+ if cn.rindex(kn, 0)
477
+ canon, sw, cn = k, v, kn
478
+ next
479
+ elsif kn.rindex(cn, 0)
480
+ next
481
+ end
482
+ end
483
+ throw :ambiguous, key
484
+ end
485
+ end
486
+ if canon
487
+ block_given? or return key, *sw
488
+ yield(key, *sw)
489
+ end
490
+ end
491
+
492
+ def convert(opt = nil, val = nil, *)
493
+ val
494
+ end
495
+ end
496
+
497
+
498
+ #
499
+ # Map from option/keyword string to object with completion.
500
+ #
501
+ class OptionMap < Hash
502
+ include Completion
503
+ end
504
+
505
+
506
+ #
507
+ # Individual switch class. Not important to the user.
508
+ #
509
+ # Defined within Switch are several Switch-derived classes: NoArgument,
510
+ # RequiredArgument, etc.
511
+ #
512
+ class Switch
513
+ attr_reader :pattern, :conv, :short, :long, :arg, :desc, :block
514
+
515
+ #
516
+ # Guesses argument style from +arg+. Returns corresponding
517
+ # Gem::OptionParser::Switch class (OptionalArgument, etc.).
518
+ #
519
+ def self.guess(arg)
520
+ case arg
521
+ when ""
522
+ t = self
523
+ when /\A=?\[/
524
+ t = Switch::OptionalArgument
525
+ when /\A\s+\[/
526
+ t = Switch::PlacedArgument
527
+ else
528
+ t = Switch::RequiredArgument
529
+ end
530
+ self >= t or incompatible_argument_styles(arg, t)
531
+ t
532
+ end
533
+
534
+ def self.incompatible_argument_styles(arg, t)
535
+ raise(ArgumentError, "#{arg}: incompatible argument styles\n #{self}, #{t}",
536
+ ParseError.filter_backtrace(caller(2)))
537
+ end
538
+
539
+ def self.pattern
540
+ NilClass
541
+ end
542
+
543
+ def initialize(pattern = nil, conv = nil,
544
+ short = nil, long = nil, arg = nil,
545
+ desc = ([] if short or long), block = nil, &_block)
546
+ raise if Array === pattern
547
+ block ||= _block
548
+ @pattern, @conv, @short, @long, @arg, @desc, @block =
549
+ pattern, conv, short, long, arg, desc, block
550
+ end
551
+
552
+ #
553
+ # Parses +arg+ and returns rest of +arg+ and matched portion to the
554
+ # argument pattern. Yields when the pattern doesn't match substring.
555
+ #
556
+ def parse_arg(arg) # :nodoc:
557
+ pattern or return nil, [arg]
558
+ unless m = pattern.match(arg)
559
+ yield(InvalidArgument, arg)
560
+ return arg, []
561
+ end
562
+ if String === m
563
+ m = [s = m]
564
+ else
565
+ m = m.to_a
566
+ s = m[0]
567
+ return nil, m unless String === s
568
+ end
569
+ raise InvalidArgument, arg unless arg.rindex(s, 0)
570
+ return nil, m if s.length == arg.length
571
+ yield(InvalidArgument, arg) # didn't match whole arg
572
+ return arg[s.length..-1], m
573
+ end
574
+ private :parse_arg
575
+
576
+ #
577
+ # Parses argument, converts and returns +arg+, +block+ and result of
578
+ # conversion. Yields at semi-error condition instead of raising an
579
+ # exception.
580
+ #
581
+ def conv_arg(arg, val = []) # :nodoc:
582
+ if conv
583
+ val = conv.call(*val)
584
+ else
585
+ val = proc {|v| v}.call(*val)
586
+ end
587
+ return arg, block, val
588
+ end
589
+ private :conv_arg
590
+
591
+ #
592
+ # Produces the summary text. Each line of the summary is yielded to the
593
+ # block (without newline).
594
+ #
595
+ # +sdone+:: Already summarized short style options keyed hash.
596
+ # +ldone+:: Already summarized long style options keyed hash.
597
+ # +width+:: Width of left side (option part). In other words, the right
598
+ # side (description part) starts after +width+ columns.
599
+ # +max+:: Maximum width of left side -> the options are filled within
600
+ # +max+ columns.
601
+ # +indent+:: Prefix string indents all summarized lines.
602
+ #
603
+ def summarize(sdone = {}, ldone = {}, width = 1, max = width - 1, indent = "")
604
+ sopts, lopts = [], [], nil
605
+ @short.each {|s| sdone.fetch(s) {sopts << s}; sdone[s] = true} if @short
606
+ @long.each {|s| ldone.fetch(s) {lopts << s}; ldone[s] = true} if @long
607
+ return if sopts.empty? and lopts.empty? # completely hidden
608
+
609
+ left = [sopts.join(', ')]
610
+ right = desc.dup
611
+
612
+ while s = lopts.shift
613
+ l = left[-1].length + s.length
614
+ l += arg.length if left.size == 1 && arg
615
+ l < max or sopts.empty? or left << +''
616
+ left[-1] << (left[-1].empty? ? ' ' * 4 : ', ') << s
617
+ end
618
+
619
+ if arg
620
+ left[0] << (left[1] ? arg.sub(/\A(\[?)=/, '\1') + ',' : arg)
621
+ end
622
+ mlen = left.collect {|ss| ss.length}.max.to_i
623
+ while mlen > width and l = left.shift
624
+ mlen = left.collect {|ss| ss.length}.max.to_i if l.length == mlen
625
+ if l.length < width and (r = right[0]) and !r.empty?
626
+ l = l.to_s.ljust(width) + ' ' + r
627
+ right.shift
628
+ end
629
+ yield(indent + l)
630
+ end
631
+
632
+ while begin l = left.shift; r = right.shift; l or r end
633
+ l = l.to_s.ljust(width) + ' ' + r if r and !r.empty?
634
+ yield(indent + l)
635
+ end
636
+
637
+ self
638
+ end
639
+
640
+ def add_banner(to) # :nodoc:
641
+ unless @short or @long
642
+ s = desc.join
643
+ to << " [" + s + "]..." unless s.empty?
644
+ end
645
+ to
646
+ end
647
+
648
+ def match_nonswitch?(str) # :nodoc:
649
+ @pattern =~ str unless @short or @long
650
+ end
651
+
652
+ #
653
+ # Main name of the switch.
654
+ #
655
+ def switch_name
656
+ (long.first || short.first).sub(/\A-+(?:\[no-\])?/, '')
657
+ end
658
+
659
+ def compsys(sdone, ldone) # :nodoc:
660
+ sopts, lopts = [], []
661
+ @short.each {|s| sdone.fetch(s) {sopts << s}; sdone[s] = true} if @short
662
+ @long.each {|s| ldone.fetch(s) {lopts << s}; ldone[s] = true} if @long
663
+ return if sopts.empty? and lopts.empty? # completely hidden
664
+
665
+ (sopts+lopts).each do |opt|
666
+ # "(-x -c -r)-l[left justify]"
667
+ if /^--\[no-\](.+)$/ =~ opt
668
+ o = $1
669
+ yield("--#{o}", desc.join(""))
670
+ yield("--no-#{o}", desc.join(""))
671
+ else
672
+ yield("#{opt}", desc.join(""))
673
+ end
674
+ end
675
+ end
676
+
677
+ def pretty_print_contents(q) # :nodoc:
678
+ if @block
679
+ q.text ":" + @block.source_location.join(":") + ":"
680
+ first = false
681
+ else
682
+ first = true
683
+ end
684
+ [@short, @long].each do |list|
685
+ list.each do |opt|
686
+ if first
687
+ q.text ":"
688
+ first = false
689
+ end
690
+ q.breakable
691
+ q.text opt
692
+ end
693
+ end
694
+ end
695
+
696
+ def pretty_print(q) # :nodoc:
697
+ q.object_group(self) {pretty_print_contents(q)}
698
+ end
699
+
700
+ #
701
+ # Switch that takes no arguments.
702
+ #
703
+ class NoArgument < self
704
+
705
+ #
706
+ # Raises an exception if any arguments given.
707
+ #
708
+ def parse(arg, argv)
709
+ yield(NeedlessArgument, arg) if arg
710
+ conv_arg(arg)
711
+ end
712
+
713
+ def self.incompatible_argument_styles(*)
714
+ end
715
+
716
+ def self.pattern
717
+ Object
718
+ end
719
+
720
+ def pretty_head # :nodoc:
721
+ "NoArgument"
722
+ end
723
+ end
724
+
725
+ #
726
+ # Switch that takes an argument.
727
+ #
728
+ class RequiredArgument < self
729
+
730
+ #
731
+ # Raises an exception if argument is not present.
732
+ #
733
+ def parse(arg, argv)
734
+ unless arg
735
+ raise MissingArgument if argv.empty?
736
+ arg = argv.shift
737
+ end
738
+ conv_arg(*parse_arg(arg, &method(:raise)))
739
+ end
740
+
741
+ def pretty_head # :nodoc:
742
+ "Required"
743
+ end
744
+ end
745
+
746
+ #
747
+ # Switch that can omit argument.
748
+ #
749
+ class OptionalArgument < self
750
+
751
+ #
752
+ # Parses argument if given, or uses default value.
753
+ #
754
+ def parse(arg, argv, &error)
755
+ if arg
756
+ conv_arg(*parse_arg(arg, &error))
757
+ else
758
+ conv_arg(arg)
759
+ end
760
+ end
761
+
762
+ def pretty_head # :nodoc:
763
+ "Optional"
764
+ end
765
+ end
766
+
767
+ #
768
+ # Switch that takes an argument, which does not begin with '-' or is '-'.
769
+ #
770
+ class PlacedArgument < self
771
+
772
+ #
773
+ # Returns nil if argument is not present or begins with '-' and is not '-'.
774
+ #
775
+ def parse(arg, argv, &error)
776
+ if !(val = arg) and (argv.empty? or /\A-./ =~ (val = argv[0]))
777
+ return nil, block, nil
778
+ end
779
+ opt = (val = parse_arg(val, &error))[1]
780
+ val = conv_arg(*val)
781
+ if opt and !arg
782
+ argv.shift
783
+ else
784
+ val[0] = nil
785
+ end
786
+ val
787
+ end
788
+
789
+ def pretty_head # :nodoc:
790
+ "Placed"
791
+ end
792
+ end
793
+ end
794
+
795
+ #
796
+ # Simple option list providing mapping from short and/or long option
797
+ # string to Gem::OptionParser::Switch and mapping from acceptable argument to
798
+ # matching pattern and converter pair. Also provides summary feature.
799
+ #
800
+ class List
801
+ # Map from acceptable argument types to pattern and converter pairs.
802
+ attr_reader :atype
803
+
804
+ # Map from short style option switches to actual switch objects.
805
+ attr_reader :short
806
+
807
+ # Map from long style option switches to actual switch objects.
808
+ attr_reader :long
809
+
810
+ # List of all switches and summary string.
811
+ attr_reader :list
812
+
813
+ #
814
+ # Just initializes all instance variables.
815
+ #
816
+ def initialize
817
+ @atype = {}
818
+ @short = OptionMap.new
819
+ @long = OptionMap.new
820
+ @list = []
821
+ end
822
+
823
+ def pretty_print(q) # :nodoc:
824
+ q.group(1, "(", ")") do
825
+ @list.each do |sw|
826
+ next unless Switch === sw
827
+ q.group(1, "(" + sw.pretty_head, ")") do
828
+ sw.pretty_print_contents(q)
829
+ end
830
+ end
831
+ end
832
+ end
833
+
834
+ #
835
+ # See Gem::OptionParser.accept.
836
+ #
837
+ def accept(t, pat = /.*/m, &block)
838
+ if pat
839
+ pat.respond_to?(:match) or
840
+ raise TypeError, "has no `match'", ParseError.filter_backtrace(caller(2))
841
+ else
842
+ pat = t if t.respond_to?(:match)
843
+ end
844
+ unless block
845
+ block = pat.method(:convert).to_proc if pat.respond_to?(:convert)
846
+ end
847
+ @atype[t] = [pat, block]
848
+ end
849
+
850
+ #
851
+ # See Gem::OptionParser.reject.
852
+ #
853
+ def reject(t)
854
+ @atype.delete(t)
855
+ end
856
+
857
+ #
858
+ # Adds +sw+ according to +sopts+, +lopts+ and +nlopts+.
859
+ #
860
+ # +sw+:: Gem::OptionParser::Switch instance to be added.
861
+ # +sopts+:: Short style option list.
862
+ # +lopts+:: Long style option list.
863
+ # +nlopts+:: Negated long style options list.
864
+ #
865
+ def update(sw, sopts, lopts, nsw = nil, nlopts = nil) # :nodoc:
866
+ sopts.each {|o| @short[o] = sw} if sopts
867
+ lopts.each {|o| @long[o] = sw} if lopts
868
+ nlopts.each {|o| @long[o] = nsw} if nsw and nlopts
869
+ used = @short.invert.update(@long.invert)
870
+ @list.delete_if {|o| Switch === o and !used[o]}
871
+ end
872
+ private :update
873
+
874
+ #
875
+ # Inserts +switch+ at the head of the list, and associates short, long
876
+ # and negated long options. Arguments are:
877
+ #
878
+ # +switch+:: Gem::OptionParser::Switch instance to be inserted.
879
+ # +short_opts+:: List of short style options.
880
+ # +long_opts+:: List of long style options.
881
+ # +nolong_opts+:: List of long style options with "no-" prefix.
882
+ #
883
+ # prepend(switch, short_opts, long_opts, nolong_opts)
884
+ #
885
+ def prepend(*args)
886
+ update(*args)
887
+ @list.unshift(args[0])
888
+ end
889
+
890
+ #
891
+ # Appends +switch+ at the tail of the list, and associates short, long
892
+ # and negated long options. Arguments are:
893
+ #
894
+ # +switch+:: Gem::OptionParser::Switch instance to be inserted.
895
+ # +short_opts+:: List of short style options.
896
+ # +long_opts+:: List of long style options.
897
+ # +nolong_opts+:: List of long style options with "no-" prefix.
898
+ #
899
+ # append(switch, short_opts, long_opts, nolong_opts)
900
+ #
901
+ def append(*args)
902
+ update(*args)
903
+ @list.push(args[0])
904
+ end
905
+
906
+ #
907
+ # Searches +key+ in +id+ list. The result is returned or yielded if a
908
+ # block is given. If it isn't found, nil is returned.
909
+ #
910
+ def search(id, key)
911
+ if list = __send__(id)
912
+ val = list.fetch(key) {return nil}
913
+ block_given? ? yield(val) : val
914
+ end
915
+ end
916
+
917
+ #
918
+ # Searches list +id+ for +opt+ and the optional patterns for completion
919
+ # +pat+. If +icase+ is true, the search is case insensitive. The result
920
+ # is returned or yielded if a block is given. If it isn't found, nil is
921
+ # returned.
922
+ #
923
+ def complete(id, opt, icase = false, *pat, &block)
924
+ __send__(id).complete(opt, icase, *pat, &block)
925
+ end
926
+
927
+ def get_candidates(id)
928
+ yield __send__(id).keys
929
+ end
930
+
931
+ #
932
+ # Iterates over each option, passing the option to the +block+.
933
+ #
934
+ def each_option(&block)
935
+ list.each(&block)
936
+ end
937
+
938
+ #
939
+ # Creates the summary table, passing each line to the +block+ (without
940
+ # newline). The arguments +args+ are passed along to the summarize
941
+ # method which is called on every option.
942
+ #
943
+ def summarize(*args, &block)
944
+ sum = []
945
+ list.reverse_each do |opt|
946
+ if opt.respond_to?(:summarize) # perhaps Gem::OptionParser::Switch
947
+ s = []
948
+ opt.summarize(*args) {|l| s << l}
949
+ sum.concat(s.reverse)
950
+ elsif !opt or opt.empty?
951
+ sum << ""
952
+ elsif opt.respond_to?(:each_line)
953
+ sum.concat([*opt.each_line].reverse)
954
+ else
955
+ sum.concat([*opt.each].reverse)
956
+ end
957
+ end
958
+ sum.reverse_each(&block)
959
+ end
960
+
961
+ def add_banner(to) # :nodoc:
962
+ list.each do |opt|
963
+ if opt.respond_to?(:add_banner)
964
+ opt.add_banner(to)
965
+ end
966
+ end
967
+ to
968
+ end
969
+
970
+ def compsys(*args, &block) # :nodoc:
971
+ list.each do |opt|
972
+ if opt.respond_to?(:compsys)
973
+ opt.compsys(*args, &block)
974
+ end
975
+ end
976
+ end
977
+ end
978
+
979
+ #
980
+ # Hash with completion search feature. See Gem::OptionParser::Completion.
981
+ #
982
+ class CompletingHash < Hash
983
+ include Completion
984
+
985
+ #
986
+ # Completion for hash key.
987
+ #
988
+ def match(key)
989
+ *values = fetch(key) {
990
+ raise AmbiguousArgument, catch(:ambiguous) {return complete(key)}
991
+ }
992
+ return key, *values
993
+ end
994
+ end
995
+
996
+ # :stopdoc:
997
+
998
+ #
999
+ # Enumeration of acceptable argument styles. Possible values are:
1000
+ #
1001
+ # NO_ARGUMENT:: The switch takes no arguments. (:NONE)
1002
+ # REQUIRED_ARGUMENT:: The switch requires an argument. (:REQUIRED)
1003
+ # OPTIONAL_ARGUMENT:: The switch requires an optional argument. (:OPTIONAL)
1004
+ #
1005
+ # Use like --switch=argument (long style) or -Xargument (short style). For
1006
+ # short style, only portion matched to argument pattern is treated as
1007
+ # argument.
1008
+ #
1009
+ ArgumentStyle = {}
1010
+ NoArgument.each {|el| ArgumentStyle[el] = Switch::NoArgument}
1011
+ RequiredArgument.each {|el| ArgumentStyle[el] = Switch::RequiredArgument}
1012
+ OptionalArgument.each {|el| ArgumentStyle[el] = Switch::OptionalArgument}
1013
+ ArgumentStyle.freeze
1014
+
1015
+ #
1016
+ # Switches common used such as '--', and also provides default
1017
+ # argument classes
1018
+ #
1019
+ DefaultList = List.new
1020
+ DefaultList.short['-'] = Switch::NoArgument.new {}
1021
+ DefaultList.long[''] = Switch::NoArgument.new {throw :terminate}
1022
+
1023
+
1024
+ COMPSYS_HEADER = <<'XXX' # :nodoc:
1025
+
1026
+ typeset -A opt_args
1027
+ local context state line
1028
+
1029
+ _arguments -s -S \
1030
+ XXX
1031
+
1032
+ def compsys(to, name = File.basename($0)) # :nodoc:
1033
+ to << "#compdef #{name}\n"
1034
+ to << COMPSYS_HEADER
1035
+ visit(:compsys, {}, {}) {|o, d|
1036
+ to << %Q[ "#{o}[#{d.gsub(/[\"\[\]]/, '\\\\\&')}]" \\\n]
1037
+ }
1038
+ to << " '*:file:_files' && return 0\n"
1039
+ end
1040
+
1041
+ #
1042
+ # Default options for ARGV, which never appear in option summary.
1043
+ #
1044
+ Officious = {}
1045
+
1046
+ #
1047
+ # --help
1048
+ # Shows option summary.
1049
+ #
1050
+ Officious['help'] = proc do |parser|
1051
+ Switch::NoArgument.new do |arg|
1052
+ puts parser.help
1053
+ exit
1054
+ end
1055
+ end
1056
+
1057
+ #
1058
+ # --*-completion-bash=WORD
1059
+ # Shows candidates for command line completion.
1060
+ #
1061
+ Officious['*-completion-bash'] = proc do |parser|
1062
+ Switch::RequiredArgument.new do |arg|
1063
+ puts parser.candidate(arg)
1064
+ exit
1065
+ end
1066
+ end
1067
+
1068
+ #
1069
+ # --*-completion-zsh[=NAME:FILE]
1070
+ # Creates zsh completion file.
1071
+ #
1072
+ Officious['*-completion-zsh'] = proc do |parser|
1073
+ Switch::OptionalArgument.new do |arg|
1074
+ parser.compsys(STDOUT, arg)
1075
+ exit
1076
+ end
1077
+ end
1078
+
1079
+ #
1080
+ # --version
1081
+ # Shows version string if Version is defined.
1082
+ #
1083
+ Officious['version'] = proc do |parser|
1084
+ Switch::OptionalArgument.new do |pkg|
1085
+ if pkg
1086
+ begin
1087
+ require 'rubygems/optparse/lib/optparse/version'
1088
+ rescue LoadError
1089
+ else
1090
+ show_version(*pkg.split(/,/)) or
1091
+ abort("#{parser.program_name}: no version found in package #{pkg}")
1092
+ exit
1093
+ end
1094
+ end
1095
+ v = parser.ver or abort("#{parser.program_name}: version unknown")
1096
+ puts v
1097
+ exit
1098
+ end
1099
+ end
1100
+
1101
+ # :startdoc:
1102
+
1103
+ #
1104
+ # Class methods
1105
+ #
1106
+
1107
+ #
1108
+ # Initializes a new instance and evaluates the optional block in context
1109
+ # of the instance. Arguments +args+ are passed to #new, see there for
1110
+ # description of parameters.
1111
+ #
1112
+ # This method is *deprecated*, its behavior corresponds to the older #new
1113
+ # method.
1114
+ #
1115
+ def self.with(*args, &block)
1116
+ opts = new(*args)
1117
+ opts.instance_eval(&block)
1118
+ opts
1119
+ end
1120
+
1121
+ #
1122
+ # Returns an incremented value of +default+ according to +arg+.
1123
+ #
1124
+ def self.inc(arg, default = nil)
1125
+ case arg
1126
+ when Integer
1127
+ arg.nonzero?
1128
+ when nil
1129
+ default.to_i + 1
1130
+ end
1131
+ end
1132
+ def inc(*args)
1133
+ self.class.inc(*args)
1134
+ end
1135
+
1136
+ #
1137
+ # Initializes the instance and yields itself if called with a block.
1138
+ #
1139
+ # +banner+:: Banner message.
1140
+ # +width+:: Summary width.
1141
+ # +indent+:: Summary indent.
1142
+ #
1143
+ def initialize(banner = nil, width = 32, indent = ' ' * 4)
1144
+ @stack = [DefaultList, List.new, List.new]
1145
+ @program_name = nil
1146
+ @banner = banner
1147
+ @summary_width = width
1148
+ @summary_indent = indent
1149
+ @default_argv = ARGV
1150
+ @require_exact = false
1151
+ @raise_unknown = true
1152
+ add_officious
1153
+ yield self if block_given?
1154
+ end
1155
+
1156
+ def add_officious # :nodoc:
1157
+ list = base()
1158
+ Officious.each do |opt, block|
1159
+ list.long[opt] ||= block.call(self)
1160
+ end
1161
+ end
1162
+
1163
+ #
1164
+ # Terminates option parsing. Optional parameter +arg+ is a string pushed
1165
+ # back to be the first non-option argument.
1166
+ #
1167
+ def terminate(arg = nil)
1168
+ self.class.terminate(arg)
1169
+ end
1170
+ def self.terminate(arg = nil)
1171
+ throw :terminate, arg
1172
+ end
1173
+
1174
+ @stack = [DefaultList]
1175
+ def self.top() DefaultList end
1176
+
1177
+ #
1178
+ # Directs to accept specified class +t+. The argument string is passed to
1179
+ # the block in which it should be converted to the desired class.
1180
+ #
1181
+ # +t+:: Argument class specifier, any object including Class.
1182
+ # +pat+:: Pattern for argument, defaults to +t+ if it responds to match.
1183
+ #
1184
+ # accept(t, pat, &block)
1185
+ #
1186
+ def accept(*args, &blk) top.accept(*args, &blk) end
1187
+ #
1188
+ # See #accept.
1189
+ #
1190
+ def self.accept(*args, &blk) top.accept(*args, &blk) end
1191
+
1192
+ #
1193
+ # Directs to reject specified class argument.
1194
+ #
1195
+ # +t+:: Argument class specifier, any object including Class.
1196
+ #
1197
+ # reject(t)
1198
+ #
1199
+ def reject(*args, &blk) top.reject(*args, &blk) end
1200
+ #
1201
+ # See #reject.
1202
+ #
1203
+ def self.reject(*args, &blk) top.reject(*args, &blk) end
1204
+
1205
+ #
1206
+ # Instance methods
1207
+ #
1208
+
1209
+ # Heading banner preceding summary.
1210
+ attr_writer :banner
1211
+
1212
+ # Program name to be emitted in error message and default banner,
1213
+ # defaults to $0.
1214
+ attr_writer :program_name
1215
+
1216
+ # Width for option list portion of summary. Must be Numeric.
1217
+ attr_accessor :summary_width
1218
+
1219
+ # Indentation for summary. Must be String (or have + String method).
1220
+ attr_accessor :summary_indent
1221
+
1222
+ # Strings to be parsed in default.
1223
+ attr_accessor :default_argv
1224
+
1225
+ # Whether to require that options match exactly (disallows providing
1226
+ # abbreviated long option as short option).
1227
+ attr_accessor :require_exact
1228
+
1229
+ # Whether to raise at unknown option.
1230
+ attr_accessor :raise_unknown
1231
+
1232
+ #
1233
+ # Heading banner preceding summary.
1234
+ #
1235
+ def banner
1236
+ unless @banner
1237
+ @banner = +"Usage: #{program_name} [options]"
1238
+ visit(:add_banner, @banner)
1239
+ end
1240
+ @banner
1241
+ end
1242
+
1243
+ #
1244
+ # Program name to be emitted in error message and default banner, defaults
1245
+ # to $0.
1246
+ #
1247
+ def program_name
1248
+ @program_name || File.basename($0, '.*')
1249
+ end
1250
+
1251
+ # for experimental cascading :-)
1252
+ alias set_banner banner=
1253
+ alias set_program_name program_name=
1254
+ alias set_summary_width summary_width=
1255
+ alias set_summary_indent summary_indent=
1256
+
1257
+ # Version
1258
+ attr_writer :version
1259
+ # Release code
1260
+ attr_writer :release
1261
+
1262
+ #
1263
+ # Version
1264
+ #
1265
+ def version
1266
+ (defined?(@version) && @version) || (defined?(::Version) && ::Version)
1267
+ end
1268
+
1269
+ #
1270
+ # Release code
1271
+ #
1272
+ def release
1273
+ (defined?(@release) && @release) || (defined?(::Release) && ::Release) || (defined?(::RELEASE) && ::RELEASE)
1274
+ end
1275
+
1276
+ #
1277
+ # Returns version string from program_name, version and release.
1278
+ #
1279
+ def ver
1280
+ if v = version
1281
+ str = +"#{program_name} #{[v].join('.')}"
1282
+ str << " (#{v})" if v = release
1283
+ str
1284
+ end
1285
+ end
1286
+
1287
+ def warn(mesg = $!)
1288
+ super("#{program_name}: #{mesg}")
1289
+ end
1290
+
1291
+ def abort(mesg = $!)
1292
+ super("#{program_name}: #{mesg}")
1293
+ end
1294
+
1295
+ #
1296
+ # Subject of #on / #on_head, #accept / #reject
1297
+ #
1298
+ def top
1299
+ @stack[-1]
1300
+ end
1301
+
1302
+ #
1303
+ # Subject of #on_tail.
1304
+ #
1305
+ def base
1306
+ @stack[1]
1307
+ end
1308
+
1309
+ #
1310
+ # Pushes a new List.
1311
+ #
1312
+ def new
1313
+ @stack.push(List.new)
1314
+ if block_given?
1315
+ yield self
1316
+ else
1317
+ self
1318
+ end
1319
+ end
1320
+
1321
+ #
1322
+ # Removes the last List.
1323
+ #
1324
+ def remove
1325
+ @stack.pop
1326
+ end
1327
+
1328
+ #
1329
+ # Puts option summary into +to+ and returns +to+. Yields each line if
1330
+ # a block is given.
1331
+ #
1332
+ # +to+:: Output destination, which must have method <<. Defaults to [].
1333
+ # +width+:: Width of left side, defaults to @summary_width.
1334
+ # +max+:: Maximum length allowed for left side, defaults to +width+ - 1.
1335
+ # +indent+:: Indentation, defaults to @summary_indent.
1336
+ #
1337
+ def summarize(to = [], width = @summary_width, max = width - 1, indent = @summary_indent, &blk)
1338
+ nl = "\n"
1339
+ blk ||= proc {|l| to << (l.index(nl, -1) ? l : l + nl)}
1340
+ visit(:summarize, {}, {}, width, max, indent, &blk)
1341
+ to
1342
+ end
1343
+
1344
+ #
1345
+ # Returns option summary string.
1346
+ #
1347
+ def help; summarize("#{banner}".sub(/\n?\z/, "\n")) end
1348
+ alias to_s help
1349
+
1350
+ def pretty_print(q) # :nodoc:
1351
+ q.object_group(self) do
1352
+ first = true
1353
+ if @stack.size > 2
1354
+ @stack.each_with_index do |s, i|
1355
+ next if i < 2
1356
+ next if s.list.empty?
1357
+ if first
1358
+ first = false
1359
+ q.text ":"
1360
+ end
1361
+ q.breakable
1362
+ s.pretty_print(q)
1363
+ end
1364
+ end
1365
+ end
1366
+ end
1367
+
1368
+ def inspect # :nodoc:
1369
+ require 'pp'
1370
+ pretty_print_inspect
1371
+ end
1372
+
1373
+ #
1374
+ # Returns option summary list.
1375
+ #
1376
+ def to_a; summarize("#{banner}".split(/^/)) end
1377
+
1378
+ #
1379
+ # Checks if an argument is given twice, in which case an ArgumentError is
1380
+ # raised. Called from Gem::OptionParser#switch only.
1381
+ #
1382
+ # +obj+:: New argument.
1383
+ # +prv+:: Previously specified argument.
1384
+ # +msg+:: Exception message.
1385
+ #
1386
+ def notwice(obj, prv, msg) # :nodoc:
1387
+ unless !prv or prv == obj
1388
+ raise(ArgumentError, "argument #{msg} given twice: #{obj}",
1389
+ ParseError.filter_backtrace(caller(2)))
1390
+ end
1391
+ obj
1392
+ end
1393
+ private :notwice
1394
+
1395
+ SPLAT_PROC = proc {|*a| a.length <= 1 ? a.first : a} # :nodoc:
1396
+
1397
+ # :call-seq:
1398
+ # make_switch(params, block = nil)
1399
+ #
1400
+ # :include: ../doc/optparse/creates_option.rdoc
1401
+ #
1402
+ def make_switch(opts, block = nil)
1403
+ short, long, nolong, style, pattern, conv, not_pattern, not_conv, not_style = [], [], []
1404
+ ldesc, sdesc, desc, arg = [], [], []
1405
+ default_style = Switch::NoArgument
1406
+ default_pattern = nil
1407
+ klass = nil
1408
+ q, a = nil
1409
+ has_arg = false
1410
+
1411
+ opts.each do |o|
1412
+ # argument class
1413
+ next if search(:atype, o) do |pat, c|
1414
+ klass = notwice(o, klass, 'type')
1415
+ if not_style and not_style != Switch::NoArgument
1416
+ not_pattern, not_conv = pat, c
1417
+ else
1418
+ default_pattern, conv = pat, c
1419
+ end
1420
+ end
1421
+
1422
+ # directly specified pattern(any object possible to match)
1423
+ if (!(String === o || Symbol === o)) and o.respond_to?(:match)
1424
+ pattern = notwice(o, pattern, 'pattern')
1425
+ if pattern.respond_to?(:convert)
1426
+ conv = pattern.method(:convert).to_proc
1427
+ else
1428
+ conv = SPLAT_PROC
1429
+ end
1430
+ next
1431
+ end
1432
+
1433
+ # anything others
1434
+ case o
1435
+ when Proc, Method
1436
+ block = notwice(o, block, 'block')
1437
+ when Array, Hash
1438
+ case pattern
1439
+ when CompletingHash
1440
+ when nil
1441
+ pattern = CompletingHash.new
1442
+ conv = pattern.method(:convert).to_proc if pattern.respond_to?(:convert)
1443
+ else
1444
+ raise ArgumentError, "argument pattern given twice"
1445
+ end
1446
+ o.each {|pat, *v| pattern[pat] = v.fetch(0) {pat}}
1447
+ when Module
1448
+ raise ArgumentError, "unsupported argument type: #{o}", ParseError.filter_backtrace(caller(4))
1449
+ when *ArgumentStyle.keys
1450
+ style = notwice(ArgumentStyle[o], style, 'style')
1451
+ when /^--no-([^\[\]=\s]*)(.+)?/
1452
+ q, a = $1, $2
1453
+ o = notwice(a ? Object : TrueClass, klass, 'type')
1454
+ not_pattern, not_conv = search(:atype, o) unless not_style
1455
+ not_style = (not_style || default_style).guess(arg = a) if a
1456
+ default_style = Switch::NoArgument
1457
+ default_pattern, conv = search(:atype, FalseClass) unless default_pattern
1458
+ ldesc << "--no-#{q}"
1459
+ (q = q.downcase).tr!('_', '-')
1460
+ long << "no-#{q}"
1461
+ nolong << q
1462
+ when /^--\[no-\]([^\[\]=\s]*)(.+)?/
1463
+ q, a = $1, $2
1464
+ o = notwice(a ? Object : TrueClass, klass, 'type')
1465
+ if a
1466
+ default_style = default_style.guess(arg = a)
1467
+ default_pattern, conv = search(:atype, o) unless default_pattern
1468
+ end
1469
+ ldesc << "--[no-]#{q}"
1470
+ (o = q.downcase).tr!('_', '-')
1471
+ long << o
1472
+ not_pattern, not_conv = search(:atype, FalseClass) unless not_style
1473
+ not_style = Switch::NoArgument
1474
+ nolong << "no-#{o}"
1475
+ when /^--([^\[\]=\s]*)(.+)?/
1476
+ q, a = $1, $2
1477
+ if a
1478
+ o = notwice(NilClass, klass, 'type')
1479
+ default_style = default_style.guess(arg = a)
1480
+ default_pattern, conv = search(:atype, o) unless default_pattern
1481
+ end
1482
+ ldesc << "--#{q}"
1483
+ (o = q.downcase).tr!('_', '-')
1484
+ long << o
1485
+ when /^-(\[\^?\]?(?:[^\\\]]|\\.)*\])(.+)?/
1486
+ q, a = $1, $2
1487
+ o = notwice(Object, klass, 'type')
1488
+ if a
1489
+ default_style = default_style.guess(arg = a)
1490
+ default_pattern, conv = search(:atype, o) unless default_pattern
1491
+ else
1492
+ has_arg = true
1493
+ end
1494
+ sdesc << "-#{q}"
1495
+ short << Regexp.new(q)
1496
+ when /^-(.)(.+)?/
1497
+ q, a = $1, $2
1498
+ if a
1499
+ o = notwice(NilClass, klass, 'type')
1500
+ default_style = default_style.guess(arg = a)
1501
+ default_pattern, conv = search(:atype, o) unless default_pattern
1502
+ end
1503
+ sdesc << "-#{q}"
1504
+ short << q
1505
+ when /^=/
1506
+ style = notwice(default_style.guess(arg = o), style, 'style')
1507
+ default_pattern, conv = search(:atype, Object) unless default_pattern
1508
+ else
1509
+ desc.push(o) if o && !o.empty?
1510
+ end
1511
+ end
1512
+
1513
+ default_pattern, conv = search(:atype, default_style.pattern) unless default_pattern
1514
+ if !(short.empty? and long.empty?)
1515
+ if has_arg and default_style == Switch::NoArgument
1516
+ default_style = Switch::RequiredArgument
1517
+ end
1518
+ s = (style || default_style).new(pattern || default_pattern,
1519
+ conv, sdesc, ldesc, arg, desc, block)
1520
+ elsif !block
1521
+ if style or pattern
1522
+ raise ArgumentError, "no switch given", ParseError.filter_backtrace(caller)
1523
+ end
1524
+ s = desc
1525
+ else
1526
+ short << pattern
1527
+ s = (style || default_style).new(pattern,
1528
+ conv, nil, nil, arg, desc, block)
1529
+ end
1530
+ return s, short, long,
1531
+ (not_style.new(not_pattern, not_conv, sdesc, ldesc, nil, desc, block) if not_style),
1532
+ nolong
1533
+ end
1534
+
1535
+ # :call-seq:
1536
+ # define(*params, &block)
1537
+ #
1538
+ # :include: ../doc/optparse/creates_option.rdoc
1539
+ #
1540
+ def define(*opts, &block)
1541
+ top.append(*(sw = make_switch(opts, block)))
1542
+ sw[0]
1543
+ end
1544
+
1545
+ # :call-seq:
1546
+ # on(*params, &block)
1547
+ #
1548
+ # :include: ../doc/optparse/creates_option.rdoc
1549
+ #
1550
+ def on(*opts, &block)
1551
+ define(*opts, &block)
1552
+ self
1553
+ end
1554
+ alias def_option define
1555
+
1556
+ # :call-seq:
1557
+ # define_head(*params, &block)
1558
+ #
1559
+ # :include: ../doc/optparse/creates_option.rdoc
1560
+ #
1561
+ def define_head(*opts, &block)
1562
+ top.prepend(*(sw = make_switch(opts, block)))
1563
+ sw[0]
1564
+ end
1565
+
1566
+ # :call-seq:
1567
+ # on_head(*params, &block)
1568
+ #
1569
+ # :include: ../doc/optparse/creates_option.rdoc
1570
+ #
1571
+ # The new option is added at the head of the summary.
1572
+ #
1573
+ def on_head(*opts, &block)
1574
+ define_head(*opts, &block)
1575
+ self
1576
+ end
1577
+ alias def_head_option define_head
1578
+
1579
+ # :call-seq:
1580
+ # define_tail(*params, &block)
1581
+ #
1582
+ # :include: ../doc/optparse/creates_option.rdoc
1583
+ #
1584
+ def define_tail(*opts, &block)
1585
+ base.append(*(sw = make_switch(opts, block)))
1586
+ sw[0]
1587
+ end
1588
+
1589
+ #
1590
+ # :call-seq:
1591
+ # on_tail(*params, &block)
1592
+ #
1593
+ # :include: ../doc/optparse/creates_option.rdoc
1594
+ #
1595
+ # The new option is added at the tail of the summary.
1596
+ #
1597
+ def on_tail(*opts, &block)
1598
+ define_tail(*opts, &block)
1599
+ self
1600
+ end
1601
+ alias def_tail_option define_tail
1602
+
1603
+ #
1604
+ # Add separator in summary.
1605
+ #
1606
+ def separator(string)
1607
+ top.append(string, nil, nil)
1608
+ end
1609
+
1610
+ #
1611
+ # Parses command line arguments +argv+ in order. When a block is given,
1612
+ # each non-option argument is yielded. When optional +into+ keyword
1613
+ # argument is provided, the parsed option values are stored there via
1614
+ # <code>[]=</code> method (so it can be Hash, or OpenStruct, or other
1615
+ # similar object).
1616
+ #
1617
+ # Returns the rest of +argv+ left unparsed.
1618
+ #
1619
+ def order(*argv, into: nil, &nonopt)
1620
+ argv = argv[0].dup if argv.size == 1 and Array === argv[0]
1621
+ order!(argv, into: into, &nonopt)
1622
+ end
1623
+
1624
+ #
1625
+ # Same as #order, but removes switches destructively.
1626
+ # Non-option arguments remain in +argv+.
1627
+ #
1628
+ def order!(argv = default_argv, into: nil, &nonopt)
1629
+ setter = ->(name, val) {into[name.to_sym] = val} if into
1630
+ parse_in_order(argv, setter, &nonopt)
1631
+ end
1632
+
1633
+ def parse_in_order(argv = default_argv, setter = nil, &nonopt) # :nodoc:
1634
+ opt, arg, val, rest = nil
1635
+ nonopt ||= proc {|a| throw :terminate, a}
1636
+ argv.unshift(arg) if arg = catch(:terminate) {
1637
+ while arg = argv.shift
1638
+ case arg
1639
+ # long option
1640
+ when /\A--([^=]*)(?:=(.*))?/m
1641
+ opt, rest = $1, $2
1642
+ opt.tr!('_', '-')
1643
+ begin
1644
+ sw, = complete(:long, opt, true)
1645
+ if require_exact && !sw.long.include?(arg)
1646
+ throw :terminate, arg unless raise_unknown
1647
+ raise InvalidOption, arg
1648
+ end
1649
+ rescue ParseError
1650
+ throw :terminate, arg unless raise_unknown
1651
+ raise $!.set_option(arg, true)
1652
+ end
1653
+ begin
1654
+ opt, cb, val = sw.parse(rest, argv) {|*exc| raise(*exc)}
1655
+ val = cb.call(val) if cb
1656
+ setter.call(sw.switch_name, val) if setter
1657
+ rescue ParseError
1658
+ raise $!.set_option(arg, rest)
1659
+ end
1660
+
1661
+ # short option
1662
+ when /\A-(.)((=).*|.+)?/m
1663
+ eq, rest, opt = $3, $2, $1
1664
+ has_arg, val = eq, rest
1665
+ begin
1666
+ sw, = search(:short, opt)
1667
+ unless sw
1668
+ begin
1669
+ sw, = complete(:short, opt)
1670
+ # short option matched.
1671
+ val = arg.delete_prefix('-')
1672
+ has_arg = true
1673
+ rescue InvalidOption
1674
+ raise if require_exact
1675
+ # if no short options match, try completion with long
1676
+ # options.
1677
+ sw, = complete(:long, opt)
1678
+ eq ||= !rest
1679
+ end
1680
+ end
1681
+ rescue ParseError
1682
+ throw :terminate, arg unless raise_unknown
1683
+ raise $!.set_option(arg, true)
1684
+ end
1685
+ begin
1686
+ opt, cb, val = sw.parse(val, argv) {|*exc| raise(*exc) if eq}
1687
+ rescue ParseError
1688
+ raise $!.set_option(arg, arg.length > 2)
1689
+ else
1690
+ raise InvalidOption, arg if has_arg and !eq and arg == "-#{opt}"
1691
+ end
1692
+ begin
1693
+ argv.unshift(opt) if opt and (!rest or (opt = opt.sub(/\A-*/, '-')) != '-')
1694
+ val = cb.call(val) if cb
1695
+ setter.call(sw.switch_name, val) if setter
1696
+ rescue ParseError
1697
+ raise $!.set_option(arg, arg.length > 2)
1698
+ end
1699
+
1700
+ # non-option argument
1701
+ else
1702
+ catch(:prune) do
1703
+ visit(:each_option) do |sw0|
1704
+ sw = sw0
1705
+ sw.block.call(arg) if Switch === sw and sw.match_nonswitch?(arg)
1706
+ end
1707
+ nonopt.call(arg)
1708
+ end
1709
+ end
1710
+ end
1711
+
1712
+ nil
1713
+ }
1714
+
1715
+ visit(:search, :short, nil) {|sw| sw.block.call(*argv) if !sw.pattern}
1716
+
1717
+ argv
1718
+ end
1719
+ private :parse_in_order
1720
+
1721
+ #
1722
+ # Parses command line arguments +argv+ in permutation mode and returns
1723
+ # list of non-option arguments. When optional +into+ keyword
1724
+ # argument is provided, the parsed option values are stored there via
1725
+ # <code>[]=</code> method (so it can be Hash, or OpenStruct, or other
1726
+ # similar object).
1727
+ #
1728
+ def permute(*argv, into: nil)
1729
+ argv = argv[0].dup if argv.size == 1 and Array === argv[0]
1730
+ permute!(argv, into: into)
1731
+ end
1732
+
1733
+ #
1734
+ # Same as #permute, but removes switches destructively.
1735
+ # Non-option arguments remain in +argv+.
1736
+ #
1737
+ def permute!(argv = default_argv, into: nil)
1738
+ nonopts = []
1739
+ order!(argv, into: into, &nonopts.method(:<<))
1740
+ argv[0, 0] = nonopts
1741
+ argv
1742
+ end
1743
+
1744
+ #
1745
+ # Parses command line arguments +argv+ in order when environment variable
1746
+ # POSIXLY_CORRECT is set, and in permutation mode otherwise.
1747
+ # When optional +into+ keyword argument is provided, the parsed option
1748
+ # values are stored there via <code>[]=</code> method (so it can be Hash,
1749
+ # or OpenStruct, or other similar object).
1750
+ #
1751
+ def parse(*argv, into: nil)
1752
+ argv = argv[0].dup if argv.size == 1 and Array === argv[0]
1753
+ parse!(argv, into: into)
1754
+ end
1755
+
1756
+ #
1757
+ # Same as #parse, but removes switches destructively.
1758
+ # Non-option arguments remain in +argv+.
1759
+ #
1760
+ def parse!(argv = default_argv, into: nil)
1761
+ if ENV.include?('POSIXLY_CORRECT')
1762
+ order!(argv, into: into)
1763
+ else
1764
+ permute!(argv, into: into)
1765
+ end
1766
+ end
1767
+
1768
+ #
1769
+ # Wrapper method for getopts.rb.
1770
+ #
1771
+ # params = ARGV.getopts("ab:", "foo", "bar:", "zot:Z;zot option")
1772
+ # # params["a"] = true # -a
1773
+ # # params["b"] = "1" # -b1
1774
+ # # params["foo"] = "1" # --foo
1775
+ # # params["bar"] = "x" # --bar x
1776
+ # # params["zot"] = "z" # --zot Z
1777
+ #
1778
+ # Option +symbolize_names+ (boolean) specifies whether returned Hash keys should be Symbols; defaults to +false+ (use Strings).
1779
+ #
1780
+ # params = ARGV.getopts("ab:", "foo", "bar:", "zot:Z;zot option", symbolize_names: true)
1781
+ # # params[:a] = true # -a
1782
+ # # params[:b] = "1" # -b1
1783
+ # # params[:foo] = "1" # --foo
1784
+ # # params[:bar] = "x" # --bar x
1785
+ # # params[:zot] = "z" # --zot Z
1786
+ #
1787
+ def getopts(*args, symbolize_names: false)
1788
+ argv = Array === args.first ? args.shift : default_argv
1789
+ single_options, *long_options = *args
1790
+
1791
+ result = {}
1792
+
1793
+ single_options.scan(/(.)(:)?/) do |opt, val|
1794
+ if val
1795
+ result[opt] = nil
1796
+ define("-#{opt} VAL")
1797
+ else
1798
+ result[opt] = false
1799
+ define("-#{opt}")
1800
+ end
1801
+ end if single_options
1802
+
1803
+ long_options.each do |arg|
1804
+ arg, desc = arg.split(';', 2)
1805
+ opt, val = arg.split(':', 2)
1806
+ if val
1807
+ result[opt] = val.empty? ? nil : val
1808
+ define("--#{opt}=#{result[opt] || "VAL"}", *[desc].compact)
1809
+ else
1810
+ result[opt] = false
1811
+ define("--#{opt}", *[desc].compact)
1812
+ end
1813
+ end
1814
+
1815
+ parse_in_order(argv, result.method(:[]=))
1816
+ symbolize_names ? result.transform_keys(&:to_sym) : result
1817
+ end
1818
+
1819
+ #
1820
+ # See #getopts.
1821
+ #
1822
+ def self.getopts(*args, symbolize_names: false)
1823
+ new.getopts(*args, symbolize_names: symbolize_names)
1824
+ end
1825
+
1826
+ #
1827
+ # Traverses @stack, sending each element method +id+ with +args+ and
1828
+ # +block+.
1829
+ #
1830
+ def visit(id, *args, &block) # :nodoc:
1831
+ @stack.reverse_each do |el|
1832
+ el.__send__(id, *args, &block)
1833
+ end
1834
+ nil
1835
+ end
1836
+ private :visit
1837
+
1838
+ #
1839
+ # Searches +key+ in @stack for +id+ hash and returns or yields the result.
1840
+ #
1841
+ def search(id, key) # :nodoc:
1842
+ block_given = block_given?
1843
+ visit(:search, id, key) do |k|
1844
+ return block_given ? yield(k) : k
1845
+ end
1846
+ end
1847
+ private :search
1848
+
1849
+ #
1850
+ # Completes shortened long style option switch and returns pair of
1851
+ # canonical switch and switch descriptor Gem::OptionParser::Switch.
1852
+ #
1853
+ # +typ+:: Searching table.
1854
+ # +opt+:: Searching key.
1855
+ # +icase+:: Search case insensitive if true.
1856
+ # +pat+:: Optional pattern for completion.
1857
+ #
1858
+ def complete(typ, opt, icase = false, *pat) # :nodoc:
1859
+ if pat.empty?
1860
+ search(typ, opt) {|sw| return [sw, opt]} # exact match or...
1861
+ end
1862
+ ambiguous = catch(:ambiguous) {
1863
+ visit(:complete, typ, opt, icase, *pat) {|o, *sw| return sw}
1864
+ }
1865
+ exc = ambiguous ? AmbiguousOption : InvalidOption
1866
+ raise exc.new(opt, additional: self.method(:additional_message).curry[typ])
1867
+ end
1868
+ private :complete
1869
+
1870
+ #
1871
+ # Returns additional info.
1872
+ #
1873
+ def additional_message(typ, opt)
1874
+ return unless typ and opt and defined?(DidYouMean::SpellChecker)
1875
+ all_candidates = []
1876
+ visit(:get_candidates, typ) do |candidates|
1877
+ all_candidates.concat(candidates)
1878
+ end
1879
+ all_candidates.select! {|cand| cand.is_a?(String) }
1880
+ checker = DidYouMean::SpellChecker.new(dictionary: all_candidates)
1881
+ DidYouMean.formatter.message_for(all_candidates & checker.correct(opt))
1882
+ end
1883
+
1884
+ def candidate(word)
1885
+ list = []
1886
+ case word
1887
+ when '-'
1888
+ long = short = true
1889
+ when /\A--/
1890
+ word, arg = word.split(/=/, 2)
1891
+ argpat = Completion.regexp(arg, false) if arg and !arg.empty?
1892
+ long = true
1893
+ when /\A-/
1894
+ short = true
1895
+ end
1896
+ pat = Completion.regexp(word, long)
1897
+ visit(:each_option) do |opt|
1898
+ next unless Switch === opt
1899
+ opts = (long ? opt.long : []) + (short ? opt.short : [])
1900
+ opts = Completion.candidate(word, true, pat, &opts.method(:each)).map(&:first) if pat
1901
+ if /\A=/ =~ opt.arg
1902
+ opts.map! {|sw| sw + "="}
1903
+ if arg and CompletingHash === opt.pattern
1904
+ if opts = opt.pattern.candidate(arg, false, argpat)
1905
+ opts.map!(&:last)
1906
+ end
1907
+ end
1908
+ end
1909
+ list.concat(opts)
1910
+ end
1911
+ list
1912
+ end
1913
+
1914
+ #
1915
+ # Loads options from file names as +filename+. Does nothing when the file
1916
+ # is not present. Returns whether successfully loaded.
1917
+ #
1918
+ # +filename+ defaults to basename of the program without suffix in a
1919
+ # directory ~/.options, then the basename with '.options' suffix
1920
+ # under XDG and Haiku standard places.
1921
+ #
1922
+ # The optional +into+ keyword argument works exactly like that accepted in
1923
+ # method #parse.
1924
+ #
1925
+ def load(filename = nil, into: nil)
1926
+ unless filename
1927
+ basename = File.basename($0, '.*')
1928
+ return true if load(File.expand_path(basename, '~/.options'), into: into) rescue nil
1929
+ basename << ".options"
1930
+ return [
1931
+ # XDG
1932
+ ENV['XDG_CONFIG_HOME'],
1933
+ '~/.config',
1934
+ *ENV['XDG_CONFIG_DIRS']&.split(File::PATH_SEPARATOR),
1935
+
1936
+ # Haiku
1937
+ '~/config/settings',
1938
+ ].any? {|dir|
1939
+ next if !dir or dir.empty?
1940
+ load(File.expand_path(basename, dir), into: into) rescue nil
1941
+ }
1942
+ end
1943
+ begin
1944
+ parse(*File.readlines(filename, chomp: true), into: into)
1945
+ true
1946
+ rescue Errno::ENOENT, Errno::ENOTDIR
1947
+ false
1948
+ end
1949
+ end
1950
+
1951
+ #
1952
+ # Parses environment variable +env+ or its uppercase with splitting like a
1953
+ # shell.
1954
+ #
1955
+ # +env+ defaults to the basename of the program.
1956
+ #
1957
+ def environment(env = File.basename($0, '.*'))
1958
+ env = ENV[env] || ENV[env.upcase] or return
1959
+ require 'shellwords'
1960
+ parse(*Shellwords.shellwords(env))
1961
+ end
1962
+
1963
+ #
1964
+ # Acceptable argument classes
1965
+ #
1966
+
1967
+ #
1968
+ # Any string and no conversion. This is fall-back.
1969
+ #
1970
+ accept(Object) {|s,|s or s.nil?}
1971
+
1972
+ accept(NilClass) {|s,|s}
1973
+
1974
+ #
1975
+ # Any non-empty string, and no conversion.
1976
+ #
1977
+ accept(String, /.+/m) {|s,*|s}
1978
+
1979
+ #
1980
+ # Ruby/C-like integer, octal for 0-7 sequence, binary for 0b, hexadecimal
1981
+ # for 0x, and decimal for others; with optional sign prefix. Converts to
1982
+ # Integer.
1983
+ #
1984
+ decimal = '\d+(?:_\d+)*'
1985
+ binary = 'b[01]+(?:_[01]+)*'
1986
+ hex = 'x[\da-f]+(?:_[\da-f]+)*'
1987
+ octal = "0(?:[0-7]+(?:_[0-7]+)*|#{binary}|#{hex})?"
1988
+ integer = "#{octal}|#{decimal}"
1989
+
1990
+ accept(Integer, %r"\A[-+]?(?:#{integer})\z"io) {|s,|
1991
+ begin
1992
+ Integer(s)
1993
+ rescue ArgumentError
1994
+ raise Gem::OptionParser::InvalidArgument, s
1995
+ end if s
1996
+ }
1997
+
1998
+ #
1999
+ # Float number format, and converts to Float.
2000
+ #
2001
+ float = "(?:#{decimal}(?=(.)?)(?:\\.(?:#{decimal})?)?|\\.#{decimal})(?:E[-+]?#{decimal})?"
2002
+ floatpat = %r"\A[-+]?#{float}\z"io
2003
+ accept(Float, floatpat) {|s,| s.to_f if s}
2004
+
2005
+ #
2006
+ # Generic numeric format, converts to Integer for integer format, Float
2007
+ # for float format, and Rational for rational format.
2008
+ #
2009
+ real = "[-+]?(?:#{octal}|#{float})"
2010
+ accept(Numeric, /\A(#{real})(?:\/(#{real}))?\z/io) {|s, d, f, n,|
2011
+ if n
2012
+ Rational(d, n)
2013
+ elsif f
2014
+ Float(s)
2015
+ else
2016
+ Integer(s)
2017
+ end
2018
+ }
2019
+
2020
+ #
2021
+ # Decimal integer format, to be converted to Integer.
2022
+ #
2023
+ DecimalInteger = /\A[-+]?#{decimal}\z/io
2024
+ accept(DecimalInteger, DecimalInteger) {|s,|
2025
+ begin
2026
+ Integer(s, 10)
2027
+ rescue ArgumentError
2028
+ raise Gem::OptionParser::InvalidArgument, s
2029
+ end if s
2030
+ }
2031
+
2032
+ #
2033
+ # Ruby/C like octal/hexadecimal/binary integer format, to be converted to
2034
+ # Integer.
2035
+ #
2036
+ OctalInteger = /\A[-+]?(?:[0-7]+(?:_[0-7]+)*|0(?:#{binary}|#{hex}))\z/io
2037
+ accept(OctalInteger, OctalInteger) {|s,|
2038
+ begin
2039
+ Integer(s, 8)
2040
+ rescue ArgumentError
2041
+ raise Gem::OptionParser::InvalidArgument, s
2042
+ end if s
2043
+ }
2044
+
2045
+ #
2046
+ # Decimal integer/float number format, to be converted to Integer for
2047
+ # integer format, Float for float format.
2048
+ #
2049
+ DecimalNumeric = floatpat # decimal integer is allowed as float also.
2050
+ accept(DecimalNumeric, floatpat) {|s, f|
2051
+ begin
2052
+ if f
2053
+ Float(s)
2054
+ else
2055
+ Integer(s)
2056
+ end
2057
+ rescue ArgumentError
2058
+ raise Gem::OptionParser::InvalidArgument, s
2059
+ end if s
2060
+ }
2061
+
2062
+ #
2063
+ # Boolean switch, which means whether it is present or not, whether it is
2064
+ # absent or not with prefix no-, or it takes an argument
2065
+ # yes/no/true/false/+/-.
2066
+ #
2067
+ yesno = CompletingHash.new
2068
+ %w[- no false].each {|el| yesno[el] = false}
2069
+ %w[+ yes true].each {|el| yesno[el] = true}
2070
+ yesno['nil'] = false # should be nil?
2071
+ accept(TrueClass, yesno) {|arg, val| val == nil or val}
2072
+ #
2073
+ # Similar to TrueClass, but defaults to false.
2074
+ #
2075
+ accept(FalseClass, yesno) {|arg, val| val != nil and val}
2076
+
2077
+ #
2078
+ # List of strings separated by ",".
2079
+ #
2080
+ accept(Array) do |s, |
2081
+ if s
2082
+ s = s.split(',').collect {|ss| ss unless ss.empty?}
2083
+ end
2084
+ s
2085
+ end
2086
+
2087
+ #
2088
+ # Regular expression with options.
2089
+ #
2090
+ accept(Regexp, %r"\A/((?:\\.|[^\\])*)/([[:alpha:]]+)?\z|.*") do |all, s, o|
2091
+ f = 0
2092
+ if o
2093
+ f |= Regexp::IGNORECASE if /i/ =~ o
2094
+ f |= Regexp::MULTILINE if /m/ =~ o
2095
+ f |= Regexp::EXTENDED if /x/ =~ o
2096
+ case o = o.delete("imx")
2097
+ when ""
2098
+ when "u"
2099
+ s = s.encode(Encoding::UTF_8)
2100
+ when "e"
2101
+ s = s.encode(Encoding::EUC_JP)
2102
+ when "s"
2103
+ s = s.encode(Encoding::SJIS)
2104
+ when "n"
2105
+ f |= Regexp::NOENCODING
2106
+ else
2107
+ raise Gem::OptionParser::InvalidArgument, "unknown regexp option - #{o}"
2108
+ end
2109
+ else
2110
+ s ||= all
2111
+ end
2112
+ Regexp.new(s, f)
2113
+ end
2114
+
2115
+ #
2116
+ # Exceptions
2117
+ #
2118
+
2119
+ #
2120
+ # Base class of exceptions from Gem::OptionParser.
2121
+ #
2122
+ class ParseError < RuntimeError
2123
+ # Reason which caused the error.
2124
+ Reason = 'parse error'
2125
+
2126
+ def initialize(*args, additional: nil)
2127
+ @additional = additional
2128
+ @arg0, = args
2129
+ @args = args
2130
+ @reason = nil
2131
+ end
2132
+
2133
+ attr_reader :args
2134
+ attr_writer :reason
2135
+ attr_accessor :additional
2136
+
2137
+ #
2138
+ # Pushes back erred argument(s) to +argv+.
2139
+ #
2140
+ def recover(argv)
2141
+ argv[0, 0] = @args
2142
+ argv
2143
+ end
2144
+
2145
+ def self.filter_backtrace(array)
2146
+ unless $DEBUG
2147
+ array.delete_if(&%r"\A#{Regexp.quote(__FILE__)}:"o.method(:=~))
2148
+ end
2149
+ array
2150
+ end
2151
+
2152
+ def set_backtrace(array)
2153
+ super(self.class.filter_backtrace(array))
2154
+ end
2155
+
2156
+ def set_option(opt, eq)
2157
+ if eq
2158
+ @args[0] = opt
2159
+ else
2160
+ @args.unshift(opt)
2161
+ end
2162
+ self
2163
+ end
2164
+
2165
+ #
2166
+ # Returns error reason. Override this for I18N.
2167
+ #
2168
+ def reason
2169
+ @reason || self.class::Reason
2170
+ end
2171
+
2172
+ def inspect
2173
+ "#<#{self.class}: #{args.join(' ')}>"
2174
+ end
2175
+
2176
+ #
2177
+ # Default stringizing method to emit standard error message.
2178
+ #
2179
+ def message
2180
+ "#{reason}: #{args.join(' ')}#{additional[@arg0] if additional}"
2181
+ end
2182
+
2183
+ alias to_s message
2184
+ end
2185
+
2186
+ #
2187
+ # Raises when ambiguously completable string is encountered.
2188
+ #
2189
+ class AmbiguousOption < ParseError
2190
+ const_set(:Reason, 'ambiguous option')
2191
+ end
2192
+
2193
+ #
2194
+ # Raises when there is an argument for a switch which takes no argument.
2195
+ #
2196
+ class NeedlessArgument < ParseError
2197
+ const_set(:Reason, 'needless argument')
2198
+ end
2199
+
2200
+ #
2201
+ # Raises when a switch with mandatory argument has no argument.
2202
+ #
2203
+ class MissingArgument < ParseError
2204
+ const_set(:Reason, 'missing argument')
2205
+ end
2206
+
2207
+ #
2208
+ # Raises when switch is undefined.
2209
+ #
2210
+ class InvalidOption < ParseError
2211
+ const_set(:Reason, 'invalid option')
2212
+ end
2213
+
2214
+ #
2215
+ # Raises when the given argument does not match required format.
2216
+ #
2217
+ class InvalidArgument < ParseError
2218
+ const_set(:Reason, 'invalid argument')
2219
+ end
2220
+
2221
+ #
2222
+ # Raises when the given argument word can't be completed uniquely.
2223
+ #
2224
+ class AmbiguousArgument < InvalidArgument
2225
+ const_set(:Reason, 'ambiguous argument')
2226
+ end
2227
+
2228
+ #
2229
+ # Miscellaneous
2230
+ #
2231
+
2232
+ #
2233
+ # Extends command line arguments array (ARGV) to parse itself.
2234
+ #
2235
+ module Arguable
2236
+
2237
+ #
2238
+ # Sets Gem::OptionParser object, when +opt+ is +false+ or +nil+, methods
2239
+ # Gem::OptionParser::Arguable#options and Gem::OptionParser::Arguable#options= are
2240
+ # undefined. Thus, there is no ways to access the Gem::OptionParser object
2241
+ # via the receiver object.
2242
+ #
2243
+ def options=(opt)
2244
+ unless @optparse = opt
2245
+ class << self
2246
+ undef_method(:options)
2247
+ undef_method(:options=)
2248
+ end
2249
+ end
2250
+ end
2251
+
2252
+ #
2253
+ # Actual Gem::OptionParser object, automatically created if nonexistent.
2254
+ #
2255
+ # If called with a block, yields the Gem::OptionParser object and returns the
2256
+ # result of the block. If an Gem::OptionParser::ParseError exception occurs
2257
+ # in the block, it is rescued, a error message printed to STDERR and
2258
+ # +nil+ returned.
2259
+ #
2260
+ def options
2261
+ @optparse ||= Gem::OptionParser.new
2262
+ @optparse.default_argv = self
2263
+ block_given? or return @optparse
2264
+ begin
2265
+ yield @optparse
2266
+ rescue ParseError
2267
+ @optparse.warn $!
2268
+ nil
2269
+ end
2270
+ end
2271
+
2272
+ #
2273
+ # Parses +self+ destructively in order and returns +self+ containing the
2274
+ # rest arguments left unparsed.
2275
+ #
2276
+ def order!(&blk) options.order!(self, &blk) end
2277
+
2278
+ #
2279
+ # Parses +self+ destructively in permutation mode and returns +self+
2280
+ # containing the rest arguments left unparsed.
2281
+ #
2282
+ def permute!() options.permute!(self) end
2283
+
2284
+ #
2285
+ # Parses +self+ destructively and returns +self+ containing the
2286
+ # rest arguments left unparsed.
2287
+ #
2288
+ def parse!() options.parse!(self) end
2289
+
2290
+ #
2291
+ # Substitution of getopts is possible as follows. Also see
2292
+ # Gem::OptionParser#getopts.
2293
+ #
2294
+ # def getopts(*args)
2295
+ # ($OPT = ARGV.getopts(*args)).each do |opt, val|
2296
+ # eval "$OPT_#{opt.gsub(/[^A-Za-z0-9_]/, '_')} = val"
2297
+ # end
2298
+ # rescue Gem::OptionParser::ParseError
2299
+ # end
2300
+ #
2301
+ def getopts(*args, symbolize_names: false)
2302
+ options.getopts(self, *args, symbolize_names: symbolize_names)
2303
+ end
2304
+
2305
+ #
2306
+ # Initializes instance variable.
2307
+ #
2308
+ def self.extend_object(obj)
2309
+ super
2310
+ obj.instance_eval {@optparse = nil}
2311
+ end
2312
+ def initialize(*args)
2313
+ super
2314
+ @optparse = nil
2315
+ end
2316
+ end
2317
+
2318
+ #
2319
+ # Acceptable argument classes. Now contains DecimalInteger, OctalInteger
2320
+ # and DecimalNumeric. See Acceptable argument classes (in source code).
2321
+ #
2322
+ module Acceptables
2323
+ const_set(:DecimalInteger, Gem::OptionParser::DecimalInteger)
2324
+ const_set(:OctalInteger, Gem::OptionParser::OctalInteger)
2325
+ const_set(:DecimalNumeric, Gem::OptionParser::DecimalNumeric)
2326
+ end
2327
+ end
2328
+
2329
+ # ARGV is arguable by Gem::OptionParser
2330
+ ARGV.extend(Gem::OptionParser::Arguable)