ruby-lint 0.0.3 → 0.0.4
Sign up to get free protection for your applications and to get access to all the features.
- data.tar.gz.asc +14 -14
- data/.gitignore +1 -2
- data/.rubocop.yml +59 -0
- data/.travis.yml +3 -7
- data/.yardopts +2 -2
- data/Gemfile +4 -0
- data/MANIFEST +103 -67
- data/README.md +13 -31
- data/Rakefile +1 -6
- data/checksum/ruby-lint-0.0.3.gem.sha512 +1 -0
- data/debug/memory_usage.rb +14 -0
- data/debug/profile.rb +18 -0
- data/doc/architecture.md +14 -18
- data/doc/changelog.md +29 -0
- data/doc/code_analysis.md +12 -12
- data/doc/configuration.md +21 -50
- data/doc/graphviz/flow.dot +1 -1
- data/doc/images/flow.png +0 -0
- data/lib/ruby-lint.rb +33 -19
- data/lib/ruby-lint/analysis/argument_amount.rb +84 -0
- data/lib/ruby-lint/analysis/base.rb +132 -0
- data/lib/ruby-lint/analysis/confusing_variables.rb +26 -0
- data/lib/ruby-lint/analysis/pedantics.rb +36 -0
- data/lib/ruby-lint/analysis/shadowing_variables.rb +47 -0
- data/lib/ruby-lint/analysis/undefined_methods.rb +91 -0
- data/lib/ruby-lint/analysis/undefined_variables.rb +46 -0
- data/lib/ruby-lint/analysis/unused_variables.rb +49 -0
- data/lib/ruby-lint/ast/builder.rb +17 -0
- data/lib/ruby-lint/ast/node.rb +39 -0
- data/lib/ruby-lint/cli.rb +0 -19
- data/lib/ruby-lint/cli/analyze.rb +54 -28
- data/lib/ruby-lint/cli/ast.rb +21 -6
- data/lib/ruby-lint/cli/base.rb +0 -23
- data/lib/ruby-lint/configuration.rb +112 -42
- data/lib/ruby-lint/constant_loader.rb +10 -28
- data/lib/ruby-lint/default_names.rb +19 -0
- data/lib/ruby-lint/definition/constant_proxy.rb +56 -0
- data/lib/ruby-lint/definition/ruby_method.rb +21 -143
- data/lib/ruby-lint/definition/ruby_object.rb +142 -246
- data/lib/ruby-lint/definition_builder/base.rb +43 -0
- data/lib/ruby-lint/definition_builder/primitive.rb +39 -0
- data/lib/ruby-lint/definition_builder/ruby_array.rb +77 -0
- data/lib/ruby-lint/definition_builder/ruby_block.rb +34 -0
- data/lib/ruby-lint/definition_builder/ruby_class.rb +24 -0
- data/lib/ruby-lint/definition_builder/ruby_hash.rb +37 -0
- data/lib/ruby-lint/definition_builder/ruby_method.rb +84 -0
- data/lib/ruby-lint/definition_builder/ruby_module.rb +68 -0
- data/lib/ruby-lint/definition_generator.rb +41 -15
- data/lib/ruby-lint/definitions/core/arg0.rb +1 -1
- data/lib/ruby-lint/definitions/core/argf.rb +1 -1
- data/lib/ruby-lint/definitions/core/argument_error.rb +2 -2
- data/lib/ruby-lint/definitions/core/argv.rb +1 -1
- data/lib/ruby-lint/definitions/core/array.rb +6 -6
- data/lib/ruby-lint/definitions/core/autoload.rb +2 -2
- data/lib/ruby-lint/definitions/core/basic_object.rb +3 -3
- data/lib/ruby-lint/definitions/core/bignum.rb +2 -2
- data/lib/ruby-lint/definitions/core/binding.rb +2 -2
- data/lib/ruby-lint/definitions/core/class.rb +2 -2
- data/lib/ruby-lint/definitions/core/comparable.rb +1 -1
- data/lib/ruby-lint/definitions/core/complex.rb +4 -4
- data/lib/ruby-lint/definitions/core/condition_variable.rb +2 -2
- data/lib/ruby-lint/definitions/core/continuation.rb +2 -2
- data/lib/ruby-lint/definitions/core/data.rb +2 -2
- data/lib/ruby-lint/definitions/core/date.rb +34 -34
- data/lib/ruby-lint/definitions/core/date_time.rb +26 -26
- data/lib/ruby-lint/definitions/core/default_record_separator.rb +1 -1
- data/lib/ruby-lint/definitions/core/digest.rb +14 -14
- data/lib/ruby-lint/definitions/core/dir.rb +34 -34
- data/lib/ruby-lint/definitions/core/encoding.rb +237 -237
- data/lib/ruby-lint/definitions/core/encoding_error.rb +2 -2
- data/lib/ruby-lint/definitions/core/enumerable.rb +15 -15
- data/lib/ruby-lint/definitions/core/enumerator.rb +2 -2
- data/lib/ruby-lint/definitions/core/env.rb +1 -1
- data/lib/ruby-lint/definitions/core/eoferror.rb +2 -2
- data/lib/ruby-lint/definitions/core/erb.rb +22 -22
- data/lib/ruby-lint/definitions/core/errno.rb +525 -525
- data/lib/ruby-lint/definitions/core/etc.rb +6 -6
- data/lib/ruby-lint/definitions/core/exception.rb +2 -2
- data/lib/ruby-lint/definitions/core/false.rb +1 -1
- data/lib/ruby-lint/definitions/core/false_class.rb +2 -2
- data/lib/ruby-lint/definitions/core/fatal_error.rb +2 -2
- data/lib/ruby-lint/definitions/core/fiber.rb +2 -2
- data/lib/ruby-lint/definitions/core/fiber_error.rb +2 -2
- data/lib/ruby-lint/definitions/core/file.rb +68 -68
- data/lib/ruby-lint/definitions/core/file_list.rb +2 -2
- data/lib/ruby-lint/definitions/core/file_test.rb +1 -1
- data/lib/ruby-lint/definitions/core/file_utils.rb +16 -16
- data/lib/ruby-lint/definitions/core/fixnum.rb +4 -4
- data/lib/ruby-lint/definitions/core/float.rb +16 -16
- data/lib/ruby-lint/definitions/core/float_domain_error.rb +2 -2
- data/lib/ruby-lint/definitions/core/gc.rb +2 -2
- data/lib/ruby-lint/definitions/core/gem.rb +119 -119
- data/lib/ruby-lint/definitions/core/hash.rb +14 -14
- data/lib/ruby-lint/definitions/core/immediate_value.rb +1 -1
- data/lib/ruby-lint/definitions/core/index_error.rb +2 -2
- data/lib/ruby-lint/definitions/core/integer.rb +2 -2
- data/lib/ruby-lint/definitions/core/interrupt.rb +2 -2
- data/lib/ruby-lint/definitions/core/io.rb +38 -38
- data/lib/ruby-lint/definitions/core/ioerror.rb +2 -2
- data/lib/ruby-lint/definitions/core/kernel.rb +3 -3
- data/lib/ruby-lint/definitions/core/key_error.rb +2 -2
- data/lib/ruby-lint/definitions/core/load_error.rb +6 -6
- data/lib/ruby-lint/definitions/core/local_jump_error.rb +2 -2
- data/lib/ruby-lint/definitions/core/main.rb +1 -1
- data/lib/ruby-lint/definitions/core/marshal.rb +35 -35
- data/lib/ruby-lint/definitions/core/match_data.rb +2 -2
- data/lib/ruby-lint/definitions/core/math.rb +7 -7
- data/lib/ruby-lint/definitions/core/memory_segmention_error.rb +2 -2
- data/lib/ruby-lint/definitions/core/method.rb +2 -2
- data/lib/ruby-lint/definitions/core/module.rb +1 -1
- data/lib/ruby-lint/definitions/core/monitor.rb +4 -4
- data/lib/ruby-lint/definitions/core/monitor_mixin.rb +5 -5
- data/lib/ruby-lint/definitions/core/mutex.rb +2 -2
- data/lib/ruby-lint/definitions/core/name_error.rb +2 -2
- data/lib/ruby-lint/definitions/core/nil.rb +1 -1
- data/lib/ruby-lint/definitions/core/nil_class.rb +2 -2
- data/lib/ruby-lint/definitions/core/no_memory_error.rb +2 -2
- data/lib/ruby-lint/definitions/core/no_method_error.rb +2 -2
- data/lib/ruby-lint/definitions/core/not_implemented_error.rb +2 -2
- data/lib/ruby-lint/definitions/core/numeric.rb +2 -2
- data/lib/ruby-lint/definitions/core/object.rb +2 -2
- data/lib/ruby-lint/definitions/core/object_space.rb +1 -1
- data/lib/ruby-lint/definitions/core/open_struct.rb +3 -3
- data/lib/ruby-lint/definitions/core/option_parser.rb +83 -83
- data/lib/ruby-lint/definitions/core/precision.rb +1 -1
- data/lib/ruby-lint/definitions/core/primitive_failure.rb +2 -2
- data/lib/ruby-lint/definitions/core/proc.rb +2 -2
- data/lib/ruby-lint/definitions/core/process.rb +29 -29
- data/lib/ruby-lint/definitions/core/psych.rb +149 -149
- data/lib/ruby-lint/definitions/core/queue.rb +2 -2
- data/lib/ruby-lint/definitions/core/rake.rb +117 -117
- data/lib/ruby-lint/definitions/core/rake_file_utils.rb +1 -1
- data/lib/ruby-lint/definitions/core/rakeversion.rb +1 -1
- data/lib/ruby-lint/definitions/core/random.rb +2 -2
- data/lib/ruby-lint/definitions/core/range.rb +4 -4
- data/lib/ruby-lint/definitions/core/range_error.rb +2 -2
- data/lib/ruby-lint/definitions/core/rational.rb +2 -2
- data/lib/ruby-lint/definitions/core/rb_config.rb +3 -3
- data/lib/ruby-lint/definitions/core/regexp.rb +28 -28
- data/lib/ruby-lint/definitions/core/regexp_error.rb +2 -2
- data/lib/ruby-lint/definitions/core/rubinius.rb +1057 -1057
- data/lib/ruby-lint/definitions/core/ruby_copyright.rb +1 -1
- data/lib/ruby-lint/definitions/core/ruby_description.rb +1 -1
- data/lib/ruby-lint/definitions/core/ruby_engine.rb +1 -1
- data/lib/ruby-lint/definitions/core/ruby_lint.rb +9 -9
- data/lib/ruby-lint/definitions/core/ruby_patchlevel.rb +1 -1
- data/lib/ruby-lint/definitions/core/ruby_platform.rb +1 -1
- data/lib/ruby-lint/definitions/core/ruby_release_date.rb +1 -1
- data/lib/ruby-lint/definitions/core/ruby_version.rb +1 -1
- data/lib/ruby-lint/definitions/core/runtime_error.rb +2 -2
- data/lib/ruby-lint/definitions/core/scan_error.rb +2 -2
- data/lib/ruby-lint/definitions/core/script_error.rb +2 -2
- data/lib/ruby-lint/definitions/core/security_error.rb +2 -2
- data/lib/ruby-lint/definitions/core/shellwords.rb +1 -1
- data/lib/ruby-lint/definitions/core/signal.rb +3 -3
- data/lib/ruby-lint/definitions/core/signal_exception.rb +2 -2
- data/lib/ruby-lint/definitions/core/singleton.rb +2 -2
- data/lib/ruby-lint/definitions/core/sized_queue.rb +2 -2
- data/lib/ruby-lint/definitions/core/standard_error.rb +2 -2
- data/lib/ruby-lint/definitions/core/stderr.rb +1 -1
- data/lib/ruby-lint/definitions/core/stdin.rb +1 -1
- data/lib/ruby-lint/definitions/core/stdout.rb +1 -1
- data/lib/ruby-lint/definitions/core/stop_iteration.rb +2 -2
- data/lib/ruby-lint/definitions/core/string.rb +21 -21
- data/lib/ruby-lint/definitions/core/string_io.rb +7 -7
- data/lib/ruby-lint/definitions/core/string_scanner.rb +4 -4
- data/lib/ruby-lint/definitions/core/struct.rb +20 -20
- data/lib/ruby-lint/definitions/core/syck.rb +4 -4
- data/lib/ruby-lint/definitions/core/symbol.rb +2 -2
- data/lib/ruby-lint/definitions/core/syntax_error.rb +2 -2
- data/lib/ruby-lint/definitions/core/system_call_error.rb +2 -2
- data/lib/ruby-lint/definitions/core/system_exit.rb +2 -2
- data/lib/ruby-lint/definitions/core/system_stack_error.rb +2 -2
- data/lib/ruby-lint/definitions/core/thread.rb +6 -6
- data/lib/ruby-lint/definitions/core/thread_error.rb +2 -2
- data/lib/ruby-lint/definitions/core/thread_group.rb +2 -2
- data/lib/ruby-lint/definitions/core/time.rb +3 -3
- data/lib/ruby-lint/definitions/core/toplevel_binding.rb +1 -1
- data/lib/ruby-lint/definitions/core/true.rb +1 -1
- data/lib/ruby-lint/definitions/core/true_class.rb +2 -2
- data/lib/ruby-lint/definitions/core/type_error.rb +2 -2
- data/lib/ruby-lint/definitions/core/unbound_method.rb +2 -2
- data/lib/ruby-lint/definitions/core/unmarshalable.rb +1 -1
- data/lib/ruby-lint/definitions/core/unsupported_library_error.rb +2 -2
- data/lib/ruby-lint/definitions/core/weak_ref.rb +4 -4
- data/lib/ruby-lint/definitions/core/zero_division_error.rb +2 -2
- data/lib/ruby-lint/definitions/rails.rb +12 -0
- data/lib/ruby-lint/definitions/rails/abstract_controller.rb +335 -0
- data/lib/ruby-lint/definitions/rails/action_controller.rb +1244 -0
- data/lib/ruby-lint/definitions/rails/action_dispatch.rb +741 -0
- data/lib/ruby-lint/definitions/rails/action_mailer.rb +502 -0
- data/lib/ruby-lint/definitions/rails/action_pack.rb +16 -0
- data/lib/ruby-lint/definitions/rails/action_view.rb +898 -0
- data/lib/ruby-lint/definitions/rails/active_model.rb +437 -0
- data/lib/ruby-lint/definitions/rails/active_record.rb +2901 -0
- data/lib/ruby-lint/definitions/rails/active_support.rb +1963 -0
- data/lib/ruby-lint/definitions/rails/arel.rb +665 -0
- data/lib/ruby-lint/definitions/rails/rails.rb +452 -0
- data/lib/ruby-lint/definitions/rails/sprockets.rb +968 -0
- data/lib/ruby-lint/generated_constant.rb +33 -0
- data/lib/ruby-lint/helper/constant_paths.rb +30 -21
- data/lib/ruby-lint/inspector.rb +20 -2
- data/lib/ruby-lint/iterator.rb +11 -171
- data/lib/ruby-lint/nested_stack.rb +46 -0
- data/lib/ruby-lint/parser.rb +24 -806
- data/lib/ruby-lint/report.rb +2 -30
- data/lib/ruby-lint/report/entry.rb +2 -2
- data/lib/ruby-lint/runner.rb +81 -0
- data/lib/ruby-lint/template/definition.erb +12 -8
- data/lib/ruby-lint/variable_predicates.rb +16 -58
- data/lib/ruby-lint/version.rb +1 -1
- data/lib/ruby-lint/virtual_machine.rb +1044 -0
- data/ruby-lint.gemspec +17 -7
- data/spec/fixtures/complex/rails/basic_controller.rb +9 -0
- data/spec/fixtures/complex/rcap.rb +38 -0
- data/spec/fixtures/complex/slop.rb +680 -0
- data/spec/fixtures/config.yml +8 -0
- data/spec/fixtures/invalid.rb +3 -0
- data/spec/fixtures/invalid_2.rb +3 -0
- data/spec/fixtures/syntax_error.rb +1 -0
- data/spec/fixtures/valid.rb +3 -0
- data/spec/helper.rb +6 -0
- data/spec/ruby-lint/{analyze → analysis}/argument_amount.rb +7 -7
- data/spec/ruby-lint/analysis/confusing_variables.rb +46 -0
- data/spec/ruby-lint/analysis/pedantics.rb +43 -0
- data/spec/ruby-lint/{analyze → analysis}/shadowing_variables.rb +5 -5
- data/spec/ruby-lint/{analyze → analysis}/undefined_methods.rb +18 -18
- data/spec/ruby-lint/{analyze → analysis}/undefined_variables.rb +19 -25
- data/spec/ruby-lint/{analyze → analysis}/unused_variables.rb +29 -16
- data/spec/ruby-lint/ast/node.rb +33 -0
- data/spec/ruby-lint/cli/analyze.rb +31 -0
- data/spec/ruby-lint/cli/ast.rb +23 -0
- data/spec/ruby-lint/configuration.rb +45 -6
- data/spec/ruby-lint/definition/constant_proxy.rb +31 -0
- data/spec/ruby-lint/definition/dsl.rb +13 -20
- data/spec/ruby-lint/definition/ruby_method.rb +65 -148
- data/spec/ruby-lint/definition/ruby_object.rb +60 -96
- data/spec/ruby-lint/definition_builder/primitive.rb +62 -0
- data/spec/ruby-lint/definition_builder/ruby_class.rb +108 -0
- data/spec/ruby-lint/definition_builder/ruby_method.rb +53 -0
- data/spec/ruby-lint/definition_builder/ruby_module.rb +50 -0
- data/spec/ruby-lint/iterator.rb +40 -16
- data/spec/ruby-lint/nested_stack.rb +23 -0
- data/spec/ruby-lint/parser.rb +14 -0
- data/spec/ruby-lint/report.rb +0 -10
- data/spec/ruby-lint/report/entry.rb +8 -4
- data/spec/ruby-lint/runner.rb +32 -0
- data/spec/ruby-lint/virtual_machine/assignments/arrays.rb +87 -0
- data/spec/ruby-lint/virtual_machine/assignments/hashes.rb +52 -0
- data/spec/ruby-lint/virtual_machine/assignments/optional.rb +41 -0
- data/spec/ruby-lint/{definitions_builder → virtual_machine}/assignments/return_values.rb +11 -11
- data/spec/ruby-lint/virtual_machine/assignments/variables.rb +85 -0
- data/spec/ruby-lint/virtual_machine/associate_nodes.rb +17 -0
- data/spec/ruby-lint/virtual_machine/autoloading.rb +31 -0
- data/spec/ruby-lint/virtual_machine/blocks.rb +64 -0
- data/spec/ruby-lint/virtual_machine/classes/class_methods.rb +21 -0
- data/spec/ruby-lint/virtual_machine/classes/extending.rb +104 -0
- data/spec/ruby-lint/virtual_machine/classes/redefining.rb +24 -0
- data/spec/ruby-lint/virtual_machine/classes/sclass.rb +94 -0
- data/spec/ruby-lint/virtual_machine/classes/scoping.rb +60 -0
- data/spec/ruby-lint/virtual_machine/complex/rails.rb +18 -0
- data/spec/ruby-lint/virtual_machine/complex/rcap.rb +15 -0
- data/spec/ruby-lint/virtual_machine/complex/slop.rb +16 -0
- data/spec/ruby-lint/{definitions_builder → virtual_machine}/for.rb +2 -2
- data/spec/ruby-lint/virtual_machine/freeze.rb +13 -0
- data/spec/ruby-lint/virtual_machine/interpolation.rb +12 -0
- data/spec/ruby-lint/virtual_machine/methods/defining.rb +40 -0
- data/spec/ruby-lint/virtual_machine/methods/exporting.rb +19 -0
- data/spec/ruby-lint/virtual_machine/methods/parameters.rb +77 -0
- data/spec/ruby-lint/virtual_machine/methods/patching.rb +26 -0
- data/spec/ruby-lint/virtual_machine/methods/scoping.rb +63 -0
- data/spec/ruby-lint/virtual_machine/methods/visibility.rb +64 -0
- data/spec/ruby-lint/{definitions_builder → virtual_machine}/modules.rb +16 -16
- data/spec/ruby-lint/virtual_machine/reference_amount.rb +33 -0
- data/spec/ruby-lint/virtual_machine/unused.rb +17 -0
- data/spec/support/bacon.rb +13 -13
- data/spec/support/building.rb +23 -16
- data/spec/support/coveralls.rb +5 -0
- data/spec/support/fixtures.rb +20 -0
- data/spec/support/parsing.rb +3 -5
- data/spec/support/simplecov.rb +5 -3
- data/task/build.rake +1 -1
- data/task/cop.rake +11 -0
- data/task/generate.rake +40 -2
- data/task/profile.rake +27 -0
- data/task/todo.rake +6 -0
- data/task/travis.rake +7 -0
- metadata +160 -75
- metadata.gz.asc +14 -14
- data/doc/build/.gitkeep +0 -0
- data/lib/ruby-lint/analyze/argument_amount.rb +0 -73
- data/lib/ruby-lint/analyze/shadowing_variables.rb +0 -32
- data/lib/ruby-lint/analyze/undefined_methods.rb +0 -68
- data/lib/ruby-lint/analyze/undefined_variables.rb +0 -72
- data/lib/ruby-lint/analyze/unused_variables.rb +0 -48
- data/lib/ruby-lint/base.rb +0 -85
- data/lib/ruby-lint/definitions_builder.rb +0 -692
- data/lib/ruby-lint/helper/conversion.rb +0 -33
- data/lib/ruby-lint/helper/current_scope.rb +0 -98
- data/lib/ruby-lint/helper/methods.rb +0 -91
- data/lib/ruby-lint/node.rb +0 -107
- data/lib/ruby-lint/parser_error.rb +0 -30
- data/spec/ruby-lint/constant_loader.rb +0 -32
- data/spec/ruby-lint/definition/method_calls.rb +0 -26
- data/spec/ruby-lint/definitions_builder/assignments/arrays.rb +0 -71
- data/spec/ruby-lint/definitions_builder/assignments/hashes.rb +0 -65
- data/spec/ruby-lint/definitions_builder/assignments/objects.rb +0 -23
- data/spec/ruby-lint/definitions_builder/assignments/optional.rb +0 -22
- data/spec/ruby-lint/definitions_builder/assignments/variables.rb +0 -71
- data/spec/ruby-lint/definitions_builder/associate_nodes.rb +0 -17
- data/spec/ruby-lint/definitions_builder/blocks.rb +0 -40
- data/spec/ruby-lint/definitions_builder/classes.rb +0 -230
- data/spec/ruby-lint/definitions_builder/methods.rb +0 -147
- data/spec/ruby-lint/definitions_builder/reference_amount.rb +0 -31
- data/spec/ruby-lint/definitions_builder/unused.rb +0 -15
- data/spec/ruby-lint/node.rb +0 -38
- data/spec/ruby-lint/parser/assignments.rb +0 -225
- data/spec/ruby-lint/parser/classes.rb +0 -110
- data/spec/ruby-lint/parser/errors.rb +0 -12
- data/spec/ruby-lint/parser/metadata.rb +0 -17
- data/spec/ruby-lint/parser/method_definitions.rb +0 -111
- data/spec/ruby-lint/parser/methods.rb +0 -217
- data/spec/ruby-lint/parser/modules.rb +0 -70
- data/spec/ruby-lint/parser/operators.rb +0 -40
- data/spec/ruby-lint/parser/statements/begin.rb +0 -55
- data/spec/ruby-lint/parser/statements/case.rb +0 -34
- data/spec/ruby-lint/parser/statements/defined.rb +0 -11
- data/spec/ruby-lint/parser/statements/for.rb +0 -34
- data/spec/ruby-lint/parser/statements/if.rb +0 -46
- data/spec/ruby-lint/parser/statements/return.rb +0 -14
- data/spec/ruby-lint/parser/statements/super.rb +0 -49
- data/spec/ruby-lint/parser/statements/unless.rb +0 -42
- data/spec/ruby-lint/parser/statements/until.rb +0 -25
- data/spec/ruby-lint/parser/statements/while.rb +0 -25
- data/spec/ruby-lint/parser/statements/yield.rb +0 -18
- data/spec/ruby-lint/parser/types/arrays.rb +0 -47
- data/spec/ruby-lint/parser/types/booleans.rb +0 -11
- data/spec/ruby-lint/parser/types/constants.rb +0 -32
- data/spec/ruby-lint/parser/types/hashes.rb +0 -55
- data/spec/ruby-lint/parser/types/nil.rb +0 -7
- data/spec/ruby-lint/parser/types/numbers.rb +0 -11
- data/spec/ruby-lint/parser/types/procs.rb +0 -11
- data/spec/ruby-lint/parser/types/ranges.rb +0 -11
- data/spec/ruby-lint/parser/types/regexp.rb +0 -27
- data/spec/ruby-lint/parser/types/strings.rb +0 -44
- data/spec/ruby-lint/parser/types/symbols.rb +0 -15
- data/task/stdlib.rake +0 -23
data/ruby-lint.gemspec
CHANGED
@@ -10,13 +10,26 @@ Gem::Specification.new do |s|
|
|
10
10
|
s.homepage = 'https://github.com/yorickpeterse/ruby-lint/'
|
11
11
|
s.description = s.summary
|
12
12
|
s.executables = ['ruby-lint']
|
13
|
+
s.license = 'MIT'
|
14
|
+
|
15
|
+
s.post_install_message = <<-EOF.strip
|
16
|
+
Thanks for installing ruby-lint. Keep in mind that ruby-lint is still a young
|
17
|
+
project and as such there will be bugs, false positives and otherwise weird
|
18
|
+
behaviour. If you encounter any of these issues please report them (and provide
|
19
|
+
some sample code) at the issue tracker:
|
20
|
+
|
21
|
+
https://github.com/YorickPeterse/ruby-lint/issues/new
|
22
|
+
|
23
|
+
If you have any further questions you can find me on Twitter/Freenode/Github
|
24
|
+
using the handle "yorickpeterse".
|
25
|
+
EOF
|
13
26
|
|
14
27
|
s.files = File.read(File.expand_path('../MANIFEST', __FILE__)).split("\n")
|
15
28
|
|
16
29
|
s.has_rdoc = 'yard'
|
17
30
|
s.required_ruby_version = '>= 1.9.3'
|
18
31
|
|
19
|
-
s.add_dependency '
|
32
|
+
s.add_dependency 'parser', ['>= 2.0.0.pre2']
|
20
33
|
s.add_dependency 'slop'
|
21
34
|
|
22
35
|
s.add_development_dependency 'rake'
|
@@ -26,10 +39,7 @@ Gem::Specification.new do |s|
|
|
26
39
|
s.add_development_dependency 'bacon-colored_output'
|
27
40
|
s.add_development_dependency 'simplecov'
|
28
41
|
s.add_development_dependency 'rubygems-openpgp'
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
if RUBY_PLATFORM != 'java'
|
33
|
-
s.add_development_dependency 'redcarpet', ['>= 2.1.1']
|
34
|
-
end
|
42
|
+
s.add_development_dependency 'coveralls'
|
43
|
+
s.add_development_dependency 'json'
|
44
|
+
s.add_development_dependency 'kramdown'
|
35
45
|
end
|
@@ -0,0 +1,38 @@
|
|
1
|
+
module RCAP
|
2
|
+
# The VM will raise an error if this class doesn't exist.
|
3
|
+
module Base
|
4
|
+
class Alert
|
5
|
+
end
|
6
|
+
end
|
7
|
+
|
8
|
+
module CAP_1_2
|
9
|
+
|
10
|
+
# An Alert object is valid if
|
11
|
+
# * it has an identifier
|
12
|
+
# * it has a sender
|
13
|
+
# * it has a sent time
|
14
|
+
# * it has a valid status value
|
15
|
+
# * it has a valid messge type value
|
16
|
+
# * it has a valid scope value
|
17
|
+
# * all Info objects contained in infos are valid
|
18
|
+
class Alert < RCAP::Base::Alert
|
19
|
+
|
20
|
+
XMLNS = "urn:oasis:names:tc:emergency:cap:1.2"
|
21
|
+
CAP_VERSION = "1.2"
|
22
|
+
|
23
|
+
STATUS_DRAFT = "Draft"
|
24
|
+
# Valid values for status
|
25
|
+
VALID_STATUSES = [ STATUS_ACTUAL, STATUS_EXERCISE, STATUS_SYSTEM, STATUS_TEST, STATUS_DRAFT ]
|
26
|
+
|
27
|
+
# @return [String]
|
28
|
+
def xmlns
|
29
|
+
XMLNS
|
30
|
+
end
|
31
|
+
|
32
|
+
# @return [Class]
|
33
|
+
def info_class
|
34
|
+
Info
|
35
|
+
end
|
36
|
+
end
|
37
|
+
end
|
38
|
+
end
|
@@ -0,0 +1,680 @@
|
|
1
|
+
require 'slop/option'
|
2
|
+
require 'slop/commands'
|
3
|
+
|
4
|
+
class Slop
|
5
|
+
include Enumerable
|
6
|
+
|
7
|
+
VERSION = '3.4.5'
|
8
|
+
|
9
|
+
# The main Error class, all Exception classes inherit from this class.
|
10
|
+
class Error < StandardError; end
|
11
|
+
|
12
|
+
# Raised when an option argument is expected but none are given.
|
13
|
+
class MissingArgumentError < Error; end
|
14
|
+
|
15
|
+
# Raised when an option is expected/required but not present.
|
16
|
+
class MissingOptionError < Error; end
|
17
|
+
|
18
|
+
# Raised when an argument does not match its intended match constraint.
|
19
|
+
class InvalidArgumentError < Error; end
|
20
|
+
|
21
|
+
# Raised when an invalid option is found and the strict flag is enabled.
|
22
|
+
class InvalidOptionError < Error; end
|
23
|
+
|
24
|
+
# Raised when an invalid command is found and the strict flag is enabled.
|
25
|
+
class InvalidCommandError < Error; end
|
26
|
+
|
27
|
+
# Returns a default Hash of configuration options this Slop instance uses.
|
28
|
+
DEFAULT_OPTIONS = {
|
29
|
+
:strict => false,
|
30
|
+
:help => false,
|
31
|
+
:banner => nil,
|
32
|
+
:ignore_case => false,
|
33
|
+
:autocreate => false,
|
34
|
+
:arguments => false,
|
35
|
+
:optional_arguments => false,
|
36
|
+
:multiple_switches => true,
|
37
|
+
:longest_flag => 0
|
38
|
+
}
|
39
|
+
|
40
|
+
class << self
|
41
|
+
|
42
|
+
# items - The Array of items to extract options from (default: ARGV).
|
43
|
+
# config - The Hash of configuration options to send to Slop.new().
|
44
|
+
# block - An optional block used to add options.
|
45
|
+
#
|
46
|
+
# Examples:
|
47
|
+
#
|
48
|
+
# Slop.parse(ARGV, :help => true) do
|
49
|
+
# on '-n', '--name', 'Your username', :argument => true
|
50
|
+
# end
|
51
|
+
#
|
52
|
+
# Returns a new instance of Slop.
|
53
|
+
def parse(items = ARGV, config = {}, &block)
|
54
|
+
parse! items.dup, config, &block
|
55
|
+
end
|
56
|
+
|
57
|
+
# items - The Array of items to extract options from (default: ARGV).
|
58
|
+
# config - The Hash of configuration options to send to Slop.new().
|
59
|
+
# block - An optional block used to add options.
|
60
|
+
#
|
61
|
+
# Returns a new instance of Slop.
|
62
|
+
def parse!(items = ARGV, config = {}, &block)
|
63
|
+
config, items = items, ARGV if items.is_a?(Hash) && config.empty?
|
64
|
+
slop = new config, &block
|
65
|
+
slop.parse! items
|
66
|
+
slop
|
67
|
+
end
|
68
|
+
|
69
|
+
# Build a Slop object from a option specification.
|
70
|
+
#
|
71
|
+
# This allows you to design your options via a simple String rather
|
72
|
+
# than programatically. Do note though that with this method, you're
|
73
|
+
# unable to pass any advanced options to the on() method when creating
|
74
|
+
# options.
|
75
|
+
#
|
76
|
+
# string - The optspec String
|
77
|
+
# config - A Hash of configuration options to pass to Slop.new
|
78
|
+
#
|
79
|
+
# Examples:
|
80
|
+
#
|
81
|
+
# opts = Slop.optspec(<<-SPEC)
|
82
|
+
# ruby foo.rb [options]
|
83
|
+
# ---
|
84
|
+
# n,name= Your name
|
85
|
+
# a,age= Your age
|
86
|
+
# A,auth Sign in with auth
|
87
|
+
# p,passcode= Your secret pass code
|
88
|
+
# SPEC
|
89
|
+
#
|
90
|
+
# opts.fetch_option(:name).description #=> "Your name"
|
91
|
+
#
|
92
|
+
# Returns a new instance of Slop.
|
93
|
+
def optspec(string, config = {})
|
94
|
+
warn "[DEPRECATED] `Slop.optspec` is deprecated and will be removed in version 4"
|
95
|
+
config[:banner], optspec = string.split(/^--+$/, 2) if string[/^--+$/]
|
96
|
+
lines = optspec.split("\n").reject(&:empty?)
|
97
|
+
opts = Slop.new(config)
|
98
|
+
|
99
|
+
lines.each do |line|
|
100
|
+
opt, description = line.split(' ', 2)
|
101
|
+
short, long = opt.split(',').map { |s| s.sub(/\A--?/, '') }
|
102
|
+
opt = opts.on(short, long, description)
|
103
|
+
|
104
|
+
if long && long.end_with?('=')
|
105
|
+
long.sub!(/\=$/, '')
|
106
|
+
opt.config[:argument] = true
|
107
|
+
end
|
108
|
+
end
|
109
|
+
|
110
|
+
opts
|
111
|
+
end
|
112
|
+
|
113
|
+
end
|
114
|
+
|
115
|
+
# The Hash of configuration options for this Slop instance.
|
116
|
+
attr_reader :config
|
117
|
+
|
118
|
+
# The Array of Slop::Option objects tied to this Slop instance.
|
119
|
+
attr_reader :options
|
120
|
+
|
121
|
+
# The Hash of sub-commands for this Slop instance.
|
122
|
+
attr_reader :commands
|
123
|
+
|
124
|
+
# Create a new instance of Slop and optionally build options via a block.
|
125
|
+
#
|
126
|
+
# config - A Hash of configuration options.
|
127
|
+
# block - An optional block used to specify options.
|
128
|
+
def initialize(config = {}, &block)
|
129
|
+
@config = DEFAULT_OPTIONS.merge(config)
|
130
|
+
@options = []
|
131
|
+
@commands = {}
|
132
|
+
@trash = []
|
133
|
+
@triggered_options = []
|
134
|
+
@unknown_options = []
|
135
|
+
@callbacks = {}
|
136
|
+
@separators = {}
|
137
|
+
@runner = nil
|
138
|
+
@command = config.delete(:command)
|
139
|
+
|
140
|
+
if block_given?
|
141
|
+
block.arity == 1 ? yield(self) : instance_eval(&block)
|
142
|
+
end
|
143
|
+
|
144
|
+
if config[:help]
|
145
|
+
on('-h', '--help', 'Display this help message.', :tail => true) do
|
146
|
+
puts help
|
147
|
+
exit
|
148
|
+
end
|
149
|
+
end
|
150
|
+
end
|
151
|
+
|
152
|
+
# Is strict mode enabled?
|
153
|
+
#
|
154
|
+
# Returns true if strict mode is enabled, false otherwise.
|
155
|
+
def strict?
|
156
|
+
config[:strict]
|
157
|
+
end
|
158
|
+
|
159
|
+
# Set the banner.
|
160
|
+
#
|
161
|
+
# banner - The String to set the banner.
|
162
|
+
def banner=(banner)
|
163
|
+
config[:banner] = banner
|
164
|
+
end
|
165
|
+
|
166
|
+
# Get or set the banner.
|
167
|
+
#
|
168
|
+
# banner - The String to set the banner.
|
169
|
+
#
|
170
|
+
# Returns the banner String.
|
171
|
+
def banner(banner = nil)
|
172
|
+
config[:banner] = banner if banner
|
173
|
+
config[:banner]
|
174
|
+
end
|
175
|
+
|
176
|
+
# Set the description (used for commands).
|
177
|
+
#
|
178
|
+
# desc - The String to set the description.
|
179
|
+
def description=(desc)
|
180
|
+
config[:description] = desc
|
181
|
+
end
|
182
|
+
|
183
|
+
# Get or set the description (used for commands).
|
184
|
+
#
|
185
|
+
# desc - The String to set the description.
|
186
|
+
#
|
187
|
+
# Returns the description String.
|
188
|
+
def description(desc = nil)
|
189
|
+
config[:description] = desc if desc
|
190
|
+
config[:description]
|
191
|
+
end
|
192
|
+
|
193
|
+
# Add a new command.
|
194
|
+
#
|
195
|
+
# command - The Symbol or String used to identify this command.
|
196
|
+
# options - A Hash of configuration options (see Slop::new)
|
197
|
+
#
|
198
|
+
# Returns a new instance of Slop mapped to this command.
|
199
|
+
def command(command, options = {}, &block)
|
200
|
+
options = @config.merge(options)
|
201
|
+
@commands[command.to_s] = Slop.new(options.merge(:command => command.to_s), &block)
|
202
|
+
end
|
203
|
+
|
204
|
+
# Parse a list of items, executing and gathering options along the way.
|
205
|
+
#
|
206
|
+
# items - The Array of items to extract options from (default: ARGV).
|
207
|
+
# block - An optional block which when used will yield non options.
|
208
|
+
#
|
209
|
+
# Returns an Array of original items.
|
210
|
+
def parse(items = ARGV, &block)
|
211
|
+
parse! items.dup, &block
|
212
|
+
items
|
213
|
+
end
|
214
|
+
|
215
|
+
# Parse a list of items, executing and gathering options along the way.
|
216
|
+
# unlike parse() this method will remove any options and option arguments
|
217
|
+
# from the original Array.
|
218
|
+
#
|
219
|
+
# items - The Array of items to extract options from (default: ARGV).
|
220
|
+
# block - An optional block which when used will yield non options.
|
221
|
+
#
|
222
|
+
# Returns an Array of original items with options removed.
|
223
|
+
def parse!(items = ARGV, &block)
|
224
|
+
if items.empty? && @callbacks[:empty]
|
225
|
+
@callbacks[:empty].each { |cb| cb.call(self) }
|
226
|
+
return items
|
227
|
+
end
|
228
|
+
|
229
|
+
if cmd = @commands[items[0]]
|
230
|
+
items.shift
|
231
|
+
return cmd.parse! items
|
232
|
+
end
|
233
|
+
|
234
|
+
items.each_with_index do |item, index|
|
235
|
+
@trash << index && break if item == '--'
|
236
|
+
autocreate(items, index) if config[:autocreate]
|
237
|
+
process_item(items, index, &block) unless @trash.include?(index)
|
238
|
+
end
|
239
|
+
items.reject!.with_index { |item, index| @trash.include?(index) }
|
240
|
+
|
241
|
+
missing_options = options.select { |opt| opt.required? && opt.count < 1 }
|
242
|
+
if missing_options.any?
|
243
|
+
raise MissingOptionError,
|
244
|
+
"Missing required option(s): #{missing_options.map(&:key).join(', ')}"
|
245
|
+
end
|
246
|
+
|
247
|
+
if @unknown_options.any?
|
248
|
+
raise InvalidOptionError, "Unknown options #{@unknown_options.join(', ')}"
|
249
|
+
end
|
250
|
+
|
251
|
+
if @triggered_options.empty? && @callbacks[:no_options]
|
252
|
+
@callbacks[:no_options].each { |cb| cb.call(self) }
|
253
|
+
end
|
254
|
+
|
255
|
+
if @runner.respond_to?(:call)
|
256
|
+
@runner.call(self, items) unless config[:help] and present?(:help)
|
257
|
+
end
|
258
|
+
|
259
|
+
items
|
260
|
+
end
|
261
|
+
|
262
|
+
# Add an Option.
|
263
|
+
#
|
264
|
+
# objects - An Array with an optional Hash as the last element.
|
265
|
+
#
|
266
|
+
# Examples:
|
267
|
+
#
|
268
|
+
# on '-u', '--username=', 'Your username'
|
269
|
+
# on :v, :verbose, 'Enable verbose mode'
|
270
|
+
#
|
271
|
+
# Returns the created instance of Slop::Option.
|
272
|
+
def on(*objects, &block)
|
273
|
+
option = build_option(objects, &block)
|
274
|
+
original = options.find do |o|
|
275
|
+
o.long and o.long == option.long or o.short and o.short == option.short
|
276
|
+
end
|
277
|
+
options.delete(original) if original
|
278
|
+
options << option
|
279
|
+
option
|
280
|
+
end
|
281
|
+
alias option on
|
282
|
+
alias opt on
|
283
|
+
|
284
|
+
# Fetch an options argument value.
|
285
|
+
#
|
286
|
+
# key - The Symbol or String option short or long flag.
|
287
|
+
#
|
288
|
+
# Returns the Object value for this option, or nil.
|
289
|
+
def [](key)
|
290
|
+
option = fetch_option(key)
|
291
|
+
option.value if option
|
292
|
+
end
|
293
|
+
alias get []
|
294
|
+
|
295
|
+
# Returns a new Hash with option flags as keys and option values as values.
|
296
|
+
#
|
297
|
+
# include_commands - If true, merge options from all sub-commands.
|
298
|
+
def to_hash(include_commands = false)
|
299
|
+
hash = Hash[options.map { |opt| [opt.key.to_sym, opt.value] }]
|
300
|
+
if include_commands
|
301
|
+
@commands.each { |cmd, opts| hash.merge!(cmd.to_sym => opts.to_hash) }
|
302
|
+
end
|
303
|
+
hash
|
304
|
+
end
|
305
|
+
alias to_h to_hash
|
306
|
+
|
307
|
+
# Enumerable interface. Yields each Slop::Option.
|
308
|
+
def each(&block)
|
309
|
+
options.each(&block)
|
310
|
+
end
|
311
|
+
|
312
|
+
# Specify code to be executed when these options are parsed.
|
313
|
+
#
|
314
|
+
# callable - An object responding to a call method.
|
315
|
+
#
|
316
|
+
# yields - The instance of Slop parsing these options
|
317
|
+
# An Array of unparsed arguments
|
318
|
+
#
|
319
|
+
# Example:
|
320
|
+
#
|
321
|
+
# Slop.parse do
|
322
|
+
# on :v, :verbose
|
323
|
+
#
|
324
|
+
# run do |opts, args|
|
325
|
+
# puts "Arguments: #{args.inspect}" if opts.verbose?
|
326
|
+
# end
|
327
|
+
# end
|
328
|
+
def run(callable = nil, &block)
|
329
|
+
@runner = callable || block
|
330
|
+
unless @runner.respond_to?(:call)
|
331
|
+
raise ArgumentError, "You must specify a callable object or a block to #run"
|
332
|
+
end
|
333
|
+
end
|
334
|
+
|
335
|
+
# Check for an options presence.
|
336
|
+
#
|
337
|
+
# Examples:
|
338
|
+
#
|
339
|
+
# opts.parse %w( --foo )
|
340
|
+
# opts.present?(:foo) #=> true
|
341
|
+
# opts.present?(:bar) #=> false
|
342
|
+
#
|
343
|
+
# Returns true if all of the keys are present in the parsed arguments.
|
344
|
+
def present?(*keys)
|
345
|
+
keys.all? { |key| (opt = fetch_option(key)) && opt.count > 0 }
|
346
|
+
end
|
347
|
+
|
348
|
+
# Override this method so we can check if an option? method exists.
|
349
|
+
#
|
350
|
+
# Returns true if this option key exists in our list of options.
|
351
|
+
def respond_to_missing?(method_name, include_private = false)
|
352
|
+
options.any? { |o| o.key == method_name.to_s.chop } || super
|
353
|
+
end
|
354
|
+
|
355
|
+
# Fetch a list of options which were missing from the parsed list.
|
356
|
+
#
|
357
|
+
# Examples:
|
358
|
+
#
|
359
|
+
# opts = Slop.new do
|
360
|
+
# on :n, :name=
|
361
|
+
# on :p, :password=
|
362
|
+
# end
|
363
|
+
#
|
364
|
+
# opts.parse %w[ --name Lee ]
|
365
|
+
# opts.missing #=> ['password']
|
366
|
+
#
|
367
|
+
# Returns an Array of Strings representing missing options.
|
368
|
+
def missing
|
369
|
+
(options - @triggered_options).map(&:key)
|
370
|
+
end
|
371
|
+
|
372
|
+
# Fetch a Slop::Option object.
|
373
|
+
#
|
374
|
+
# key - The Symbol or String option key.
|
375
|
+
#
|
376
|
+
# Examples:
|
377
|
+
#
|
378
|
+
# opts.on(:foo, 'Something fooey', :argument => :optional)
|
379
|
+
# opt = opts.fetch_option(:foo)
|
380
|
+
# opt.class #=> Slop::Option
|
381
|
+
# opt.accepts_optional_argument? #=> true
|
382
|
+
#
|
383
|
+
# Returns an Option or nil if none were found.
|
384
|
+
def fetch_option(key)
|
385
|
+
options.find { |option| [option.long, option.short].include?(clean(key)) }
|
386
|
+
end
|
387
|
+
|
388
|
+
# Fetch a Slop object associated with this command.
|
389
|
+
#
|
390
|
+
# command - The String or Symbol name of the command.
|
391
|
+
#
|
392
|
+
# Examples:
|
393
|
+
#
|
394
|
+
# opts.command :foo do
|
395
|
+
# on :v, :verbose, 'Enable verbose mode'
|
396
|
+
# end
|
397
|
+
#
|
398
|
+
# # ruby run.rb foo -v
|
399
|
+
# opts.fetch_command(:foo).verbose? #=> true
|
400
|
+
def fetch_command(command)
|
401
|
+
@commands[command.to_s]
|
402
|
+
end
|
403
|
+
|
404
|
+
# Add a callback.
|
405
|
+
#
|
406
|
+
# label - The Symbol identifier to attach this callback.
|
407
|
+
#
|
408
|
+
# Returns nothing.
|
409
|
+
def add_callback(label, &block)
|
410
|
+
(@callbacks[label] ||= []) << block
|
411
|
+
end
|
412
|
+
|
413
|
+
# Add string separators between options.
|
414
|
+
#
|
415
|
+
# text - The String text to print.
|
416
|
+
def separator(text)
|
417
|
+
if @separators[options.size]
|
418
|
+
@separators[options.size] << "\n#{text}"
|
419
|
+
else
|
420
|
+
@separators[options.size] = text
|
421
|
+
end
|
422
|
+
end
|
423
|
+
|
424
|
+
# Print a handy Slop help string.
|
425
|
+
#
|
426
|
+
# Returns the banner followed by available option help strings.
|
427
|
+
def to_s
|
428
|
+
heads = options.reject(&:tail?)
|
429
|
+
tails = (options - heads)
|
430
|
+
opts = (heads + tails).select(&:help).map(&:to_s)
|
431
|
+
optstr = opts.each_with_index.map { |o, i|
|
432
|
+
(str = @separators[i + 1]) ? [o, str].join("\n") : o
|
433
|
+
}.join("\n")
|
434
|
+
|
435
|
+
if @commands.any?
|
436
|
+
optstr << "\n" if !optstr.empty?
|
437
|
+
optstr << "\nAvailable commands:\n\n"
|
438
|
+
optstr << commands_to_help
|
439
|
+
optstr << "\n\nSee `<command> --help` for more information on a specific command."
|
440
|
+
end
|
441
|
+
|
442
|
+
banner = config[:banner]
|
443
|
+
if banner.nil?
|
444
|
+
banner = "Usage: #{File.basename($0, '.*')}"
|
445
|
+
banner << " #{@command}" if @command
|
446
|
+
banner << " [command]" if @commands.any?
|
447
|
+
banner << " [options]"
|
448
|
+
end
|
449
|
+
if banner
|
450
|
+
"#{banner}\n#{@separators[0] ? "#{@separators[0]}\n" : ''}#{optstr}"
|
451
|
+
else
|
452
|
+
optstr
|
453
|
+
end
|
454
|
+
end
|
455
|
+
alias help to_s
|
456
|
+
|
457
|
+
private
|
458
|
+
|
459
|
+
# Convenience method for present?(:option).
|
460
|
+
#
|
461
|
+
# Examples:
|
462
|
+
#
|
463
|
+
# opts.parse %( --verbose )
|
464
|
+
# opts.verbose? #=> true
|
465
|
+
# opts.other? #=> false
|
466
|
+
#
|
467
|
+
# Returns true if this option is present. If this method does not end
|
468
|
+
# with a ? character it will instead call super().
|
469
|
+
def method_missing(method, *args, &block)
|
470
|
+
meth = method.to_s
|
471
|
+
if meth.end_with?('?')
|
472
|
+
meth.chop!
|
473
|
+
present?(meth) || present?(meth.gsub('_', '-'))
|
474
|
+
else
|
475
|
+
super
|
476
|
+
end
|
477
|
+
end
|
478
|
+
|
479
|
+
# Process a list item, figure out if it's an option, execute any
|
480
|
+
# callbacks, assign any option arguments, and do some sanity checks.
|
481
|
+
#
|
482
|
+
# items - The Array of items to process.
|
483
|
+
# index - The current Integer index of the item we want to process.
|
484
|
+
# block - An optional block which when passed will yield non options.
|
485
|
+
#
|
486
|
+
# Returns nothing.
|
487
|
+
def process_item(items, index, &block)
|
488
|
+
return unless item = items[index]
|
489
|
+
option, argument = extract_option(item) if item.start_with?('-')
|
490
|
+
|
491
|
+
if option
|
492
|
+
option.count += 1 unless item.start_with?('--no-')
|
493
|
+
option.count += 1 if option.key[0, 3] == "no-"
|
494
|
+
@trash << index
|
495
|
+
@triggered_options << option
|
496
|
+
|
497
|
+
if option.expects_argument?
|
498
|
+
argument ||= items.at(index + 1)
|
499
|
+
|
500
|
+
if !argument || argument =~ /\A--?[a-zA-Z][a-zA-Z0-9_-]*\z/
|
501
|
+
raise MissingArgumentError, "#{option.key} expects an argument"
|
502
|
+
end
|
503
|
+
|
504
|
+
execute_option(option, argument, index, item)
|
505
|
+
elsif option.accepts_optional_argument?
|
506
|
+
argument ||= items.at(index + 1)
|
507
|
+
|
508
|
+
if argument && argument =~ /\A([^\-?]|-\d)+/
|
509
|
+
execute_option(option, argument, index, item)
|
510
|
+
else
|
511
|
+
option.call(nil)
|
512
|
+
end
|
513
|
+
elsif config[:multiple_switches] && argument
|
514
|
+
execute_multiple_switches(option, argument, items, index)
|
515
|
+
else
|
516
|
+
option.value = option.count > 0
|
517
|
+
option.call(nil)
|
518
|
+
end
|
519
|
+
else
|
520
|
+
@unknown_options << item if strict? && item =~ /\A--?/
|
521
|
+
block.call(item) if block && !@trash.include?(index)
|
522
|
+
end
|
523
|
+
end
|
524
|
+
|
525
|
+
# Execute an option, firing off callbacks and assigning arguments.
|
526
|
+
#
|
527
|
+
# option - The Slop::Option object found by #process_item.
|
528
|
+
# argument - The argument Object to assign to this option.
|
529
|
+
# index - The current Integer index of the object we're processing.
|
530
|
+
# item - The optional String item we're processing.
|
531
|
+
#
|
532
|
+
# Returns nothing.
|
533
|
+
def execute_option(option, argument, index, item = nil)
|
534
|
+
if !option
|
535
|
+
if config[:multiple_switches] && strict?
|
536
|
+
raise InvalidOptionError, "Unknown option -#{item}"
|
537
|
+
end
|
538
|
+
return
|
539
|
+
end
|
540
|
+
|
541
|
+
if argument
|
542
|
+
unless item && item.end_with?("=#{argument}")
|
543
|
+
@trash << index + 1 unless option.argument_in_value
|
544
|
+
end
|
545
|
+
option.value = argument
|
546
|
+
else
|
547
|
+
option.value = option.count > 0
|
548
|
+
end
|
549
|
+
|
550
|
+
if option.match? && !argument.match(option.config[:match])
|
551
|
+
raise InvalidArgumentError, "#{argument} is an invalid argument"
|
552
|
+
end
|
553
|
+
|
554
|
+
option.call(option.value)
|
555
|
+
end
|
556
|
+
|
557
|
+
# Execute a `-abc` type option where a, b and c are all options. This
|
558
|
+
# method is only executed if the multiple_switches argument is true.
|
559
|
+
#
|
560
|
+
# option - The first Option object.
|
561
|
+
# argument - The argument to this option. (Split into multiple Options).
|
562
|
+
# items - The Array of items currently being parsed.
|
563
|
+
# index - The index of the current item being processed.
|
564
|
+
#
|
565
|
+
# Returns nothing.
|
566
|
+
def execute_multiple_switches(option, argument, items, index)
|
567
|
+
execute_option(option, nil, index)
|
568
|
+
flags = argument.split('')
|
569
|
+
flags.each do |key|
|
570
|
+
next unless opt = fetch_option(key)
|
571
|
+
opt.count += 1
|
572
|
+
if (opt.expects_argument? || opt.accepts_optional_argument?) &&
|
573
|
+
(flags[-1] == opt.key) && (val = items[index+1])
|
574
|
+
execute_option(opt, val, index, key)
|
575
|
+
else
|
576
|
+
execute_option(opt, nil, index, key)
|
577
|
+
end
|
578
|
+
end
|
579
|
+
end
|
580
|
+
|
581
|
+
# Extract an option from a flag.
|
582
|
+
#
|
583
|
+
# flag - The flag key used to extract an option.
|
584
|
+
#
|
585
|
+
# Returns an Array of [option, argument].
|
586
|
+
def extract_option(flag)
|
587
|
+
option = fetch_option(flag)
|
588
|
+
option ||= fetch_option(flag.downcase) if config[:ignore_case]
|
589
|
+
option ||= fetch_option(flag.gsub(/([^-])-/, '\1_'))
|
590
|
+
|
591
|
+
unless option
|
592
|
+
case flag
|
593
|
+
when /\A--?([^=]+)=(.+)\z/, /\A-([a-zA-Z])(.+)\z/, /\A--no-(.+)\z/
|
594
|
+
option, argument = fetch_option($1), ($2 || false)
|
595
|
+
option.argument_in_value = true if option
|
596
|
+
end
|
597
|
+
end
|
598
|
+
|
599
|
+
[option, argument]
|
600
|
+
end
|
601
|
+
|
602
|
+
# Autocreate an option on the fly. See the :autocreate Slop config option.
|
603
|
+
#
|
604
|
+
# items - The Array of items we're parsing.
|
605
|
+
# index - The current Integer index for the item we're processing.
|
606
|
+
#
|
607
|
+
# Returns nothing.
|
608
|
+
def autocreate(items, index)
|
609
|
+
flag = items[index]
|
610
|
+
if !fetch_option(flag) && !@trash.include?(index)
|
611
|
+
option = build_option(Array(flag))
|
612
|
+
argument = items[index + 1]
|
613
|
+
option.config[:argument] = (argument && argument !~ /\A--?/)
|
614
|
+
option.config[:autocreated] = true
|
615
|
+
options << option
|
616
|
+
end
|
617
|
+
end
|
618
|
+
|
619
|
+
# Build an option from a list of objects.
|
620
|
+
#
|
621
|
+
# objects - An Array of objects used to build this option.
|
622
|
+
#
|
623
|
+
# Returns a new instance of Slop::Option.
|
624
|
+
def build_option(objects, &block)
|
625
|
+
config = {}
|
626
|
+
config[:argument] = true if @config[:arguments]
|
627
|
+
config[:optional_argument] = true if @config[:optional_arguments]
|
628
|
+
|
629
|
+
if objects.last.is_a?(Hash)
|
630
|
+
config.merge!(objects.pop)
|
631
|
+
end
|
632
|
+
|
633
|
+
short = extract_short_flag(objects, config)
|
634
|
+
long = extract_long_flag(objects, config)
|
635
|
+
desc = objects.shift if objects[0].respond_to?(:to_str)
|
636
|
+
|
637
|
+
Option.new(self, short, long, desc, config, &block)
|
638
|
+
end
|
639
|
+
|
640
|
+
def extract_short_flag(objects, config)
|
641
|
+
flag = objects[0].to_s
|
642
|
+
if flag =~ /\A-?\w=?\z/
|
643
|
+
config[:argument] ||= flag.end_with?('=')
|
644
|
+
objects.shift
|
645
|
+
flag.delete('-=')
|
646
|
+
end
|
647
|
+
end
|
648
|
+
|
649
|
+
# Extract the long flag from an item.
|
650
|
+
#
|
651
|
+
# objects - The Array of objects passed from #build_option.
|
652
|
+
# config - The Hash of configuration options built in #build_option.
|
653
|
+
def extract_long_flag(objects, config)
|
654
|
+
flag = objects.first.to_s
|
655
|
+
if flag =~ /\A(?:--?)?[a-zA-Z0-9][a-zA-Z0-9_.-]+\=?\??\z/
|
656
|
+
config[:argument] ||= true if flag.end_with?('=')
|
657
|
+
config[:optional_argument] = true if flag.end_with?('=?')
|
658
|
+
objects.shift
|
659
|
+
clean(flag).sub(/\=\??\z/, '')
|
660
|
+
end
|
661
|
+
end
|
662
|
+
|
663
|
+
# Remove any leading -- characters from a string.
|
664
|
+
#
|
665
|
+
# object - The Object we want to cast to a String and clean.
|
666
|
+
#
|
667
|
+
# Returns the newly cleaned String with leading -- characters removed.
|
668
|
+
def clean(object)
|
669
|
+
object.to_s.sub(/\A--?/, '')
|
670
|
+
end
|
671
|
+
|
672
|
+
def commands_to_help
|
673
|
+
padding = 0
|
674
|
+
@commands.each { |c, _| padding = c.size if c.size > padding }
|
675
|
+
@commands.map do |cmd, opts|
|
676
|
+
" #{cmd}#{' ' * (padding - cmd.size)} #{opts.description}"
|
677
|
+
end.join("\n")
|
678
|
+
end
|
679
|
+
|
680
|
+
end
|