chess_engine 0.0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +7 -0
- data/Gemfile +5 -0
- data/Gemfile.lock +21 -0
- data/README.md +26 -0
- data/chess_engine.gemspec +14 -0
- data/lib/chess_engine/board.rb +95 -0
- data/lib/chess_engine/cli.rb +105 -0
- data/lib/chess_engine/game.rb +148 -0
- data/lib/chess_engine/input.rb +19 -0
- data/lib/chess_engine/move.rb +35 -0
- data/lib/chess_engine/piece.rb +97 -0
- data/lib/chess_engine/validator.rb +87 -0
- data/lib/chess_engine.rb +1 -0
- data/spec/board_spec.rb +70 -0
- data/spec/game_helper.rb +5 -0
- data/spec/game_spec.rb +79 -0
- data/spec/spec_helper.rb +100 -0
- data/spec/validator_spec.rb +26 -0
- data/vendor/bundle/bin/coderay +27 -0
- data/vendor/bundle/bin/pry +27 -0
- data/vendor/bundle/cache/coderay-1.1.2.gem +0 -0
- data/vendor/bundle/cache/colorize-0.8.1.gem +0 -0
- data/vendor/bundle/cache/method_source-0.9.2.gem +0 -0
- data/vendor/bundle/cache/pry-0.12.2.gem +0 -0
- data/vendor/bundle/cache/rb-readline-0.5.5.gem +0 -0
- data/vendor/bundle/gems/coderay-1.1.2/MIT-LICENSE +22 -0
- data/vendor/bundle/gems/coderay-1.1.2/README_INDEX.rdoc +123 -0
- data/vendor/bundle/gems/coderay-1.1.2/bin/coderay +215 -0
- data/vendor/bundle/gems/coderay-1.1.2/lib/coderay/duo.rb +81 -0
- data/vendor/bundle/gems/coderay-1.1.2/lib/coderay/encoders/_map.rb +17 -0
- data/vendor/bundle/gems/coderay-1.1.2/lib/coderay/encoders/comment_filter.rb +25 -0
- data/vendor/bundle/gems/coderay-1.1.2/lib/coderay/encoders/count.rb +39 -0
- data/vendor/bundle/gems/coderay-1.1.2/lib/coderay/encoders/debug.rb +49 -0
- data/vendor/bundle/gems/coderay-1.1.2/lib/coderay/encoders/debug_lint.rb +63 -0
- data/vendor/bundle/gems/coderay-1.1.2/lib/coderay/encoders/div.rb +23 -0
- data/vendor/bundle/gems/coderay-1.1.2/lib/coderay/encoders/encoder.rb +190 -0
- data/vendor/bundle/gems/coderay-1.1.2/lib/coderay/encoders/filter.rb +58 -0
- data/vendor/bundle/gems/coderay-1.1.2/lib/coderay/encoders/html/css.rb +65 -0
- data/vendor/bundle/gems/coderay-1.1.2/lib/coderay/encoders/html/numbering.rb +108 -0
- data/vendor/bundle/gems/coderay-1.1.2/lib/coderay/encoders/html/output.rb +166 -0
- data/vendor/bundle/gems/coderay-1.1.2/lib/coderay/encoders/html.rb +333 -0
- data/vendor/bundle/gems/coderay-1.1.2/lib/coderay/encoders/json.rb +83 -0
- data/vendor/bundle/gems/coderay-1.1.2/lib/coderay/encoders/lines_of_code.rb +45 -0
- data/vendor/bundle/gems/coderay-1.1.2/lib/coderay/encoders/lint.rb +59 -0
- data/vendor/bundle/gems/coderay-1.1.2/lib/coderay/encoders/null.rb +18 -0
- data/vendor/bundle/gems/coderay-1.1.2/lib/coderay/encoders/page.rb +24 -0
- data/vendor/bundle/gems/coderay-1.1.2/lib/coderay/encoders/span.rb +23 -0
- data/vendor/bundle/gems/coderay-1.1.2/lib/coderay/encoders/statistic.rb +95 -0
- data/vendor/bundle/gems/coderay-1.1.2/lib/coderay/encoders/terminal.rb +195 -0
- data/vendor/bundle/gems/coderay-1.1.2/lib/coderay/encoders/text.rb +46 -0
- data/vendor/bundle/gems/coderay-1.1.2/lib/coderay/encoders/token_kind_filter.rb +111 -0
- data/vendor/bundle/gems/coderay-1.1.2/lib/coderay/encoders/xml.rb +72 -0
- data/vendor/bundle/gems/coderay-1.1.2/lib/coderay/encoders/yaml.rb +50 -0
- data/vendor/bundle/gems/coderay-1.1.2/lib/coderay/encoders.rb +18 -0
- data/vendor/bundle/gems/coderay-1.1.2/lib/coderay/for_redcloth.rb +95 -0
- data/vendor/bundle/gems/coderay-1.1.2/lib/coderay/helpers/file_type.rb +151 -0
- data/vendor/bundle/gems/coderay-1.1.2/lib/coderay/helpers/plugin.rb +55 -0
- data/vendor/bundle/gems/coderay-1.1.2/lib/coderay/helpers/plugin_host.rb +221 -0
- data/vendor/bundle/gems/coderay-1.1.2/lib/coderay/helpers/word_list.rb +72 -0
- data/vendor/bundle/gems/coderay-1.1.2/lib/coderay/scanners/_map.rb +24 -0
- data/vendor/bundle/gems/coderay-1.1.2/lib/coderay/scanners/c.rb +189 -0
- data/vendor/bundle/gems/coderay-1.1.2/lib/coderay/scanners/clojure.rb +217 -0
- data/vendor/bundle/gems/coderay-1.1.2/lib/coderay/scanners/cpp.rb +217 -0
- data/vendor/bundle/gems/coderay-1.1.2/lib/coderay/scanners/css.rb +196 -0
- data/vendor/bundle/gems/coderay-1.1.2/lib/coderay/scanners/debug.rb +75 -0
- data/vendor/bundle/gems/coderay-1.1.2/lib/coderay/scanners/delphi.rb +144 -0
- data/vendor/bundle/gems/coderay-1.1.2/lib/coderay/scanners/diff.rb +221 -0
- data/vendor/bundle/gems/coderay-1.1.2/lib/coderay/scanners/erb.rb +81 -0
- data/vendor/bundle/gems/coderay-1.1.2/lib/coderay/scanners/go.rb +208 -0
- data/vendor/bundle/gems/coderay-1.1.2/lib/coderay/scanners/groovy.rb +268 -0
- data/vendor/bundle/gems/coderay-1.1.2/lib/coderay/scanners/haml.rb +168 -0
- data/vendor/bundle/gems/coderay-1.1.2/lib/coderay/scanners/html.rb +275 -0
- data/vendor/bundle/gems/coderay-1.1.2/lib/coderay/scanners/java/builtin_types.rb +421 -0
- data/vendor/bundle/gems/coderay-1.1.2/lib/coderay/scanners/java.rb +174 -0
- data/vendor/bundle/gems/coderay-1.1.2/lib/coderay/scanners/java_script.rb +237 -0
- data/vendor/bundle/gems/coderay-1.1.2/lib/coderay/scanners/json.rb +98 -0
- data/vendor/bundle/gems/coderay-1.1.2/lib/coderay/scanners/lua.rb +280 -0
- data/vendor/bundle/gems/coderay-1.1.2/lib/coderay/scanners/php.rb +527 -0
- data/vendor/bundle/gems/coderay-1.1.2/lib/coderay/scanners/python.rb +287 -0
- data/vendor/bundle/gems/coderay-1.1.2/lib/coderay/scanners/raydebug.rb +75 -0
- data/vendor/bundle/gems/coderay-1.1.2/lib/coderay/scanners/ruby/patterns.rb +178 -0
- data/vendor/bundle/gems/coderay-1.1.2/lib/coderay/scanners/ruby/string_state.rb +79 -0
- data/vendor/bundle/gems/coderay-1.1.2/lib/coderay/scanners/ruby.rb +477 -0
- data/vendor/bundle/gems/coderay-1.1.2/lib/coderay/scanners/sass.rb +232 -0
- data/vendor/bundle/gems/coderay-1.1.2/lib/coderay/scanners/scanner.rb +337 -0
- data/vendor/bundle/gems/coderay-1.1.2/lib/coderay/scanners/sql.rb +169 -0
- data/vendor/bundle/gems/coderay-1.1.2/lib/coderay/scanners/taskpaper.rb +36 -0
- data/vendor/bundle/gems/coderay-1.1.2/lib/coderay/scanners/text.rb +26 -0
- data/vendor/bundle/gems/coderay-1.1.2/lib/coderay/scanners/xml.rb +17 -0
- data/vendor/bundle/gems/coderay-1.1.2/lib/coderay/scanners/yaml.rb +140 -0
- data/vendor/bundle/gems/coderay-1.1.2/lib/coderay/scanners.rb +27 -0
- data/vendor/bundle/gems/coderay-1.1.2/lib/coderay/styles/_map.rb +7 -0
- data/vendor/bundle/gems/coderay-1.1.2/lib/coderay/styles/alpha.rb +153 -0
- data/vendor/bundle/gems/coderay-1.1.2/lib/coderay/styles/style.rb +18 -0
- data/vendor/bundle/gems/coderay-1.1.2/lib/coderay/styles.rb +15 -0
- data/vendor/bundle/gems/coderay-1.1.2/lib/coderay/token_kinds.rb +85 -0
- data/vendor/bundle/gems/coderay-1.1.2/lib/coderay/tokens.rb +161 -0
- data/vendor/bundle/gems/coderay-1.1.2/lib/coderay/tokens_proxy.rb +55 -0
- data/vendor/bundle/gems/coderay-1.1.2/lib/coderay/version.rb +3 -0
- data/vendor/bundle/gems/coderay-1.1.2/lib/coderay.rb +284 -0
- data/vendor/bundle/gems/colorize-0.8.1/CHANGELOG +65 -0
- data/vendor/bundle/gems/colorize-0.8.1/LICENSE +339 -0
- data/vendor/bundle/gems/colorize-0.8.1/README.md +105 -0
- data/vendor/bundle/gems/colorize-0.8.1/Rakefile +13 -0
- data/vendor/bundle/gems/colorize-0.8.1/colorize.gemspec +36 -0
- data/vendor/bundle/gems/colorize-0.8.1/lib/colorize/class_methods.rb +120 -0
- data/vendor/bundle/gems/colorize-0.8.1/lib/colorize/instance_methods.rb +132 -0
- data/vendor/bundle/gems/colorize-0.8.1/lib/colorize.rb +12 -0
- data/vendor/bundle/gems/colorize-0.8.1/lib/colorized_string.rb +19 -0
- data/vendor/bundle/gems/colorize-0.8.1/test/test_colorize.rb +164 -0
- data/vendor/bundle/gems/method_source-0.9.2/Gemfile +2 -0
- data/vendor/bundle/gems/method_source-0.9.2/LICENSE +25 -0
- data/vendor/bundle/gems/method_source-0.9.2/README.markdown +91 -0
- data/vendor/bundle/gems/method_source-0.9.2/Rakefile +81 -0
- data/vendor/bundle/gems/method_source-0.9.2/lib/method_source/code_helpers.rb +154 -0
- data/vendor/bundle/gems/method_source-0.9.2/lib/method_source/source_location.rb +138 -0
- data/vendor/bundle/gems/method_source-0.9.2/lib/method_source/version.rb +3 -0
- data/vendor/bundle/gems/method_source-0.9.2/lib/method_source.rb +141 -0
- data/vendor/bundle/gems/method_source-0.9.2/method_source.gemspec +35 -0
- data/vendor/bundle/gems/method_source-0.9.2/spec/method_source/code_helpers_spec.rb +43 -0
- data/vendor/bundle/gems/method_source-0.9.2/spec/method_source_spec.rb +143 -0
- data/vendor/bundle/gems/method_source-0.9.2/spec/spec_helper.rb +101 -0
- data/vendor/bundle/gems/pry-0.12.2/CHANGELOG.md +953 -0
- data/vendor/bundle/gems/pry-0.12.2/LICENSE +25 -0
- data/vendor/bundle/gems/pry-0.12.2/README.md +390 -0
- data/vendor/bundle/gems/pry-0.12.2/bin/pry +8 -0
- data/vendor/bundle/gems/pry-0.12.2/lib/pry/basic_object.rb +6 -0
- data/vendor/bundle/gems/pry-0.12.2/lib/pry/cli.rb +218 -0
- data/vendor/bundle/gems/pry-0.12.2/lib/pry/code/code_file.rb +110 -0
- data/vendor/bundle/gems/pry-0.12.2/lib/pry/code/code_range.rb +71 -0
- data/vendor/bundle/gems/pry-0.12.2/lib/pry/code/loc.rb +98 -0
- data/vendor/bundle/gems/pry-0.12.2/lib/pry/code.rb +353 -0
- data/vendor/bundle/gems/pry-0.12.2/lib/pry/code_object.rb +195 -0
- data/vendor/bundle/gems/pry-0.12.2/lib/pry/color_printer.rb +65 -0
- data/vendor/bundle/gems/pry-0.12.2/lib/pry/command.rb +723 -0
- data/vendor/bundle/gems/pry-0.12.2/lib/pry/command_set.rb +415 -0
- data/vendor/bundle/gems/pry-0.12.2/lib/pry/commands/amend_line.rb +98 -0
- data/vendor/bundle/gems/pry-0.12.2/lib/pry/commands/bang.rb +20 -0
- data/vendor/bundle/gems/pry-0.12.2/lib/pry/commands/bang_pry.rb +17 -0
- data/vendor/bundle/gems/pry-0.12.2/lib/pry/commands/cat/abstract_formatter.rb +27 -0
- data/vendor/bundle/gems/pry-0.12.2/lib/pry/commands/cat/exception_formatter.rb +79 -0
- data/vendor/bundle/gems/pry-0.12.2/lib/pry/commands/cat/file_formatter.rb +71 -0
- data/vendor/bundle/gems/pry-0.12.2/lib/pry/commands/cat/input_expression_formatter.rb +43 -0
- data/vendor/bundle/gems/pry-0.12.2/lib/pry/commands/cat.rb +63 -0
- data/vendor/bundle/gems/pry-0.12.2/lib/pry/commands/cd.rb +41 -0
- data/vendor/bundle/gems/pry-0.12.2/lib/pry/commands/change_inspector.rb +27 -0
- data/vendor/bundle/gems/pry-0.12.2/lib/pry/commands/change_prompt.rb +46 -0
- data/vendor/bundle/gems/pry-0.12.2/lib/pry/commands/clear_screen.rb +14 -0
- data/vendor/bundle/gems/pry-0.12.2/lib/pry/commands/code_collector.rb +167 -0
- data/vendor/bundle/gems/pry-0.12.2/lib/pry/commands/disable_pry.rb +27 -0
- data/vendor/bundle/gems/pry-0.12.2/lib/pry/commands/disabled_commands.rb +2 -0
- data/vendor/bundle/gems/pry-0.12.2/lib/pry/commands/easter_eggs.rb +112 -0
- data/vendor/bundle/gems/pry-0.12.2/lib/pry/commands/edit/exception_patcher.rb +25 -0
- data/vendor/bundle/gems/pry-0.12.2/lib/pry/commands/edit/file_and_line_locator.rb +36 -0
- data/vendor/bundle/gems/pry-0.12.2/lib/pry/commands/edit.rb +200 -0
- data/vendor/bundle/gems/pry-0.12.2/lib/pry/commands/exit.rb +43 -0
- data/vendor/bundle/gems/pry-0.12.2/lib/pry/commands/exit_all.rb +29 -0
- data/vendor/bundle/gems/pry-0.12.2/lib/pry/commands/exit_program.rb +23 -0
- data/vendor/bundle/gems/pry-0.12.2/lib/pry/commands/find_method.rb +191 -0
- data/vendor/bundle/gems/pry-0.12.2/lib/pry/commands/fix_indent.rb +19 -0
- data/vendor/bundle/gems/pry-0.12.2/lib/pry/commands/gem_cd.rb +26 -0
- data/vendor/bundle/gems/pry-0.12.2/lib/pry/commands/gem_install.rb +32 -0
- data/vendor/bundle/gems/pry-0.12.2/lib/pry/commands/gem_list.rb +33 -0
- data/vendor/bundle/gems/pry-0.12.2/lib/pry/commands/gem_open.rb +29 -0
- data/vendor/bundle/gems/pry-0.12.2/lib/pry/commands/gem_readme.rb +25 -0
- data/vendor/bundle/gems/pry-0.12.2/lib/pry/commands/gem_search.rb +40 -0
- data/vendor/bundle/gems/pry-0.12.2/lib/pry/commands/gem_stats.rb +83 -0
- data/vendor/bundle/gems/pry-0.12.2/lib/pry/commands/gist.rb +102 -0
- data/vendor/bundle/gems/pry-0.12.2/lib/pry/commands/help.rb +164 -0
- data/vendor/bundle/gems/pry-0.12.2/lib/pry/commands/hist.rb +181 -0
- data/vendor/bundle/gems/pry-0.12.2/lib/pry/commands/import_set.rb +23 -0
- data/vendor/bundle/gems/pry-0.12.2/lib/pry/commands/install_command.rb +54 -0
- data/vendor/bundle/gems/pry-0.12.2/lib/pry/commands/jump_to.rb +29 -0
- data/vendor/bundle/gems/pry-0.12.2/lib/pry/commands/list_inspectors.rb +35 -0
- data/vendor/bundle/gems/pry-0.12.2/lib/pry/commands/ls/constants.rb +58 -0
- data/vendor/bundle/gems/pry-0.12.2/lib/pry/commands/ls/formatter.rb +51 -0
- data/vendor/bundle/gems/pry-0.12.2/lib/pry/commands/ls/globals.rb +46 -0
- data/vendor/bundle/gems/pry-0.12.2/lib/pry/commands/ls/grep.rb +19 -0
- data/vendor/bundle/gems/pry-0.12.2/lib/pry/commands/ls/instance_vars.rb +38 -0
- data/vendor/bundle/gems/pry-0.12.2/lib/pry/commands/ls/interrogatable.rb +18 -0
- data/vendor/bundle/gems/pry-0.12.2/lib/pry/commands/ls/jruby_hacks.rb +49 -0
- data/vendor/bundle/gems/pry-0.12.2/lib/pry/commands/ls/local_names.rb +33 -0
- data/vendor/bundle/gems/pry-0.12.2/lib/pry/commands/ls/local_vars.rb +37 -0
- data/vendor/bundle/gems/pry-0.12.2/lib/pry/commands/ls/ls_entity.rb +69 -0
- data/vendor/bundle/gems/pry-0.12.2/lib/pry/commands/ls/methods.rb +54 -0
- data/vendor/bundle/gems/pry-0.12.2/lib/pry/commands/ls/methods_helper.rb +46 -0
- data/vendor/bundle/gems/pry-0.12.2/lib/pry/commands/ls/self_methods.rb +33 -0
- data/vendor/bundle/gems/pry-0.12.2/lib/pry/commands/ls.rb +113 -0
- data/vendor/bundle/gems/pry-0.12.2/lib/pry/commands/nesting.rb +25 -0
- data/vendor/bundle/gems/pry-0.12.2/lib/pry/commands/play.rb +102 -0
- data/vendor/bundle/gems/pry-0.12.2/lib/pry/commands/pry_backtrace.rb +25 -0
- data/vendor/bundle/gems/pry-0.12.2/lib/pry/commands/pry_version.rb +17 -0
- data/vendor/bundle/gems/pry-0.12.2/lib/pry/commands/raise_up.rb +33 -0
- data/vendor/bundle/gems/pry-0.12.2/lib/pry/commands/reload_code.rb +62 -0
- data/vendor/bundle/gems/pry-0.12.2/lib/pry/commands/reset.rb +18 -0
- data/vendor/bundle/gems/pry-0.12.2/lib/pry/commands/ri.rb +65 -0
- data/vendor/bundle/gems/pry-0.12.2/lib/pry/commands/save_file.rb +61 -0
- data/vendor/bundle/gems/pry-0.12.2/lib/pry/commands/shell_command.rb +75 -0
- data/vendor/bundle/gems/pry-0.12.2/lib/pry/commands/shell_mode.rb +25 -0
- data/vendor/bundle/gems/pry-0.12.2/lib/pry/commands/show_doc.rb +81 -0
- data/vendor/bundle/gems/pry-0.12.2/lib/pry/commands/show_info.rb +216 -0
- data/vendor/bundle/gems/pry-0.12.2/lib/pry/commands/show_input.rb +17 -0
- data/vendor/bundle/gems/pry-0.12.2/lib/pry/commands/show_source.rb +53 -0
- data/vendor/bundle/gems/pry-0.12.2/lib/pry/commands/stat.rb +40 -0
- data/vendor/bundle/gems/pry-0.12.2/lib/pry/commands/switch_to.rb +23 -0
- data/vendor/bundle/gems/pry-0.12.2/lib/pry/commands/toggle_color.rb +24 -0
- data/vendor/bundle/gems/pry-0.12.2/lib/pry/commands/watch_expression/expression.rb +38 -0
- data/vendor/bundle/gems/pry-0.12.2/lib/pry/commands/watch_expression.rb +107 -0
- data/vendor/bundle/gems/pry-0.12.2/lib/pry/commands/whereami.rb +197 -0
- data/vendor/bundle/gems/pry-0.12.2/lib/pry/commands/wtf.rb +70 -0
- data/vendor/bundle/gems/pry-0.12.2/lib/pry/commands.rb +6 -0
- data/vendor/bundle/gems/pry-0.12.2/lib/pry/config/behavior.rb +255 -0
- data/vendor/bundle/gems/pry-0.12.2/lib/pry/config/convenience.rb +28 -0
- data/vendor/bundle/gems/pry-0.12.2/lib/pry/config/default.rb +159 -0
- data/vendor/bundle/gems/pry-0.12.2/lib/pry/config/memoization.rb +48 -0
- data/vendor/bundle/gems/pry-0.12.2/lib/pry/config.rb +35 -0
- data/vendor/bundle/gems/pry-0.12.2/lib/pry/core_extensions.rb +142 -0
- data/vendor/bundle/gems/pry-0.12.2/lib/pry/editor.rb +128 -0
- data/vendor/bundle/gems/pry-0.12.2/lib/pry/exceptions.rb +76 -0
- data/vendor/bundle/gems/pry-0.12.2/lib/pry/forwardable.rb +23 -0
- data/vendor/bundle/gems/pry-0.12.2/lib/pry/helpers/base_helpers.rb +200 -0
- data/vendor/bundle/gems/pry-0.12.2/lib/pry/helpers/command_helpers.rb +157 -0
- data/vendor/bundle/gems/pry-0.12.2/lib/pry/helpers/documentation_helpers.rb +76 -0
- data/vendor/bundle/gems/pry-0.12.2/lib/pry/helpers/options_helpers.rb +27 -0
- data/vendor/bundle/gems/pry-0.12.2/lib/pry/helpers/platform.rb +58 -0
- data/vendor/bundle/gems/pry-0.12.2/lib/pry/helpers/table.rb +114 -0
- data/vendor/bundle/gems/pry-0.12.2/lib/pry/helpers/text.rb +115 -0
- data/vendor/bundle/gems/pry-0.12.2/lib/pry/helpers.rb +6 -0
- data/vendor/bundle/gems/pry-0.12.2/lib/pry/history.rb +161 -0
- data/vendor/bundle/gems/pry-0.12.2/lib/pry/hooks.rb +171 -0
- data/vendor/bundle/gems/pry-0.12.2/lib/pry/indent.rb +408 -0
- data/vendor/bundle/gems/pry-0.12.2/lib/pry/input_completer.rb +265 -0
- data/vendor/bundle/gems/pry-0.12.2/lib/pry/input_lock.rb +130 -0
- data/vendor/bundle/gems/pry-0.12.2/lib/pry/inspector.rb +27 -0
- data/vendor/bundle/gems/pry-0.12.2/lib/pry/last_exception.rb +61 -0
- data/vendor/bundle/gems/pry-0.12.2/lib/pry/method/disowned.rb +55 -0
- data/vendor/bundle/gems/pry-0.12.2/lib/pry/method/patcher.rb +122 -0
- data/vendor/bundle/gems/pry-0.12.2/lib/pry/method/weird_method_locator.rb +198 -0
- data/vendor/bundle/gems/pry-0.12.2/lib/pry/method.rb +552 -0
- data/vendor/bundle/gems/pry-0.12.2/lib/pry/object_path.rb +83 -0
- data/vendor/bundle/gems/pry-0.12.2/lib/pry/output.rb +50 -0
- data/vendor/bundle/gems/pry-0.12.2/lib/pry/pager.rb +250 -0
- data/vendor/bundle/gems/pry-0.12.2/lib/pry/platform.rb +91 -0
- data/vendor/bundle/gems/pry-0.12.2/lib/pry/plugins.rb +122 -0
- data/vendor/bundle/gems/pry-0.12.2/lib/pry/prompt.rb +145 -0
- data/vendor/bundle/gems/pry-0.12.2/lib/pry/pry_class.rb +425 -0
- data/vendor/bundle/gems/pry-0.12.2/lib/pry/pry_instance.rb +699 -0
- data/vendor/bundle/gems/pry-0.12.2/lib/pry/repl.rb +261 -0
- data/vendor/bundle/gems/pry-0.12.2/lib/pry/repl_file_loader.rb +73 -0
- data/vendor/bundle/gems/pry-0.12.2/lib/pry/ring.rb +84 -0
- data/vendor/bundle/gems/pry-0.12.2/lib/pry/rubygem.rb +84 -0
- data/vendor/bundle/gems/pry-0.12.2/lib/pry/slop/LICENSE +20 -0
- data/vendor/bundle/gems/pry-0.12.2/lib/pry/slop/commands.rb +195 -0
- data/vendor/bundle/gems/pry-0.12.2/lib/pry/slop/option.rb +206 -0
- data/vendor/bundle/gems/pry-0.12.2/lib/pry/slop.rb +661 -0
- data/vendor/bundle/gems/pry-0.12.2/lib/pry/terminal.rb +91 -0
- data/vendor/bundle/gems/pry-0.12.2/lib/pry/testable/evalable.rb +15 -0
- data/vendor/bundle/gems/pry-0.12.2/lib/pry/testable/mockable.rb +14 -0
- data/vendor/bundle/gems/pry-0.12.2/lib/pry/testable/pry_tester.rb +73 -0
- data/vendor/bundle/gems/pry-0.12.2/lib/pry/testable/utility.rb +26 -0
- data/vendor/bundle/gems/pry-0.12.2/lib/pry/testable/variables.rb +46 -0
- data/vendor/bundle/gems/pry-0.12.2/lib/pry/testable.rb +70 -0
- data/vendor/bundle/gems/pry-0.12.2/lib/pry/version.rb +3 -0
- data/vendor/bundle/gems/pry-0.12.2/lib/pry/wrapped_module/candidate.rb +131 -0
- data/vendor/bundle/gems/pry-0.12.2/lib/pry/wrapped_module.rb +378 -0
- data/vendor/bundle/gems/pry-0.12.2/lib/pry.rb +132 -0
- data/vendor/bundle/gems/rb-readline-0.5.5/CHANGES +94 -0
- data/vendor/bundle/gems/rb-readline-0.5.5/LICENSE +25 -0
- data/vendor/bundle/gems/rb-readline-0.5.5/README.md +81 -0
- data/vendor/bundle/gems/rb-readline-0.5.5/Rakefile +25 -0
- data/vendor/bundle/gems/rb-readline-0.5.5/bench/_rl_adjust_point.rb +26 -0
- data/vendor/bundle/gems/rb-readline-0.5.5/examples/example_readline.rb +8 -0
- data/vendor/bundle/gems/rb-readline-0.5.5/examples/example_readline_with_completion.rb +18 -0
- data/vendor/bundle/gems/rb-readline-0.5.5/examples/tinyirb.rb +11 -0
- data/vendor/bundle/gems/rb-readline-0.5.5/lib/rb-readline.rb +18 -0
- data/vendor/bundle/gems/rb-readline-0.5.5/lib/rbreadline/version.rb +3 -0
- data/vendor/bundle/gems/rb-readline-0.5.5/lib/rbreadline.rb +8920 -0
- data/vendor/bundle/gems/rb-readline-0.5.5/lib/readline.rb +539 -0
- data/vendor/bundle/gems/rb-readline-0.5.5/rb-readline.gemspec +51 -0
- data/vendor/bundle/gems/rb-readline-0.5.5/setup.rb +1585 -0
- data/vendor/bundle/gems/rb-readline-0.5.5/test/support/filesystem_completion_helper.rb +53 -0
- data/vendor/bundle/gems/rb-readline-0.5.5/test/test_completion.rb +100 -0
- data/vendor/bundle/gems/rb-readline-0.5.5/test/test_filename_completion_proc.rb +88 -0
- data/vendor/bundle/gems/rb-readline-0.5.5/test/test_history.rb +29 -0
- data/vendor/bundle/gems/rb-readline-0.5.5/test/test_rbreadline.rb +23 -0
- data/vendor/bundle/gems/rb-readline-0.5.5/test/test_readline.rb +221 -0
- data/vendor/bundle/specifications/coderay-1.1.2.gemspec +26 -0
- data/vendor/bundle/specifications/colorize-0.8.1.gemspec +38 -0
- data/vendor/bundle/specifications/method_source-0.9.2.gemspec +35 -0
- data/vendor/bundle/specifications/pry-0.12.2.gemspec +38 -0
- data/vendor/bundle/specifications/rb-readline-0.5.5.gemspec +39 -0
- metadata +363 -0
@@ -0,0 +1,20 @@
|
|
1
|
+
Copyright (c) 2012 Lee Jarvis
|
2
|
+
|
3
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
4
|
+
a copy of this software and associated documentation files (the
|
5
|
+
"Software"), to deal in the Software without restriction, including
|
6
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
7
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
8
|
+
permit persons to whom the Software is furnished to do so, subject to
|
9
|
+
the following conditions:
|
10
|
+
|
11
|
+
The above copyright notice and this permission notice shall be
|
12
|
+
included in all copies or substantial portions of the Software.
|
13
|
+
|
14
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
15
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
16
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
17
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
18
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
19
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
20
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
@@ -0,0 +1,195 @@
|
|
1
|
+
class Pry::Slop
|
2
|
+
class Commands
|
3
|
+
include Enumerable
|
4
|
+
|
5
|
+
attr_reader :config, :commands, :arguments
|
6
|
+
attr_writer :banner
|
7
|
+
|
8
|
+
# Create a new instance of Slop::Commands and optionally build
|
9
|
+
# Slop instances via a block. Any configuration options used in
|
10
|
+
# this method will be the default configuration options sent to
|
11
|
+
# each Slop object created.
|
12
|
+
#
|
13
|
+
# config - An optional configuration Hash.
|
14
|
+
# block - Optional block used to define commands.
|
15
|
+
#
|
16
|
+
# Examples:
|
17
|
+
#
|
18
|
+
# commands = Slop::Commands.new do
|
19
|
+
# on :new do
|
20
|
+
# on '-o', '--outdir=', 'The output directory'
|
21
|
+
# on '-v', '--verbose', 'Enable verbose mode'
|
22
|
+
# end
|
23
|
+
#
|
24
|
+
# on :generate do
|
25
|
+
# on '--assets', 'Generate assets', :default => true
|
26
|
+
# end
|
27
|
+
#
|
28
|
+
# global do
|
29
|
+
# on '-D', '--debug', 'Enable debug mode', :default => false
|
30
|
+
# end
|
31
|
+
# end
|
32
|
+
#
|
33
|
+
# commands[:new].class #=> Slop
|
34
|
+
# commands.parse
|
35
|
+
#
|
36
|
+
def initialize(config = {}, &block)
|
37
|
+
@config = config
|
38
|
+
@commands = {}
|
39
|
+
@banner = nil
|
40
|
+
@triggered_command = nil
|
41
|
+
|
42
|
+
warn "[DEPRECATED] Slop::Commands is deprecated and will be removed in "\
|
43
|
+
"Slop version 4. Check out http://injekt.github.com/slop/#commands for "\
|
44
|
+
"a new implementation of commands."
|
45
|
+
|
46
|
+
if block_given?
|
47
|
+
block.arity == 1 ? yield(self) : instance_eval(&block)
|
48
|
+
end
|
49
|
+
end
|
50
|
+
|
51
|
+
# Optionally set the banner for this command help output.
|
52
|
+
#
|
53
|
+
# banner - The String text to set the banner.
|
54
|
+
#
|
55
|
+
# Returns the String banner if one is set.
|
56
|
+
def banner(banner = nil)
|
57
|
+
@banner = banner if banner
|
58
|
+
@banner
|
59
|
+
end
|
60
|
+
|
61
|
+
# Add a Slop instance for a specific command.
|
62
|
+
#
|
63
|
+
# command - A String or Symbol key used to identify this command.
|
64
|
+
# config - A Hash of configuration options to pass to Slop.
|
65
|
+
# block - An optional block used to pass options to Slop.
|
66
|
+
#
|
67
|
+
# Returns the newly created Slop instance mapped to command.
|
68
|
+
def on(command, config = {}, &block)
|
69
|
+
commands[command.to_s] = Slop.new(@config.merge(config), &block)
|
70
|
+
end
|
71
|
+
|
72
|
+
# Add a Slop instance used when no other commands exist.
|
73
|
+
#
|
74
|
+
# config - A Hash of configuration options to pass to Slop.
|
75
|
+
# block - An optional block used to pass options to Slop.
|
76
|
+
#
|
77
|
+
# Returns the newly created Slop instance mapped to default.
|
78
|
+
def default(config = {}, &block)
|
79
|
+
on('default', config, &block)
|
80
|
+
end
|
81
|
+
|
82
|
+
# Add a global Slop instance.
|
83
|
+
#
|
84
|
+
# config - A Hash of configuration options to pass to Slop.
|
85
|
+
# block - An optional block used to pass options to Slop.
|
86
|
+
#
|
87
|
+
# Returns the newly created Slop instance mapped to global.
|
88
|
+
def global(config = {}, &block)
|
89
|
+
on('global', config, &block)
|
90
|
+
end
|
91
|
+
|
92
|
+
# Fetch the instance of Slop tied to a command.
|
93
|
+
#
|
94
|
+
# key - The String or Symbol key used to locate this command.
|
95
|
+
#
|
96
|
+
# Returns the Slop instance if this key is found, nil otherwise.
|
97
|
+
def [](key)
|
98
|
+
commands[key.to_s]
|
99
|
+
end
|
100
|
+
alias get []
|
101
|
+
|
102
|
+
# Check for a command presence.
|
103
|
+
#
|
104
|
+
# Examples:
|
105
|
+
#
|
106
|
+
# cmds.parse %w( foo )
|
107
|
+
# cmds.present?(:foo) #=> true
|
108
|
+
# cmds.present?(:bar) #=> false
|
109
|
+
#
|
110
|
+
# Returns true if the given key is present in the parsed arguments.
|
111
|
+
def present?(key)
|
112
|
+
key.to_s == @triggered_command
|
113
|
+
end
|
114
|
+
|
115
|
+
# Enumerable interface.
|
116
|
+
def each(&block)
|
117
|
+
@commands.each(&block)
|
118
|
+
end
|
119
|
+
|
120
|
+
# Parse a list of items.
|
121
|
+
#
|
122
|
+
# items - The Array of items to parse.
|
123
|
+
#
|
124
|
+
# Returns the original Array of items.
|
125
|
+
def parse(items = ARGV)
|
126
|
+
parse! items.dup
|
127
|
+
items
|
128
|
+
end
|
129
|
+
|
130
|
+
# Parse a list of items, removing any options or option arguments found.
|
131
|
+
#
|
132
|
+
# items - The Array of items to parse.
|
133
|
+
#
|
134
|
+
# Returns the original Array of items with options removed.
|
135
|
+
def parse!(items = ARGV)
|
136
|
+
if (opts = commands[items[0].to_s])
|
137
|
+
@triggered_command = items.shift
|
138
|
+
execute_arguments! items
|
139
|
+
opts.parse! items
|
140
|
+
execute_global_opts! items
|
141
|
+
else
|
142
|
+
if (opts = commands['default'])
|
143
|
+
opts.parse! items
|
144
|
+
else
|
145
|
+
if config[:strict] && items[0]
|
146
|
+
raise InvalidCommandError, "Unknown command `#{items[0]}`"
|
147
|
+
end
|
148
|
+
end
|
149
|
+
execute_global_opts! items
|
150
|
+
end
|
151
|
+
items
|
152
|
+
end
|
153
|
+
|
154
|
+
# Returns a nested Hash with Slop options and values. See Slop#to_hash.
|
155
|
+
def to_hash
|
156
|
+
Hash[commands.map { |k, v| [k.to_sym, v.to_hash] }]
|
157
|
+
end
|
158
|
+
|
159
|
+
# Returns the help String.
|
160
|
+
def to_s
|
161
|
+
defaults = commands.delete('default')
|
162
|
+
globals = commands.delete('global')
|
163
|
+
helps = commands.reject { |_, v| v.options.none? }
|
164
|
+
if globals && globals.options.any?
|
165
|
+
helps.merge!('Global options' => globals.to_s)
|
166
|
+
end
|
167
|
+
if defaults && defaults.options.any?
|
168
|
+
helps.merge!('Other options' => defaults.to_s)
|
169
|
+
end
|
170
|
+
banner = @banner ? "#{@banner}\n" : ""
|
171
|
+
banner + helps.map { |key, opts| " #{key}\n#{opts}" }.join("\n\n")
|
172
|
+
end
|
173
|
+
alias help to_s
|
174
|
+
|
175
|
+
# Returns the inspection String.
|
176
|
+
def inspect
|
177
|
+
"#<Slop::Commands #{config.inspect} #{commands.values.map(&:inspect)}>"
|
178
|
+
end
|
179
|
+
|
180
|
+
private
|
181
|
+
|
182
|
+
# Returns nothing.
|
183
|
+
def execute_arguments!(items)
|
184
|
+
@arguments = items.take_while { |arg| !arg.start_with?('-') }
|
185
|
+
items.shift @arguments.size
|
186
|
+
end
|
187
|
+
|
188
|
+
# Returns nothing.
|
189
|
+
def execute_global_opts!(items)
|
190
|
+
if (global_opts = commands['global'])
|
191
|
+
global_opts.parse! items
|
192
|
+
end
|
193
|
+
end
|
194
|
+
end
|
195
|
+
end
|
@@ -0,0 +1,206 @@
|
|
1
|
+
class Pry::Slop
|
2
|
+
class Option
|
3
|
+
# The default Hash of configuration options this class uses.
|
4
|
+
DEFAULT_OPTIONS = {
|
5
|
+
argument: false,
|
6
|
+
optional_argument: false,
|
7
|
+
tail: false,
|
8
|
+
default: nil,
|
9
|
+
callback: nil,
|
10
|
+
delimiter: ',',
|
11
|
+
limit: 0,
|
12
|
+
match: nil,
|
13
|
+
optional: true,
|
14
|
+
required: false,
|
15
|
+
as: String,
|
16
|
+
autocreated: false
|
17
|
+
}
|
18
|
+
|
19
|
+
attr_reader :short, :long, :description, :config, :types
|
20
|
+
attr_accessor :count, :argument_in_value
|
21
|
+
|
22
|
+
# Incapsulate internal option information, mainly used to store
|
23
|
+
# option specific configuration data, most of the meat of this
|
24
|
+
# class is found in the #value method.
|
25
|
+
#
|
26
|
+
# slop - The instance of Slop tied to this Option.
|
27
|
+
# short - The String or Symbol short flag.
|
28
|
+
# long - The String or Symbol long flag.
|
29
|
+
# description - The String description text.
|
30
|
+
# config - A Hash of configuration options.
|
31
|
+
# block - An optional block used as a callback.
|
32
|
+
def initialize(slop, short, long, description, config = {}, &block)
|
33
|
+
@slop = slop
|
34
|
+
@short = short
|
35
|
+
@long = long
|
36
|
+
@description = description
|
37
|
+
@config = DEFAULT_OPTIONS.merge(config)
|
38
|
+
@count = 0
|
39
|
+
@callback = block_given? ? block : config[:callback]
|
40
|
+
@value = nil
|
41
|
+
|
42
|
+
@types = {
|
43
|
+
string: proc { |v| v.to_s },
|
44
|
+
symbol: proc { |v| v.to_sym },
|
45
|
+
integer: proc { |v| value_to_integer(v) },
|
46
|
+
float: proc { |v| value_to_float(v) },
|
47
|
+
range: proc { |v| value_to_range(v) },
|
48
|
+
count: proc { |v| @count }
|
49
|
+
}
|
50
|
+
|
51
|
+
if long && long.size > @slop.config[:longest_flag]
|
52
|
+
@slop.config[:longest_flag] = long.size
|
53
|
+
end
|
54
|
+
|
55
|
+
@config.each_key do |key|
|
56
|
+
predicate = :"#{key}?"
|
57
|
+
unless self.class.method_defined? predicate
|
58
|
+
self.class.__send__(:define_method, predicate) { !!@config[key] }
|
59
|
+
end
|
60
|
+
end
|
61
|
+
end
|
62
|
+
|
63
|
+
# Returns true if this option expects an argument.
|
64
|
+
def expects_argument?
|
65
|
+
config[:argument] && config[:argument] != :optional
|
66
|
+
end
|
67
|
+
|
68
|
+
# Returns true if this option accepts an optional argument.
|
69
|
+
def accepts_optional_argument?
|
70
|
+
config[:optional_argument] || config[:argument] == :optional
|
71
|
+
end
|
72
|
+
|
73
|
+
# Returns the String flag of this option. Preferring the long flag.
|
74
|
+
def key
|
75
|
+
long || short
|
76
|
+
end
|
77
|
+
|
78
|
+
# Call this options callback if one exists, and it responds to call().
|
79
|
+
#
|
80
|
+
# Returns nothing.
|
81
|
+
def call(*objects)
|
82
|
+
@callback.call(*objects) if @callback.respond_to?(:call)
|
83
|
+
end
|
84
|
+
|
85
|
+
# Set the new argument value for this option.
|
86
|
+
#
|
87
|
+
# We use this setter method to handle concatenating lists. That is,
|
88
|
+
# when an array type is specified and used more than once, values from
|
89
|
+
# both options will be grouped together and flattened into a single array.
|
90
|
+
def value=(new_value)
|
91
|
+
if config[:as].to_s.downcase == 'array'
|
92
|
+
@value ||= []
|
93
|
+
|
94
|
+
if new_value.respond_to?(:split)
|
95
|
+
@value.concat new_value.split(config[:delimiter], config[:limit])
|
96
|
+
end
|
97
|
+
else
|
98
|
+
@value = new_value
|
99
|
+
end
|
100
|
+
end
|
101
|
+
|
102
|
+
# Fetch the argument value for this option.
|
103
|
+
#
|
104
|
+
# Returns the Object once any type conversions have taken place.
|
105
|
+
def value
|
106
|
+
value = @value.nil? ? config[:default] : @value
|
107
|
+
|
108
|
+
if [true, false, nil].include?(value) && config[:as].to_s != 'count'
|
109
|
+
return value
|
110
|
+
end
|
111
|
+
|
112
|
+
type = config[:as]
|
113
|
+
if type.respond_to?(:call)
|
114
|
+
type.call(value)
|
115
|
+
else
|
116
|
+
if (callable = types[type.to_s.downcase.to_sym])
|
117
|
+
callable.call(value)
|
118
|
+
else
|
119
|
+
value
|
120
|
+
end
|
121
|
+
end
|
122
|
+
end
|
123
|
+
|
124
|
+
# Returns the help String for this option.
|
125
|
+
def to_s
|
126
|
+
return config[:help] if config[:help].respond_to?(:to_str)
|
127
|
+
|
128
|
+
out = " #{short ? "-#{short}, " : ' ' * 4}"
|
129
|
+
|
130
|
+
if long
|
131
|
+
out << "--#{long}"
|
132
|
+
size = long.size
|
133
|
+
diff = @slop.config[:longest_flag] - size
|
134
|
+
out << (' ' * (diff + 6))
|
135
|
+
else
|
136
|
+
out << (' ' * (@slop.config[:longest_flag] + 8))
|
137
|
+
end
|
138
|
+
|
139
|
+
"#{out}#{description}"
|
140
|
+
end
|
141
|
+
alias help to_s
|
142
|
+
|
143
|
+
# Returns the String inspection text.
|
144
|
+
def inspect
|
145
|
+
"#<Slop::Option [-#{short} | --#{long}" +
|
146
|
+
"#{'=' if expects_argument?}#{'=?' if accepts_optional_argument?}]" +
|
147
|
+
" (#{description}) #{config.inspect}"
|
148
|
+
end
|
149
|
+
|
150
|
+
private
|
151
|
+
|
152
|
+
# Convert an object to an Integer if possible.
|
153
|
+
#
|
154
|
+
# value - The Object we want to convert to an integer.
|
155
|
+
#
|
156
|
+
# Returns the Integer value if possible to convert, else a zero.
|
157
|
+
def value_to_integer(value)
|
158
|
+
if @slop.strict?
|
159
|
+
begin
|
160
|
+
Integer(value.to_s, 10)
|
161
|
+
rescue ArgumentError
|
162
|
+
raise InvalidArgumentError, "#{value} could not be coerced into Integer"
|
163
|
+
end
|
164
|
+
else
|
165
|
+
value.to_s.to_i
|
166
|
+
end
|
167
|
+
end
|
168
|
+
|
169
|
+
# Convert an object to a Float if possible.
|
170
|
+
#
|
171
|
+
# value - The Object we want to convert to a float.
|
172
|
+
#
|
173
|
+
# Returns the Float value if possible to convert, else a zero.
|
174
|
+
def value_to_float(value)
|
175
|
+
if @slop.strict?
|
176
|
+
begin
|
177
|
+
Float(value.to_s)
|
178
|
+
rescue ArgumentError
|
179
|
+
raise InvalidArgumentError, "#{value} could not be coerced into Float"
|
180
|
+
end
|
181
|
+
else
|
182
|
+
value.to_s.to_f
|
183
|
+
end
|
184
|
+
end
|
185
|
+
|
186
|
+
# Convert an object to a Range if possible.
|
187
|
+
#
|
188
|
+
# value - The Object we want to convert to a range.
|
189
|
+
#
|
190
|
+
# Returns the Range value if one could be found, else the original object.
|
191
|
+
def value_to_range(value)
|
192
|
+
case value.to_s
|
193
|
+
when /\A(\-?\d+)\z/
|
194
|
+
Range.new($1.to_i, $1.to_i)
|
195
|
+
when /\A(-?\d+?)(\.\.\.?|-|,)(-?\d+)\z/
|
196
|
+
Range.new($1.to_i, $3.to_i, $2 == '...')
|
197
|
+
else
|
198
|
+
if @slop.strict?
|
199
|
+
raise InvalidArgumentError, "#{value} could not be coerced into Range"
|
200
|
+
else
|
201
|
+
value
|
202
|
+
end
|
203
|
+
end
|
204
|
+
end
|
205
|
+
end
|
206
|
+
end
|
@@ -0,0 +1,661 @@
|
|
1
|
+
class Pry::Slop
|
2
|
+
require_relative 'slop/option'
|
3
|
+
require_relative 'slop/commands'
|
4
|
+
include Enumerable
|
5
|
+
VERSION = '3.4.0'
|
6
|
+
|
7
|
+
# The main Error class, all Exception classes inherit from this class.
|
8
|
+
class Error < StandardError; end
|
9
|
+
|
10
|
+
# Raised when an option argument is expected but none are given.
|
11
|
+
class MissingArgumentError < Error; end
|
12
|
+
|
13
|
+
# Raised when an option is expected/required but not present.
|
14
|
+
class MissingOptionError < Error; end
|
15
|
+
|
16
|
+
# Raised when an argument does not match its intended match constraint.
|
17
|
+
class InvalidArgumentError < Error; end
|
18
|
+
|
19
|
+
# Raised when an invalid option is found and the strict flag is enabled.
|
20
|
+
class InvalidOptionError < Error; end
|
21
|
+
|
22
|
+
# Raised when an invalid command is found and the strict flag is enabled.
|
23
|
+
class InvalidCommandError < Error; end
|
24
|
+
|
25
|
+
# Returns a default Hash of configuration options this Slop instance uses.
|
26
|
+
DEFAULT_OPTIONS = {
|
27
|
+
strict: false,
|
28
|
+
help: false,
|
29
|
+
banner: nil,
|
30
|
+
ignore_case: false,
|
31
|
+
autocreate: false,
|
32
|
+
arguments: false,
|
33
|
+
optional_arguments: false,
|
34
|
+
multiple_switches: true,
|
35
|
+
longest_flag: 0
|
36
|
+
}
|
37
|
+
|
38
|
+
class << self
|
39
|
+
# items - The Array of items to extract options from (default: ARGV).
|
40
|
+
# config - The Hash of configuration options to send to Slop.new().
|
41
|
+
# block - An optional block used to add options.
|
42
|
+
#
|
43
|
+
# Examples:
|
44
|
+
#
|
45
|
+
# Slop.parse(ARGV, :help => true) do
|
46
|
+
# on '-n', '--name', 'Your username', :argument => true
|
47
|
+
# end
|
48
|
+
#
|
49
|
+
# Returns a new instance of Slop.
|
50
|
+
def parse(items = ARGV, config = {}, &block)
|
51
|
+
parse! items.dup, config, &block
|
52
|
+
end
|
53
|
+
|
54
|
+
# items - The Array of items to extract options from (default: ARGV).
|
55
|
+
# config - The Hash of configuration options to send to Slop.new().
|
56
|
+
# block - An optional block used to add options.
|
57
|
+
#
|
58
|
+
# Returns a new instance of Slop.
|
59
|
+
def parse!(items = ARGV, config = {}, &block)
|
60
|
+
config, items = items, ARGV if items.is_a?(Hash) && config.empty?
|
61
|
+
slop = Pry::Slop.new config, &block
|
62
|
+
slop.parse! items
|
63
|
+
slop
|
64
|
+
end
|
65
|
+
|
66
|
+
# Build a Slop object from a option specification.
|
67
|
+
#
|
68
|
+
# This allows you to design your options via a simple String rather
|
69
|
+
# than programatically. Do note though that with this method, you're
|
70
|
+
# unable to pass any advanced options to the on() method when creating
|
71
|
+
# options.
|
72
|
+
#
|
73
|
+
# string - The optspec String
|
74
|
+
# config - A Hash of configuration options to pass to Slop.new
|
75
|
+
#
|
76
|
+
# Examples:
|
77
|
+
#
|
78
|
+
# opts = Slop.optspec(<<-SPEC)
|
79
|
+
# ruby foo.rb [options]
|
80
|
+
# ---
|
81
|
+
# n,name= Your name
|
82
|
+
# a,age= Your age
|
83
|
+
# A,auth Sign in with auth
|
84
|
+
# p,passcode= Your secret pass code
|
85
|
+
# SPEC
|
86
|
+
#
|
87
|
+
# opts.fetch_option(:name).description #=> "Your name"
|
88
|
+
#
|
89
|
+
# Returns a new instance of Slop.
|
90
|
+
def optspec(string, config = {})
|
91
|
+
config[:banner], optspec = string.split(/^--+$/, 2) if string[/^--+$/]
|
92
|
+
lines = optspec.split("\n").reject(&:empty?)
|
93
|
+
opts = Slop.new(config)
|
94
|
+
|
95
|
+
lines.each do |line|
|
96
|
+
opt, description = line.split(' ', 2)
|
97
|
+
short, long = opt.split(',').map { |s| s.sub(/\A--?/, '') }
|
98
|
+
opt = opts.on(short, long, description)
|
99
|
+
|
100
|
+
if long && long.end_with?('=')
|
101
|
+
long.sub!(/\=$/, '')
|
102
|
+
opt.config[:argument] = true
|
103
|
+
end
|
104
|
+
end
|
105
|
+
|
106
|
+
opts
|
107
|
+
end
|
108
|
+
end
|
109
|
+
|
110
|
+
# The Hash of configuration options for this Slop instance.
|
111
|
+
attr_reader :config
|
112
|
+
|
113
|
+
# The Array of Slop::Option objects tied to this Slop instance.
|
114
|
+
attr_reader :options
|
115
|
+
|
116
|
+
# Create a new instance of Slop and optionally build options via a block.
|
117
|
+
#
|
118
|
+
# config - A Hash of configuration options.
|
119
|
+
# block - An optional block used to specify options.
|
120
|
+
def initialize(config = {}, &block)
|
121
|
+
@config = DEFAULT_OPTIONS.merge(config)
|
122
|
+
@options = []
|
123
|
+
@commands = {}
|
124
|
+
@trash = []
|
125
|
+
@triggered_options = []
|
126
|
+
@unknown_options = []
|
127
|
+
@callbacks = {}
|
128
|
+
@separators = {}
|
129
|
+
@runner = nil
|
130
|
+
|
131
|
+
if block_given?
|
132
|
+
block.arity == 1 ? yield(self) : instance_eval(&block)
|
133
|
+
end
|
134
|
+
|
135
|
+
if config[:help]
|
136
|
+
on('-h', '--help', 'Display this help message.', tail: true) do
|
137
|
+
$stderr.puts help
|
138
|
+
end
|
139
|
+
end
|
140
|
+
end
|
141
|
+
|
142
|
+
# Is strict mode enabled?
|
143
|
+
#
|
144
|
+
# Returns true if strict mode is enabled, false otherwise.
|
145
|
+
def strict?
|
146
|
+
config[:strict]
|
147
|
+
end
|
148
|
+
|
149
|
+
# Set the banner.
|
150
|
+
#
|
151
|
+
# banner - The String to set the banner.
|
152
|
+
def banner=(banner)
|
153
|
+
config[:banner] = banner
|
154
|
+
end
|
155
|
+
|
156
|
+
# Get or set the banner.
|
157
|
+
#
|
158
|
+
# banner - The String to set the banner.
|
159
|
+
#
|
160
|
+
# Returns the banner String.
|
161
|
+
def banner(banner = nil)
|
162
|
+
config[:banner] = banner if banner
|
163
|
+
config[:banner]
|
164
|
+
end
|
165
|
+
|
166
|
+
# Set the description (used for commands).
|
167
|
+
#
|
168
|
+
# desc - The String to set the description.
|
169
|
+
def description=(desc)
|
170
|
+
config[:description] = desc
|
171
|
+
end
|
172
|
+
|
173
|
+
# Get or set the description (used for commands).
|
174
|
+
#
|
175
|
+
# desc - The String to set the description.
|
176
|
+
#
|
177
|
+
# Returns the description String.
|
178
|
+
def description(desc = nil)
|
179
|
+
config[:description] = desc if desc
|
180
|
+
config[:description]
|
181
|
+
end
|
182
|
+
|
183
|
+
# Add a new command.
|
184
|
+
#
|
185
|
+
# command - The Symbol or String used to identify this command.
|
186
|
+
# options - A Hash of configuration options (see Slop::new)
|
187
|
+
#
|
188
|
+
# Returns a new instance of Slop mapped to this command.
|
189
|
+
def command(command, options = {}, &block)
|
190
|
+
@commands[command.to_s] = Pry::Slop.new(options, &block)
|
191
|
+
end
|
192
|
+
|
193
|
+
# Parse a list of items, executing and gathering options along the way.
|
194
|
+
#
|
195
|
+
# items - The Array of items to extract options from (default: ARGV).
|
196
|
+
# block - An optional block which when used will yield non options.
|
197
|
+
#
|
198
|
+
# Returns an Array of original items.
|
199
|
+
def parse(items = ARGV, &block)
|
200
|
+
parse! items.dup, &block
|
201
|
+
items
|
202
|
+
end
|
203
|
+
|
204
|
+
# Parse a list of items, executing and gathering options along the way.
|
205
|
+
# unlike parse() this method will remove any options and option arguments
|
206
|
+
# from the original Array.
|
207
|
+
#
|
208
|
+
# items - The Array of items to extract options from (default: ARGV).
|
209
|
+
# block - An optional block which when used will yield non options.
|
210
|
+
#
|
211
|
+
# Returns an Array of original items with options removed.
|
212
|
+
def parse!(items = ARGV, &block)
|
213
|
+
if items.empty? && @callbacks[:empty]
|
214
|
+
@callbacks[:empty].each { |cb| cb.call(self) }
|
215
|
+
return items
|
216
|
+
end
|
217
|
+
|
218
|
+
if (cmd = @commands[items[0]])
|
219
|
+
return cmd.parse! items[1..-1]
|
220
|
+
end
|
221
|
+
|
222
|
+
items.each_with_index do |item, index|
|
223
|
+
@trash << index && break if item == '--'
|
224
|
+
autocreate(items, index) if config[:autocreate]
|
225
|
+
process_item(items, index, &block) unless @trash.include?(index)
|
226
|
+
end
|
227
|
+
items.reject!.with_index { |item, index| @trash.include?(index) }
|
228
|
+
|
229
|
+
missing_options = options.select { |opt| opt.required? && opt.count < 1 }
|
230
|
+
if missing_options.any?
|
231
|
+
raise MissingOptionError,
|
232
|
+
"Missing required option(s): #{missing_options.map(&:key).join(', ')}"
|
233
|
+
end
|
234
|
+
|
235
|
+
if @unknown_options.any?
|
236
|
+
raise InvalidOptionError, "Unknown options #{@unknown_options.join(', ')}"
|
237
|
+
end
|
238
|
+
|
239
|
+
if @triggered_options.empty? && @callbacks[:no_options]
|
240
|
+
@callbacks[:no_options].each { |cb| cb.call(self) }
|
241
|
+
end
|
242
|
+
|
243
|
+
@runner.call(self, items) if @runner.respond_to?(:call)
|
244
|
+
|
245
|
+
items
|
246
|
+
end
|
247
|
+
|
248
|
+
# Add an Option.
|
249
|
+
#
|
250
|
+
# objects - An Array with an optional Hash as the last element.
|
251
|
+
#
|
252
|
+
# Examples:
|
253
|
+
#
|
254
|
+
# on '-u', '--username=', 'Your username'
|
255
|
+
# on :v, :verbose, 'Enable verbose mode'
|
256
|
+
#
|
257
|
+
# Returns the created instance of Slop::Option.
|
258
|
+
def on(*objects, &block)
|
259
|
+
option = build_option(objects, &block)
|
260
|
+
options << option
|
261
|
+
option
|
262
|
+
end
|
263
|
+
alias option on
|
264
|
+
alias opt on
|
265
|
+
|
266
|
+
# Fetch an options argument value.
|
267
|
+
#
|
268
|
+
# key - The Symbol or String option short or long flag.
|
269
|
+
#
|
270
|
+
# Returns the Object value for this option, or nil.
|
271
|
+
def [](key)
|
272
|
+
option = fetch_option(key)
|
273
|
+
option.value if option
|
274
|
+
end
|
275
|
+
alias get []
|
276
|
+
|
277
|
+
# Returns a new Hash with option flags as keys and option values as values.
|
278
|
+
#
|
279
|
+
# include_commands - If true, merge options from all sub-commands.
|
280
|
+
def to_hash(include_commands = false)
|
281
|
+
hash = Hash[options.map { |opt| [opt.key.to_sym, opt.value] }]
|
282
|
+
if include_commands
|
283
|
+
@commands.each { |cmd, opts| hash.merge!(cmd.to_sym => opts.to_hash) }
|
284
|
+
end
|
285
|
+
hash
|
286
|
+
end
|
287
|
+
alias to_h to_hash
|
288
|
+
|
289
|
+
# Enumerable interface. Yields each Slop::Option.
|
290
|
+
def each(&block)
|
291
|
+
options.each(&block)
|
292
|
+
end
|
293
|
+
|
294
|
+
# Specify code to be executed when these options are parsed.
|
295
|
+
#
|
296
|
+
# callable - An object responding to a call method.
|
297
|
+
#
|
298
|
+
# yields - The instance of Slop parsing these options
|
299
|
+
# An Array of unparsed arguments
|
300
|
+
#
|
301
|
+
# Example:
|
302
|
+
#
|
303
|
+
# Slop.parse do
|
304
|
+
# on :v, :verbose
|
305
|
+
#
|
306
|
+
# run do |opts, args|
|
307
|
+
# puts "Arguments: #{args.inspect}" if opts.verbose?
|
308
|
+
# end
|
309
|
+
# end
|
310
|
+
def run(callable = nil, &block)
|
311
|
+
@runner = callable || block
|
312
|
+
unless @runner.respond_to?(:call)
|
313
|
+
raise ArgumentError, "You must specify a callable object or a block to #run"
|
314
|
+
end
|
315
|
+
end
|
316
|
+
|
317
|
+
# Check for an options presence.
|
318
|
+
#
|
319
|
+
# Examples:
|
320
|
+
#
|
321
|
+
# opts.parse %w( --foo )
|
322
|
+
# opts.present?(:foo) #=> true
|
323
|
+
# opts.present?(:bar) #=> false
|
324
|
+
#
|
325
|
+
# Returns true if all of the keys are present in the parsed arguments.
|
326
|
+
def present?(*keys)
|
327
|
+
keys.all? { |key| (opt = fetch_option(key)) && opt.count > 0 }
|
328
|
+
end
|
329
|
+
|
330
|
+
# Override this method so we can check if an option? method exists.
|
331
|
+
#
|
332
|
+
# Returns true if this option key exists in our list of options.
|
333
|
+
def respond_to_missing?(method_name, include_all = false)
|
334
|
+
options.any? { |o| o.key == method_name.to_s.chop } || super
|
335
|
+
end
|
336
|
+
|
337
|
+
# Fetch a list of options which were missing from the parsed list.
|
338
|
+
#
|
339
|
+
# Examples:
|
340
|
+
#
|
341
|
+
# opts = Slop.new do
|
342
|
+
# on :n, :name=
|
343
|
+
# on :p, :password=
|
344
|
+
# end
|
345
|
+
#
|
346
|
+
# opts.parse %w[ --name Lee ]
|
347
|
+
# opts.missing #=> ['password']
|
348
|
+
#
|
349
|
+
# Returns an Array of Strings representing missing options.
|
350
|
+
def missing
|
351
|
+
(options - @triggered_options).map(&:key)
|
352
|
+
end
|
353
|
+
|
354
|
+
# Fetch a Slop::Option object.
|
355
|
+
#
|
356
|
+
# key - The Symbol or String option key.
|
357
|
+
#
|
358
|
+
# Examples:
|
359
|
+
#
|
360
|
+
# opts.on(:foo, 'Something fooey', :argument => :optional)
|
361
|
+
# opt = opts.fetch_option(:foo)
|
362
|
+
# opt.class #=> Slop::Option
|
363
|
+
# opt.accepts_optional_argument? #=> true
|
364
|
+
#
|
365
|
+
# Returns an Option or nil if none were found.
|
366
|
+
def fetch_option(key)
|
367
|
+
options.find { |option| [option.long, option.short].include?(clean(key)) }
|
368
|
+
end
|
369
|
+
|
370
|
+
# Fetch a Slop object associated with this command.
|
371
|
+
#
|
372
|
+
# command - The String or Symbol name of the command.
|
373
|
+
#
|
374
|
+
# Examples:
|
375
|
+
#
|
376
|
+
# opts.command :foo do
|
377
|
+
# on :v, :verbose, 'Enable verbose mode'
|
378
|
+
# end
|
379
|
+
#
|
380
|
+
# # ruby run.rb foo -v
|
381
|
+
# opts.fetch_command(:foo).verbose? #=> true
|
382
|
+
def fetch_command(command)
|
383
|
+
@commands[command.to_s]
|
384
|
+
end
|
385
|
+
|
386
|
+
# Add a callback.
|
387
|
+
#
|
388
|
+
# label - The Symbol identifier to attach this callback.
|
389
|
+
#
|
390
|
+
# Returns nothing.
|
391
|
+
def add_callback(label, &block)
|
392
|
+
(@callbacks[label] ||= []) << block
|
393
|
+
end
|
394
|
+
|
395
|
+
# Add string separators between options.
|
396
|
+
#
|
397
|
+
# text - The String text to print.
|
398
|
+
def separator(text)
|
399
|
+
if @separators[options.size]
|
400
|
+
@separators[options.size] << "\n#{text}"
|
401
|
+
else
|
402
|
+
@separators[options.size] = text
|
403
|
+
end
|
404
|
+
end
|
405
|
+
|
406
|
+
# Print a handy Slop help string.
|
407
|
+
#
|
408
|
+
# Returns the banner followed by available option help strings.
|
409
|
+
def to_s
|
410
|
+
heads = options.reject(&:tail?)
|
411
|
+
tails = (options - heads)
|
412
|
+
opts = (heads + tails).select(&:help).map(&:to_s)
|
413
|
+
optstr = opts.each_with_index.map { |o, i|
|
414
|
+
(str = @separators[i + 1]) ? [o, str].join("\n") : o
|
415
|
+
}.join("\n")
|
416
|
+
|
417
|
+
if @commands.any?
|
418
|
+
optstr << "\n" if !optstr.empty?
|
419
|
+
optstr << "\nAvailable commands:\n\n"
|
420
|
+
optstr << commands_to_help
|
421
|
+
optstr << "\n\nSee `<command> --help` for more information on a specific command."
|
422
|
+
end
|
423
|
+
|
424
|
+
banner = config[:banner]
|
425
|
+
banner = "Usage: #{File.basename($0, '.*')}#{' [command]' if @commands.any?} [options]" if banner.nil?
|
426
|
+
if banner
|
427
|
+
"#{banner}\n#{@separators[0] ? "#{@separators[0]}\n" : ''}#{optstr}"
|
428
|
+
else
|
429
|
+
optstr
|
430
|
+
end
|
431
|
+
end
|
432
|
+
alias help to_s
|
433
|
+
|
434
|
+
private
|
435
|
+
|
436
|
+
# Convenience method for present?(:option).
|
437
|
+
#
|
438
|
+
# Examples:
|
439
|
+
#
|
440
|
+
# opts.parse %( --verbose )
|
441
|
+
# opts.verbose? #=> true
|
442
|
+
# opts.other? #=> false
|
443
|
+
#
|
444
|
+
# Returns true if this option is present. If this method does not end
|
445
|
+
# with a ? character it will instead call super().
|
446
|
+
def method_missing(method, *args, &block)
|
447
|
+
meth = method.to_s
|
448
|
+
if meth.end_with?('?')
|
449
|
+
meth.chop!
|
450
|
+
present?(meth) || present?(meth.gsub('_', '-'))
|
451
|
+
else
|
452
|
+
super
|
453
|
+
end
|
454
|
+
end
|
455
|
+
|
456
|
+
# Process a list item, figure out if it's an option, execute any
|
457
|
+
# callbacks, assign any option arguments, and do some sanity checks.
|
458
|
+
#
|
459
|
+
# items - The Array of items to process.
|
460
|
+
# index - The current Integer index of the item we want to process.
|
461
|
+
# block - An optional block which when passed will yield non options.
|
462
|
+
#
|
463
|
+
# Returns nothing.
|
464
|
+
def process_item(items, index, &block)
|
465
|
+
return unless (item = items[index])
|
466
|
+
|
467
|
+
option, argument = extract_option(item) if item.start_with?('-')
|
468
|
+
|
469
|
+
if option
|
470
|
+
option.count += 1 unless item.start_with?('--no-')
|
471
|
+
option.count += 1 if option.key[0, 3] == "no-"
|
472
|
+
@trash << index
|
473
|
+
@triggered_options << option
|
474
|
+
|
475
|
+
if option.expects_argument?
|
476
|
+
argument ||= items.at(index + 1)
|
477
|
+
|
478
|
+
if !argument || argument =~ /\A--?[a-zA-Z][a-zA-Z0-9_-]*\z/
|
479
|
+
raise MissingArgumentError, "#{option.key} expects an argument"
|
480
|
+
end
|
481
|
+
|
482
|
+
execute_option(option, argument, index, item)
|
483
|
+
elsif option.accepts_optional_argument?
|
484
|
+
argument ||= items.at(index + 1)
|
485
|
+
|
486
|
+
if argument && argument =~ /\A([^\-?]|-\d)+/
|
487
|
+
execute_option(option, argument, index, item)
|
488
|
+
else
|
489
|
+
option.call(nil)
|
490
|
+
end
|
491
|
+
elsif config[:multiple_switches] && argument
|
492
|
+
execute_multiple_switches(option, argument, index)
|
493
|
+
else
|
494
|
+
option.value = option.count > 0
|
495
|
+
option.call(nil)
|
496
|
+
end
|
497
|
+
else
|
498
|
+
@unknown_options << item if strict? && item =~ /\A--?/
|
499
|
+
block.call(item) if block && !@trash.include?(index)
|
500
|
+
end
|
501
|
+
end
|
502
|
+
|
503
|
+
# Execute an option, firing off callbacks and assigning arguments.
|
504
|
+
#
|
505
|
+
# option - The Slop::Option object found by #process_item.
|
506
|
+
# argument - The argument Object to assign to this option.
|
507
|
+
# index - The current Integer index of the object we're processing.
|
508
|
+
# item - The optional String item we're processing.
|
509
|
+
#
|
510
|
+
# Returns nothing.
|
511
|
+
def execute_option(option, argument, index, item = nil)
|
512
|
+
if !option
|
513
|
+
if config[:multiple_switches] && strict?
|
514
|
+
raise InvalidOptionError, "Unknown option -#{item}"
|
515
|
+
end
|
516
|
+
|
517
|
+
return
|
518
|
+
end
|
519
|
+
|
520
|
+
if argument
|
521
|
+
unless item && item.end_with?("=#{argument}")
|
522
|
+
@trash << index + 1 unless option.argument_in_value
|
523
|
+
end
|
524
|
+
option.value = argument
|
525
|
+
else
|
526
|
+
option.value = option.count > 0
|
527
|
+
end
|
528
|
+
|
529
|
+
if option.match? && !argument.match(option.config[:match])
|
530
|
+
raise InvalidArgumentError, "#{argument} is an invalid argument"
|
531
|
+
end
|
532
|
+
|
533
|
+
option.call(option.value)
|
534
|
+
end
|
535
|
+
|
536
|
+
# Execute a `-abc` type option where a, b and c are all options. This
|
537
|
+
# method is only executed if the multiple_switches argument is true.
|
538
|
+
#
|
539
|
+
# option - The first Option object.
|
540
|
+
# argument - The argument to this option. (Split into multiple Options).
|
541
|
+
# index - The index of the current item being processed.
|
542
|
+
#
|
543
|
+
# Returns nothing.
|
544
|
+
def execute_multiple_switches(option, argument, index)
|
545
|
+
execute_option(option, nil, index)
|
546
|
+
argument.split('').each do |key|
|
547
|
+
next unless (opt = fetch_option(key))
|
548
|
+
|
549
|
+
opt.count += 1
|
550
|
+
execute_option(opt, nil, index, key)
|
551
|
+
end
|
552
|
+
end
|
553
|
+
|
554
|
+
# Extract an option from a flag.
|
555
|
+
#
|
556
|
+
# flag - The flag key used to extract an option.
|
557
|
+
#
|
558
|
+
# Returns an Array of [option, argument].
|
559
|
+
def extract_option(flag)
|
560
|
+
option = fetch_option(flag)
|
561
|
+
option ||= fetch_option(flag.downcase) if config[:ignore_case]
|
562
|
+
option ||= fetch_option(flag.gsub(/([^-])-/, '\1_'))
|
563
|
+
|
564
|
+
unless option
|
565
|
+
case flag
|
566
|
+
when /\A--?([^=]+)=(.+)\z/, /\A-([a-zA-Z])(.+)\z/, /\A--no-(.+)\z/
|
567
|
+
option, argument = fetch_option($1), ($2 || false)
|
568
|
+
option.argument_in_value = true if option
|
569
|
+
end
|
570
|
+
end
|
571
|
+
|
572
|
+
[option, argument]
|
573
|
+
end
|
574
|
+
|
575
|
+
# Autocreate an option on the fly. See the :autocreate Slop config option.
|
576
|
+
#
|
577
|
+
# items - The Array of items we're parsing.
|
578
|
+
# index - The current Integer index for the item we're processing.
|
579
|
+
#
|
580
|
+
# Returns nothing.
|
581
|
+
def autocreate(items, index)
|
582
|
+
flag = items[index]
|
583
|
+
if !fetch_option(flag) && !@trash.include?(index)
|
584
|
+
option = build_option(Array(flag))
|
585
|
+
argument = items[index + 1]
|
586
|
+
option.config[:argument] = (argument && argument !~ /\A--?/)
|
587
|
+
option.config[:autocreated] = true
|
588
|
+
options << option
|
589
|
+
end
|
590
|
+
end
|
591
|
+
|
592
|
+
# Build an option from a list of objects.
|
593
|
+
#
|
594
|
+
# objects - An Array of objects used to build this option.
|
595
|
+
#
|
596
|
+
# Returns a new instance of Slop::Option.
|
597
|
+
def build_option(objects, &block)
|
598
|
+
config = {}
|
599
|
+
config[:argument] = true if @config[:arguments]
|
600
|
+
config[:optional_argument] = true if @config[:optional_arguments]
|
601
|
+
|
602
|
+
if objects.last.is_a?(Hash)
|
603
|
+
config.merge!(objects.last)
|
604
|
+
objects.pop
|
605
|
+
end
|
606
|
+
short = extract_short_flag(objects, config)
|
607
|
+
long = extract_long_flag(objects, config)
|
608
|
+
desc = objects[0].respond_to?(:to_str) ? objects.shift : nil
|
609
|
+
|
610
|
+
Option.new(self, short, long, desc, config, &block)
|
611
|
+
end
|
612
|
+
|
613
|
+
# Extract the short flag from an item.
|
614
|
+
#
|
615
|
+
# objects - The Array of objects passed from #build_option.
|
616
|
+
# config - The Hash of configuration options built in #build_option.
|
617
|
+
def extract_short_flag(objects, config)
|
618
|
+
flag = clean(objects.first)
|
619
|
+
|
620
|
+
if flag.size == 2 && flag.end_with?('=')
|
621
|
+
config[:argument] ||= true
|
622
|
+
flag.chop!
|
623
|
+
end
|
624
|
+
|
625
|
+
if flag.size == 1
|
626
|
+
objects.shift
|
627
|
+
flag
|
628
|
+
end
|
629
|
+
end
|
630
|
+
|
631
|
+
# Extract the long flag from an item.
|
632
|
+
#
|
633
|
+
# objects - The Array of objects passed from #build_option.
|
634
|
+
# config - The Hash of configuration options built in #build_option.
|
635
|
+
def extract_long_flag(objects, config)
|
636
|
+
flag = objects.first.to_s
|
637
|
+
if flag =~ /\A(?:--?)?[a-zA-Z][a-zA-Z0-9_-]+\=?\??\z/
|
638
|
+
config[:argument] ||= true if flag.end_with?('=')
|
639
|
+
config[:optional_argument] = true if flag.end_with?('=?')
|
640
|
+
objects.shift
|
641
|
+
clean(flag).sub(/\=\??\z/, '')
|
642
|
+
end
|
643
|
+
end
|
644
|
+
|
645
|
+
# Remove any leading -- characters from a string.
|
646
|
+
#
|
647
|
+
# object - The Object we want to cast to a String and clean.
|
648
|
+
#
|
649
|
+
# Returns the newly cleaned String with leading -- characters removed.
|
650
|
+
def clean(object)
|
651
|
+
object.to_s.sub(/\A--?/, '')
|
652
|
+
end
|
653
|
+
|
654
|
+
def commands_to_help
|
655
|
+
padding = 0
|
656
|
+
@commands.each { |c, _| padding = c.size if c.size > padding }
|
657
|
+
@commands.map do |cmd, opts|
|
658
|
+
" #{cmd}#{' ' * (padding - cmd.size)} #{opts.description}"
|
659
|
+
end.join("\n")
|
660
|
+
end
|
661
|
+
end
|