ruby-lint 1.1.0 → 2.0.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/CONTRIBUTING.md +43 -29
- data/LICENSE +1 -1
- data/README.md +0 -1
- data/bin/ruby-lint +91 -3
- data/checksum/ruby-lint-1.1.0.gem.sha512 +1 -0
- data/doc/changelog.md +30 -0
- data/doc/code_analysis.md +13 -0
- data/doc/configuration.md +5 -24
- data/lib/ruby-lint.rb +7 -10
- data/lib/ruby-lint/analysis/argument_amount.rb +2 -0
- data/lib/ruby-lint/analysis/base.rb +12 -0
- data/lib/ruby-lint/analysis/pedantics.rb +2 -0
- data/lib/ruby-lint/analysis/shadowing_variables.rb +2 -0
- data/lib/ruby-lint/analysis/undefined_methods.rb +5 -2
- data/lib/ruby-lint/analysis/undefined_variables.rb +2 -0
- data/lib/ruby-lint/analysis/unused_variables.rb +2 -0
- data/lib/ruby-lint/analysis/useless_equality_checks.rb +2 -0
- data/lib/ruby-lint/ast/node.rb +0 -15
- data/lib/ruby-lint/command.rb +100 -0
- data/lib/ruby-lint/configuration.rb +35 -66
- data/lib/ruby-lint/constant_path.rb +7 -2
- data/lib/ruby-lint/definition/ruby_object.rb +20 -6
- data/lib/ruby-lint/definitions/core/argf.rb +3 -3
- data/lib/ruby-lint/definitions/gems/minitest.rb +792 -0
- data/lib/ruby-lint/file_list.rb +1 -1
- data/lib/ruby-lint/file_loader.rb +2 -20
- data/lib/ruby-lint/file_scanner.rb +11 -2
- data/lib/ruby-lint/method_evaluation.rb +21 -0
- data/lib/ruby-lint/presenter/base.rb +18 -0
- data/lib/ruby-lint/presenter/json.rb +3 -1
- data/lib/ruby-lint/presenter/syntastic.rb +3 -1
- data/lib/ruby-lint/presenter/text.rb +3 -1
- data/lib/ruby-lint/runner.rb +6 -63
- data/lib/ruby-lint/variable_predicates.rb +3 -3
- data/lib/ruby-lint/version.rb +1 -1
- data/lib/ruby-lint/virtual_machine.rb +4 -1
- data/ruby-lint.gemspec +13 -4
- metadata +12 -187
- data/.editorconfig +0 -15
- data/.gitignore +0 -6
- data/.ruby-version +0 -1
- data/.travis.yml +0 -19
- data/Gemfile +0 -15
- data/MANIFEST +0 -469
- data/Rakefile +0 -25
- data/benchmark/bootup.rb +0 -13
- data/benchmark/virtual_machine.rb +0 -17
- data/checksum/.gitkeep +0 -0
- data/doc/.gitkeep +0 -0
- data/doc/css/.gitkeep +0 -0
- data/doc/images/.gitkeep +0 -0
- data/gen/rails/constants.txt +0 -12
- data/gen/rails/requires.rb +0 -2
- data/gen/stdlib/constants.rb +0 -30
- data/gen/stdlib/constants.txt +0 -214
- data/gen/stdlib/requires.rb +0 -64
- data/lib/ruby-lint/benchmark/average.rb +0 -115
- data/lib/ruby-lint/cache.rb +0 -121
- data/lib/ruby-lint/cache_entry.rb +0 -44
- data/lib/ruby-lint/cli.rb +0 -4
- data/lib/ruby-lint/cli/analyze.rb +0 -209
- data/lib/ruby-lint/cli/base.rb +0 -103
- data/lib/ruby-lint/cli/cache.rb +0 -60
- data/lib/ruby-lint/default_names.rb +0 -19
- data/lib/ruby-lint/definitions/gems/.gitkeep +0 -0
- data/lib/ruby-lint/node_hash.rb +0 -105
- data/misc/stdlib.rb +0 -66
- data/profiling/virtual_machine.rb +0 -20
- data/ruby-lint.yml +0 -7
- data/spec/fixtures/associating.rb +0 -7
- data/spec/fixtures/complex/rails/basic_controller.rb +0 -9
- data/spec/fixtures/complex/rcap.rb +0 -67
- data/spec/fixtures/complex/slop.rb +0 -701
- data/spec/fixtures/config.yml +0 -8
- data/spec/fixtures/deeply/nested/file.rb +0 -1
- data/spec/fixtures/file_scanner/lib/example/recursive/source.rb +0 -6
- data/spec/fixtures/file_scanner/lib/example/recursive/target.rb +0 -8
- data/spec/fixtures/file_scanner/lib/example/user.rb +0 -6
- data/spec/fixtures/file_scanner/lib/ruby-lint/definition/constant_proxy.rb +0 -6
- data/spec/fixtures/file_scanner/lib/ruby-lint/global_scope.rb +0 -6
- data/spec/fixtures/file_scanner/lib/test-dashes/foo.rb +0 -4
- data/spec/fixtures/file_scanner/rails/app/models/example/user.rb +0 -6
- data/spec/fixtures/file_scanner/rails/app/models/user.rb +0 -4
- data/spec/fixtures/invalid.rb +0 -3
- data/spec/fixtures/invalid_2.rb +0 -3
- data/spec/fixtures/syntax_error.rb +0 -1
- data/spec/fixtures/uses_external.rb +0 -1
- data/spec/fixtures/uses_external_invalid.rb +0 -3
- data/spec/fixtures/uses_external_namespace.rb +0 -1
- data/spec/fixtures/valid.rb +0 -3
- data/spec/ruby-lint/analysis/argument_amount_spec.rb +0 -131
- data/spec/ruby-lint/analysis/base_spec.rb +0 -24
- data/spec/ruby-lint/analysis/pedantics_spec.rb +0 -63
- data/spec/ruby-lint/analysis/shadowing_variables_spec.rb +0 -85
- data/spec/ruby-lint/analysis/undefined_methods_spec.rb +0 -337
- data/spec/ruby-lint/analysis/undefined_variables_spec.rb +0 -138
- data/spec/ruby-lint/analysis/unused_variables_spec.rb +0 -270
- data/spec/ruby-lint/analysis/useless_equality_checks_spec.rb +0 -107
- data/spec/ruby-lint/ast/node_spec.rb +0 -39
- data/spec/ruby-lint/cache_entry_spec.rb +0 -25
- data/spec/ruby-lint/cache_spec.rb +0 -53
- data/spec/ruby-lint/cli/analyze_spec.rb +0 -52
- data/spec/ruby-lint/configuration_spec.rb +0 -126
- data/spec/ruby-lint/constant_loader_spec.rb +0 -79
- data/spec/ruby-lint/constant_path.rb +0 -63
- data/spec/ruby-lint/definition/constant_proxy_spec.rb +0 -85
- data/spec/ruby-lint/definition/dsl_spec.rb +0 -135
- data/spec/ruby-lint/definition/registry_spec.rb +0 -106
- data/spec/ruby-lint/definition/ruby_method_spec.rb +0 -106
- data/spec/ruby-lint/definition/ruby_object_spec.rb +0 -264
- data/spec/ruby-lint/definition_builder/primitive_spec.rb +0 -62
- data/spec/ruby-lint/definition_builder/ruby_class_spec.rb +0 -107
- data/spec/ruby-lint/definition_builder/ruby_method_spec.rb +0 -53
- data/spec/ruby-lint/definition_builder/ruby_module_spec.rb +0 -52
- data/spec/ruby-lint/definitions/argv_spec.rb +0 -18
- data/spec/ruby-lint/definitions/env_spec.rb +0 -13
- data/spec/ruby-lint/definitions/file_spec.rb +0 -17
- data/spec/ruby-lint/definitions/fixnum_spec.rb +0 -18
- data/spec/ruby-lint/definitions/io_spec.rb +0 -15
- data/spec/ruby-lint/definitions/kernel_spec.rb +0 -18
- data/spec/ruby-lint/definitions/range_spec.rb +0 -21
- data/spec/ruby-lint/definitions/string_spec.rb +0 -20
- data/spec/ruby-lint/docstring/mapping.rb +0 -27
- data/spec/ruby-lint/docstring/parser_spec.rb +0 -88
- data/spec/ruby-lint/extensions/string_spec.rb +0 -7
- data/spec/ruby-lint/file_list_spec.rb +0 -25
- data/spec/ruby-lint/file_loader_spec.rb +0 -69
- data/spec/ruby-lint/file_scanner_spec.rb +0 -84
- data/spec/ruby-lint/inspector_spec.rb +0 -224
- data/spec/ruby-lint/iterator_spec.rb +0 -151
- data/spec/ruby-lint/method_call/.gitkeep +0 -0
- data/spec/ruby-lint/nested_stack_spec.rb +0 -23
- data/spec/ruby-lint/node_hash_spec.rb +0 -56
- data/spec/ruby-lint/parser_spec.rb +0 -38
- data/spec/ruby-lint/presenter/json_spec.rb +0 -58
- data/spec/ruby-lint/presenter/syntastic_spec.rb +0 -49
- data/spec/ruby-lint/presenter/text_spec.rb +0 -49
- data/spec/ruby-lint/report/entry_spec.rb +0 -58
- data/spec/ruby-lint/report_spec.rb +0 -39
- data/spec/ruby-lint/runner_spec.rb +0 -74
- data/spec/ruby-lint/variable_predicates_spec.rb +0 -30
- data/spec/ruby-lint/virtual_machine/alias_spec.rb +0 -55
- data/spec/ruby-lint/virtual_machine/assignments/arrays_spec.rb +0 -87
- data/spec/ruby-lint/virtual_machine/assignments/assignment_arguments_spec.rb +0 -14
- data/spec/ruby-lint/virtual_machine/assignments/constants_spec.rb +0 -23
- data/spec/ruby-lint/virtual_machine/assignments/hashes_spec.rb +0 -52
- data/spec/ruby-lint/virtual_machine/assignments/optional_spec.rb +0 -41
- data/spec/ruby-lint/virtual_machine/assignments/range_spec.rb +0 -28
- data/spec/ruby-lint/virtual_machine/assignments/return_values_spec.rb +0 -78
- data/spec/ruby-lint/virtual_machine/assignments/variables_spec.rb +0 -130
- data/spec/ruby-lint/virtual_machine/associate_nodes_spec.rb +0 -61
- data/spec/ruby-lint/virtual_machine/autoloading_spec.rb +0 -18
- data/spec/ruby-lint/virtual_machine/blocks_spec.rb +0 -90
- data/spec/ruby-lint/virtual_machine/classes/class_methods_spec.rb +0 -21
- data/spec/ruby-lint/virtual_machine/classes/extending_spec.rb +0 -109
- data/spec/ruby-lint/virtual_machine/classes/redefining_spec.rb +0 -51
- data/spec/ruby-lint/virtual_machine/classes/sclass_spec.rb +0 -96
- data/spec/ruby-lint/virtual_machine/classes/scoping_spec.rb +0 -60
- data/spec/ruby-lint/virtual_machine/complex/rails_spec.rb +0 -21
- data/spec/ruby-lint/virtual_machine/complex/rcap_spec.rb +0 -15
- data/spec/ruby-lint/virtual_machine/complex/slop_spec.rb +0 -16
- data/spec/ruby-lint/virtual_machine/constants_spec.rb +0 -31
- data/spec/ruby-lint/virtual_machine/for_spec.rb +0 -16
- data/spec/ruby-lint/virtual_machine/freeze_spec.rb +0 -13
- data/spec/ruby-lint/virtual_machine/global_variables_spec.rb +0 -15
- data/spec/ruby-lint/virtual_machine/inherit_kernel_spec.rb +0 -15
- data/spec/ruby-lint/virtual_machine/interpolation_spec.rb +0 -12
- data/spec/ruby-lint/virtual_machine/location_spec.rb +0 -64
- data/spec/ruby-lint/virtual_machine/method_call_tracking_spec.rb +0 -57
- data/spec/ruby-lint/virtual_machine/methods/attr_spec.rb +0 -182
- data/spec/ruby-lint/virtual_machine/methods/calls_spec.rb +0 -25
- data/spec/ruby-lint/virtual_machine/methods/define_method_spec.rb +0 -41
- data/spec/ruby-lint/virtual_machine/methods/defining_spec.rb +0 -40
- data/spec/ruby-lint/virtual_machine/methods/docstrings_spec.rb +0 -80
- data/spec/ruby-lint/virtual_machine/methods/exporting_spec.rb +0 -19
- data/spec/ruby-lint/virtual_machine/methods/kernel_spec.rb +0 -25
- data/spec/ruby-lint/virtual_machine/methods/parameters_spec.rb +0 -89
- data/spec/ruby-lint/virtual_machine/methods/patching_spec.rb +0 -20
- data/spec/ruby-lint/virtual_machine/methods/scoping_spec.rb +0 -63
- data/spec/ruby-lint/virtual_machine/methods/square_bracket_spec.rb +0 -32
- data/spec/ruby-lint/virtual_machine/methods/visibility_spec.rb +0 -64
- data/spec/ruby-lint/virtual_machine/modules_spec.rb +0 -205
- data/spec/ruby-lint/virtual_machine/reference_amount_spec.rb +0 -81
- data/spec/ruby-lint/virtual_machine/self_spec.rb +0 -37
- data/spec/ruby-lint/virtual_machine/unused_spec.rb +0 -17
- data/spec/spec_helper.rb +0 -28
- data/spec/support/building.rb +0 -50
- data/spec/support/coveralls.rb +0 -5
- data/spec/support/definitions.rb +0 -39
- data/spec/support/fixtures.rb +0 -20
- data/spec/support/parsing.rb +0 -30
- data/spec/support/simplecov.rb +0 -18
- data/spec/support/versions.rb +0 -9
- data/task/build.rake +0 -4
- data/task/checksum.rake +0 -13
- data/task/coverage.rake +0 -6
- data/task/doc.rake +0 -16
- data/task/generate.rake +0 -77
- data/task/graphviz.rake +0 -12
- data/task/manifest.rake +0 -8
- data/task/tag.rake +0 -6
- data/task/test.rake +0 -4
- data/task/todo.rake +0 -6
- data/task/travis.rake +0 -8
data/lib/ruby-lint/cli/cache.rb
DELETED
@@ -1,60 +0,0 @@
|
|
1
|
-
RubyLint::CLI.options.command :cache do
|
2
|
-
banner 'Usage: ruby-lint cache [COMMAND] [OPTIONS]'
|
3
|
-
description 'Manages ruby-lint cache files'
|
4
|
-
|
5
|
-
separator <<-EOF.chomp
|
6
|
-
|
7
|
-
About:
|
8
|
-
|
9
|
-
This command can be used to view and manage cache entries created by
|
10
|
-
ruby-lint. This command is mainly meant to be used for debugging purposes.
|
11
|
-
|
12
|
-
Examples:
|
13
|
-
|
14
|
-
Print a summary of the cache:
|
15
|
-
|
16
|
-
$ ruby-lint cache
|
17
|
-
|
18
|
-
Clear the cache:
|
19
|
-
|
20
|
-
$ ruby-lint cache clear
|
21
|
-
EOF
|
22
|
-
|
23
|
-
separator RubyLint::CLI::OPTIONS_HEADER
|
24
|
-
|
25
|
-
RubyLint::CLI.help_option(self)
|
26
|
-
|
27
|
-
run do
|
28
|
-
config = RubyLint::Configuration.load_from_file
|
29
|
-
pattern = File.join(config.cache_directory, '*.cache')
|
30
|
-
files = Dir.glob(pattern)
|
31
|
-
size = files.map { |file| File.size(file) }.inject(:+).to_f
|
32
|
-
|
33
|
-
size_kb = (size / 1024).round(2)
|
34
|
-
size_mb = (size / 1024 / 1024).round(2)
|
35
|
-
|
36
|
-
puts <<-EOF.strip
|
37
|
-
Directory: #{config.cache_directory}
|
38
|
-
Total files: #{files.length}
|
39
|
-
Total size: #{size_kb} KB (#{size_mb} MB)
|
40
|
-
EOF
|
41
|
-
end
|
42
|
-
|
43
|
-
command :clear do
|
44
|
-
banner 'Usage: ruby-lint cache clear [OPTIONS]'
|
45
|
-
description 'Removes all the cache files'
|
46
|
-
|
47
|
-
separator RubyLint::CLI::OPTIONS_HEADER
|
48
|
-
|
49
|
-
RubyLint::CLI.help_option(self)
|
50
|
-
|
51
|
-
run do
|
52
|
-
config = RubyLint::Configuration.load_from_file
|
53
|
-
pattern = File.join(config.cache_directory, '*.cache')
|
54
|
-
|
55
|
-
Dir.glob(pattern).each do |file|
|
56
|
-
File.unlink(file)
|
57
|
-
end
|
58
|
-
end
|
59
|
-
end
|
60
|
-
end
|
@@ -1,19 +0,0 @@
|
|
1
|
-
RubyLint::Configuration.register_names 'analysis' do |names|
|
2
|
-
RubyLint::Analysis.constants.each do |const|
|
3
|
-
next if const == :Base
|
4
|
-
|
5
|
-
names[const.to_s.snake_case] = RubyLint::Analysis.const_get(const)
|
6
|
-
end
|
7
|
-
end
|
8
|
-
|
9
|
-
RubyLint::Configuration.register_names 'presenters' do |names|
|
10
|
-
RubyLint::Presenter.constants.each do |const|
|
11
|
-
names[const.to_s.snake_case] = RubyLint::Presenter.const_get(const)
|
12
|
-
end
|
13
|
-
end
|
14
|
-
|
15
|
-
RubyLint::Configuration.register_names 'levels' do |names|
|
16
|
-
RubyLint::Report::DEFAULT_LEVELS.each do |level|
|
17
|
-
names[level.to_s] = level
|
18
|
-
end
|
19
|
-
end
|
File without changes
|
data/lib/ruby-lint/node_hash.rb
DELETED
@@ -1,105 +0,0 @@
|
|
1
|
-
module RubyLint
|
2
|
-
##
|
3
|
-
# The NodeHash class is used to store information about {RubyLint::AST::Node}
|
4
|
-
# instances based on their SHA1 hashes. It's primarily used by
|
5
|
-
# {RubyLint::Runner} to store nodes and their associated comments.
|
6
|
-
#
|
7
|
-
# @!attribute [r] hash
|
8
|
-
# @return [Hash]
|
9
|
-
#
|
10
|
-
class NodeHash
|
11
|
-
attr_reader :hash
|
12
|
-
|
13
|
-
##
|
14
|
-
# Creates a new instance from a regular Hash.
|
15
|
-
#
|
16
|
-
# @param [Hash] hash
|
17
|
-
# @return [RubyLint::NodeHash]
|
18
|
-
#
|
19
|
-
def self.from_hash(hash)
|
20
|
-
converted = {}
|
21
|
-
|
22
|
-
hash.each do |key, value|
|
23
|
-
converted[key.sha1] = value
|
24
|
-
end
|
25
|
-
|
26
|
-
return new(converted)
|
27
|
-
end
|
28
|
-
|
29
|
-
##
|
30
|
-
# @param [Hash] hash
|
31
|
-
#
|
32
|
-
def initialize(hash = {})
|
33
|
-
@hash = hash
|
34
|
-
end
|
35
|
-
|
36
|
-
##
|
37
|
-
# Returns the value associated with the given node.
|
38
|
-
#
|
39
|
-
# @param [RubyLint::AST::Node] node
|
40
|
-
# @return [Mixed]
|
41
|
-
#
|
42
|
-
def [](node)
|
43
|
-
return @hash[node.sha1]
|
44
|
-
end
|
45
|
-
|
46
|
-
##
|
47
|
-
# Checks if there's a value stored for the node.
|
48
|
-
#
|
49
|
-
# @param [RubyLint::AST::Node] node
|
50
|
-
# @return [TrueClass|FalseClass]
|
51
|
-
#
|
52
|
-
def key?(node)
|
53
|
-
return @hash.key?(node.sha1)
|
54
|
-
end
|
55
|
-
|
56
|
-
##
|
57
|
-
# Sets a value for the given node.
|
58
|
-
#
|
59
|
-
# @param [RubyLint::AST::Node] node
|
60
|
-
# @param [Mixed] value
|
61
|
-
#
|
62
|
-
def []=(node, value)
|
63
|
-
return @hash[node.sha1] = value
|
64
|
-
end
|
65
|
-
|
66
|
-
##
|
67
|
-
# @return [Array]
|
68
|
-
#
|
69
|
-
def keys
|
70
|
-
return @hash.keys
|
71
|
-
end
|
72
|
-
|
73
|
-
##
|
74
|
-
# @return [Array]
|
75
|
-
#
|
76
|
-
def values
|
77
|
-
return @hash.values
|
78
|
-
end
|
79
|
-
|
80
|
-
##
|
81
|
-
# @return [Array]
|
82
|
-
#
|
83
|
-
def to_a
|
84
|
-
return @hash.to_a
|
85
|
-
end
|
86
|
-
|
87
|
-
##
|
88
|
-
# Merges the values of another NodeHash into the current one.
|
89
|
-
#
|
90
|
-
# @param [RubyLint::NodeHash] other
|
91
|
-
#
|
92
|
-
def merge!(other)
|
93
|
-
@hash.merge!(other.hash)
|
94
|
-
end
|
95
|
-
|
96
|
-
##
|
97
|
-
# Merges a regular Hash into the current NodeHash.
|
98
|
-
#
|
99
|
-
# @param [Hash] hash
|
100
|
-
#
|
101
|
-
def merge_hash!(hash)
|
102
|
-
merge!(self.class.from_hash(hash))
|
103
|
-
end
|
104
|
-
end # NodeHash
|
105
|
-
end # RubyLint
|
data/misc/stdlib.rb
DELETED
@@ -1,66 +0,0 @@
|
|
1
|
-
require 'abbrev'
|
2
|
-
require 'base64'
|
3
|
-
require 'benchmark'
|
4
|
-
require 'cgi'
|
5
|
-
require 'cmath'
|
6
|
-
require 'complex'
|
7
|
-
require 'csv'
|
8
|
-
require 'date'
|
9
|
-
require 'delegate'
|
10
|
-
require 'digest'
|
11
|
-
require 'drb'
|
12
|
-
require 'e2mmap'
|
13
|
-
require 'English'
|
14
|
-
require 'erb'
|
15
|
-
require 'expect'
|
16
|
-
require 'fileutils'
|
17
|
-
require 'find'
|
18
|
-
require 'forwardable'
|
19
|
-
require 'getoptlong'
|
20
|
-
require 'gserver'
|
21
|
-
require 'ipaddr'
|
22
|
-
require 'irb'
|
23
|
-
require 'json'
|
24
|
-
require 'kconv'
|
25
|
-
require 'logger'
|
26
|
-
require 'mathn'
|
27
|
-
require 'matrix'
|
28
|
-
require 'mkmf'
|
29
|
-
require 'monitor'
|
30
|
-
require 'mutex_m'
|
31
|
-
require 'observer'
|
32
|
-
require 'open3'
|
33
|
-
require 'openssl'
|
34
|
-
require 'open-uri'
|
35
|
-
require 'optparse'
|
36
|
-
require 'ostruct'
|
37
|
-
require 'pathname'
|
38
|
-
require 'pp'
|
39
|
-
require 'prettyprint'
|
40
|
-
require 'prime'
|
41
|
-
require 'profiler'
|
42
|
-
require 'pstore'
|
43
|
-
require 'psych'
|
44
|
-
require 'rational'
|
45
|
-
require 'rdoc'
|
46
|
-
require 'resolv'
|
47
|
-
require 'rss'
|
48
|
-
require 'scanf'
|
49
|
-
require 'securerandom'
|
50
|
-
require 'set'
|
51
|
-
require 'shellwords'
|
52
|
-
require 'singleton'
|
53
|
-
require 'socket'
|
54
|
-
require 'stringio'
|
55
|
-
require 'tempfile'
|
56
|
-
require 'thread'
|
57
|
-
require 'thwait'
|
58
|
-
require 'timeout'
|
59
|
-
require 'time'
|
60
|
-
require 'tmpdir'
|
61
|
-
require 'tsort'
|
62
|
-
require 'uri'
|
63
|
-
require 'weakref'
|
64
|
-
require 'webrick'
|
65
|
-
require 'xmlrpc'
|
66
|
-
require 'yaml'
|
@@ -1,20 +0,0 @@
|
|
1
|
-
require 'ruby-prof'
|
2
|
-
|
3
|
-
require_relative '../lib/ruby-lint'
|
4
|
-
|
5
|
-
parser = RubyLint::Parser.new
|
6
|
-
file = File.expand_path('../../lib/ruby-lint/virtual_machine.rb', __FILE__)
|
7
|
-
ast = parser.parse(File.read(file), file)
|
8
|
-
amount = 1000
|
9
|
-
|
10
|
-
result = RubyProf.profile do
|
11
|
-
amount.times do
|
12
|
-
RubyLint::VirtualMachine.new.run(ast)
|
13
|
-
end
|
14
|
-
end
|
15
|
-
|
16
|
-
result.eliminate_methods!([/Integer#times/])
|
17
|
-
|
18
|
-
printer = RubyProf::GraphHtmlPrinter.new(result)
|
19
|
-
|
20
|
-
printer.print(STDOUT, :sort_method => :self_time)
|
data/ruby-lint.yml
DELETED
@@ -1,67 +0,0 @@
|
|
1
|
-
# RCAP is released under the BSD License.
|
2
|
-
#
|
3
|
-
# Copyright 2010 - 2013 AIMRED CC. All rights reserved.
|
4
|
-
#
|
5
|
-
# Redistribution and use in source and binary forms, with or without
|
6
|
-
# modification, are permitted provided that the following conditions are met:
|
7
|
-
#
|
8
|
-
# 1. Redistributions of source code must retain the above copyright notice,
|
9
|
-
# this list of conditions and the following disclaimer.
|
10
|
-
#
|
11
|
-
# 2. Redistributions in binary form must reproduce the above copyright notice,
|
12
|
-
# this list of conditions and the following disclaimer in the documentation
|
13
|
-
# and/or other materials provided with the distribution.
|
14
|
-
#
|
15
|
-
# THIS SOFTWARE IS PROVIDED BY AIMRED CC ``AS IS'' AND ANY EXPRESS OR IMPLIED
|
16
|
-
# WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
|
17
|
-
# MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
|
18
|
-
# EVENT SHALL AIMRED CC OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
|
19
|
-
# INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
|
20
|
-
# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
|
21
|
-
# OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
|
22
|
-
# LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
|
23
|
-
# NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
|
24
|
-
# EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
25
|
-
#
|
26
|
-
# The views and conclusions contained in the software and documentation are
|
27
|
-
# those of the authors and should not be interpreted as representing official
|
28
|
-
# policies, either expressed or implied, of AIMRED CC.
|
29
|
-
|
30
|
-
module RCAP
|
31
|
-
# The VM will raise an error if this class doesn't exist.
|
32
|
-
module Base
|
33
|
-
class Alert
|
34
|
-
end
|
35
|
-
end
|
36
|
-
|
37
|
-
module CAP_1_2
|
38
|
-
|
39
|
-
# An Alert object is valid if
|
40
|
-
# * it has an identifier
|
41
|
-
# * it has a sender
|
42
|
-
# * it has a sent time
|
43
|
-
# * it has a valid status value
|
44
|
-
# * it has a valid messge type value
|
45
|
-
# * it has a valid scope value
|
46
|
-
# * all Info objects contained in infos are valid
|
47
|
-
class Alert < RCAP::Base::Alert
|
48
|
-
|
49
|
-
XMLNS = "urn:oasis:names:tc:emergency:cap:1.2"
|
50
|
-
CAP_VERSION = "1.2"
|
51
|
-
|
52
|
-
STATUS_DRAFT = "Draft"
|
53
|
-
# Valid values for status
|
54
|
-
VALID_STATUSES = [ STATUS_ACTUAL, STATUS_EXERCISE, STATUS_SYSTEM, STATUS_TEST, STATUS_DRAFT ]
|
55
|
-
|
56
|
-
# @return [String]
|
57
|
-
def xmlns
|
58
|
-
XMLNS
|
59
|
-
end
|
60
|
-
|
61
|
-
# @return [Class]
|
62
|
-
def info_class
|
63
|
-
Info
|
64
|
-
end
|
65
|
-
end
|
66
|
-
end
|
67
|
-
end
|
@@ -1,701 +0,0 @@
|
|
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.
|
21
|
-
|
22
|
-
require 'slop/option'
|
23
|
-
require 'slop/commands'
|
24
|
-
|
25
|
-
class Slop
|
26
|
-
include Enumerable
|
27
|
-
|
28
|
-
VERSION = '3.4.5'
|
29
|
-
|
30
|
-
# The main Error class, all Exception classes inherit from this class.
|
31
|
-
class Error < StandardError; end
|
32
|
-
|
33
|
-
# Raised when an option argument is expected but none are given.
|
34
|
-
class MissingArgumentError < Error; end
|
35
|
-
|
36
|
-
# Raised when an option is expected/required but not present.
|
37
|
-
class MissingOptionError < Error; end
|
38
|
-
|
39
|
-
# Raised when an argument does not match its intended match constraint.
|
40
|
-
class InvalidArgumentError < Error; end
|
41
|
-
|
42
|
-
# Raised when an invalid option is found and the strict flag is enabled.
|
43
|
-
class InvalidOptionError < Error; end
|
44
|
-
|
45
|
-
# Raised when an invalid command is found and the strict flag is enabled.
|
46
|
-
class InvalidCommandError < Error; end
|
47
|
-
|
48
|
-
# Returns a default Hash of configuration options this Slop instance uses.
|
49
|
-
DEFAULT_OPTIONS = {
|
50
|
-
:strict => false,
|
51
|
-
:help => false,
|
52
|
-
:banner => nil,
|
53
|
-
:ignore_case => false,
|
54
|
-
:autocreate => false,
|
55
|
-
:arguments => false,
|
56
|
-
:optional_arguments => false,
|
57
|
-
:multiple_switches => true,
|
58
|
-
:longest_flag => 0
|
59
|
-
}
|
60
|
-
|
61
|
-
class << self
|
62
|
-
|
63
|
-
# items - The Array of items to extract options from (default: ARGV).
|
64
|
-
# config - The Hash of configuration options to send to Slop.new().
|
65
|
-
# block - An optional block used to add options.
|
66
|
-
#
|
67
|
-
# Examples:
|
68
|
-
#
|
69
|
-
# Slop.parse(ARGV, :help => true) do
|
70
|
-
# on '-n', '--name', 'Your username', :argument => true
|
71
|
-
# end
|
72
|
-
#
|
73
|
-
# Returns a new instance of Slop.
|
74
|
-
def parse(items = ARGV, config = {}, &block)
|
75
|
-
parse! items.dup, config, &block
|
76
|
-
end
|
77
|
-
|
78
|
-
# items - The Array of items to extract options from (default: ARGV).
|
79
|
-
# config - The Hash of configuration options to send to Slop.new().
|
80
|
-
# block - An optional block used to add options.
|
81
|
-
#
|
82
|
-
# Returns a new instance of Slop.
|
83
|
-
def parse!(items = ARGV, config = {}, &block)
|
84
|
-
config, items = items, ARGV if items.is_a?(Hash) && config.empty?
|
85
|
-
slop = new config, &block
|
86
|
-
slop.parse! items
|
87
|
-
slop
|
88
|
-
end
|
89
|
-
|
90
|
-
# Build a Slop object from a option specification.
|
91
|
-
#
|
92
|
-
# This allows you to design your options via a simple String rather
|
93
|
-
# than programatically. Do note though that with this method, you're
|
94
|
-
# unable to pass any advanced options to the on() method when creating
|
95
|
-
# options.
|
96
|
-
#
|
97
|
-
# string - The optspec String
|
98
|
-
# config - A Hash of configuration options to pass to Slop.new
|
99
|
-
#
|
100
|
-
# Examples:
|
101
|
-
#
|
102
|
-
# opts = Slop.optspec(<<-SPEC)
|
103
|
-
# ruby foo.rb [options]
|
104
|
-
# ---
|
105
|
-
# n,name= Your name
|
106
|
-
# a,age= Your age
|
107
|
-
# A,auth Sign in with auth
|
108
|
-
# p,passcode= Your secret pass code
|
109
|
-
# SPEC
|
110
|
-
#
|
111
|
-
# opts.fetch_option(:name).description #=> "Your name"
|
112
|
-
#
|
113
|
-
# Returns a new instance of Slop.
|
114
|
-
def optspec(string, config = {})
|
115
|
-
warn "[DEPRECATED] `Slop.optspec` is deprecated and will be removed in version 4"
|
116
|
-
config[:banner], optspec = string.split(/^--+$/, 2) if string[/^--+$/]
|
117
|
-
lines = optspec.split("\n").reject(&:empty?)
|
118
|
-
opts = Slop.new(config)
|
119
|
-
|
120
|
-
lines.each do |line|
|
121
|
-
opt, description = line.split(' ', 2)
|
122
|
-
short, long = opt.split(',').map { |s| s.sub(/\A--?/, '') }
|
123
|
-
opt = opts.on(short, long, description)
|
124
|
-
|
125
|
-
if long && long.end_with?('=')
|
126
|
-
long.sub!(/\=$/, '')
|
127
|
-
opt.config[:argument] = true
|
128
|
-
end
|
129
|
-
end
|
130
|
-
|
131
|
-
opts
|
132
|
-
end
|
133
|
-
|
134
|
-
end
|
135
|
-
|
136
|
-
# The Hash of configuration options for this Slop instance.
|
137
|
-
attr_reader :config
|
138
|
-
|
139
|
-
# The Array of Slop::Option objects tied to this Slop instance.
|
140
|
-
attr_reader :options
|
141
|
-
|
142
|
-
# The Hash of sub-commands for this Slop instance.
|
143
|
-
attr_reader :commands
|
144
|
-
|
145
|
-
# Create a new instance of Slop and optionally build options via a block.
|
146
|
-
#
|
147
|
-
# config - A Hash of configuration options.
|
148
|
-
# block - An optional block used to specify options.
|
149
|
-
def initialize(config = {}, &block)
|
150
|
-
@config = DEFAULT_OPTIONS.merge(config)
|
151
|
-
@options = []
|
152
|
-
@commands = {}
|
153
|
-
@trash = []
|
154
|
-
@triggered_options = []
|
155
|
-
@unknown_options = []
|
156
|
-
@callbacks = {}
|
157
|
-
@separators = {}
|
158
|
-
@runner = nil
|
159
|
-
@command = config.delete(:command)
|
160
|
-
|
161
|
-
if block_given?
|
162
|
-
block.arity == 1 ? yield(self) : instance_eval(&block)
|
163
|
-
end
|
164
|
-
|
165
|
-
if config[:help]
|
166
|
-
on('-h', '--help', 'Display this help message.', :tail => true) do
|
167
|
-
puts help
|
168
|
-
exit
|
169
|
-
end
|
170
|
-
end
|
171
|
-
end
|
172
|
-
|
173
|
-
# Is strict mode enabled?
|
174
|
-
#
|
175
|
-
# Returns true if strict mode is enabled, false otherwise.
|
176
|
-
def strict?
|
177
|
-
config[:strict]
|
178
|
-
end
|
179
|
-
|
180
|
-
# Set the banner.
|
181
|
-
#
|
182
|
-
# banner - The String to set the banner.
|
183
|
-
def banner=(banner)
|
184
|
-
config[:banner] = banner
|
185
|
-
end
|
186
|
-
|
187
|
-
# Get or set the banner.
|
188
|
-
#
|
189
|
-
# banner - The String to set the banner.
|
190
|
-
#
|
191
|
-
# Returns the banner String.
|
192
|
-
def banner(banner = nil)
|
193
|
-
config[:banner] = banner if banner
|
194
|
-
config[:banner]
|
195
|
-
end
|
196
|
-
|
197
|
-
# Set the description (used for commands).
|
198
|
-
#
|
199
|
-
# desc - The String to set the description.
|
200
|
-
def description=(desc)
|
201
|
-
config[:description] = desc
|
202
|
-
end
|
203
|
-
|
204
|
-
# Get or set the description (used for commands).
|
205
|
-
#
|
206
|
-
# desc - The String to set the description.
|
207
|
-
#
|
208
|
-
# Returns the description String.
|
209
|
-
def description(desc = nil)
|
210
|
-
config[:description] = desc if desc
|
211
|
-
config[:description]
|
212
|
-
end
|
213
|
-
|
214
|
-
# Add a new command.
|
215
|
-
#
|
216
|
-
# command - The Symbol or String used to identify this command.
|
217
|
-
# options - A Hash of configuration options (see Slop::new)
|
218
|
-
#
|
219
|
-
# Returns a new instance of Slop mapped to this command.
|
220
|
-
def command(command, options = {}, &block)
|
221
|
-
options = @config.merge(options)
|
222
|
-
@commands[command.to_s] = Slop.new(options.merge(:command => command.to_s), &block)
|
223
|
-
end
|
224
|
-
|
225
|
-
# Parse a list of items, executing and gathering options along the way.
|
226
|
-
#
|
227
|
-
# items - The Array of items to extract options from (default: ARGV).
|
228
|
-
# block - An optional block which when used will yield non options.
|
229
|
-
#
|
230
|
-
# Returns an Array of original items.
|
231
|
-
def parse(items = ARGV, &block)
|
232
|
-
parse! items.dup, &block
|
233
|
-
items
|
234
|
-
end
|
235
|
-
|
236
|
-
# Parse a list of items, executing and gathering options along the way.
|
237
|
-
# unlike parse() this method will remove any options and option arguments
|
238
|
-
# from the original Array.
|
239
|
-
#
|
240
|
-
# items - The Array of items to extract options from (default: ARGV).
|
241
|
-
# block - An optional block which when used will yield non options.
|
242
|
-
#
|
243
|
-
# Returns an Array of original items with options removed.
|
244
|
-
def parse!(items = ARGV, &block)
|
245
|
-
if items.empty? && @callbacks[:empty]
|
246
|
-
@callbacks[:empty].each { |cb| cb.call(self) }
|
247
|
-
return items
|
248
|
-
end
|
249
|
-
|
250
|
-
if cmd = @commands[items[0]]
|
251
|
-
items.shift
|
252
|
-
return cmd.parse! items
|
253
|
-
end
|
254
|
-
|
255
|
-
items.each_with_index do |item, index|
|
256
|
-
@trash << index && break if item == '--'
|
257
|
-
autocreate(items, index) if config[:autocreate]
|
258
|
-
process_item(items, index, &block) unless @trash.include?(index)
|
259
|
-
end
|
260
|
-
items.reject!.with_index { |item, index| @trash.include?(index) }
|
261
|
-
|
262
|
-
missing_options = options.select { |opt| opt.required? && opt.count < 1 }
|
263
|
-
if missing_options.any?
|
264
|
-
raise MissingOptionError,
|
265
|
-
"Missing required option(s): #{missing_options.map(&:key).join(', ')}"
|
266
|
-
end
|
267
|
-
|
268
|
-
if @unknown_options.any?
|
269
|
-
raise InvalidOptionError, "Unknown options #{@unknown_options.join(', ')}"
|
270
|
-
end
|
271
|
-
|
272
|
-
if @triggered_options.empty? && @callbacks[:no_options]
|
273
|
-
@callbacks[:no_options].each { |cb| cb.call(self) }
|
274
|
-
end
|
275
|
-
|
276
|
-
if @runner.respond_to?(:call)
|
277
|
-
@runner.call(self, items) unless config[:help] and present?(:help)
|
278
|
-
end
|
279
|
-
|
280
|
-
items
|
281
|
-
end
|
282
|
-
|
283
|
-
# Add an Option.
|
284
|
-
#
|
285
|
-
# objects - An Array with an optional Hash as the last element.
|
286
|
-
#
|
287
|
-
# Examples:
|
288
|
-
#
|
289
|
-
# on '-u', '--username=', 'Your username'
|
290
|
-
# on :v, :verbose, 'Enable verbose mode'
|
291
|
-
#
|
292
|
-
# Returns the created instance of Slop::Option.
|
293
|
-
def on(*objects, &block)
|
294
|
-
option = build_option(objects, &block)
|
295
|
-
original = options.find do |o|
|
296
|
-
o.long and o.long == option.long or o.short and o.short == option.short
|
297
|
-
end
|
298
|
-
options.delete(original) if original
|
299
|
-
options << option
|
300
|
-
option
|
301
|
-
end
|
302
|
-
alias option on
|
303
|
-
alias opt on
|
304
|
-
|
305
|
-
# Fetch an options argument value.
|
306
|
-
#
|
307
|
-
# key - The Symbol or String option short or long flag.
|
308
|
-
#
|
309
|
-
# Returns the Object value for this option, or nil.
|
310
|
-
def [](key)
|
311
|
-
option = fetch_option(key)
|
312
|
-
option.value if option
|
313
|
-
end
|
314
|
-
alias get []
|
315
|
-
|
316
|
-
# Returns a new Hash with option flags as keys and option values as values.
|
317
|
-
#
|
318
|
-
# include_commands - If true, merge options from all sub-commands.
|
319
|
-
def to_hash(include_commands = false)
|
320
|
-
hash = Hash[options.map { |opt| [opt.key.to_sym, opt.value] }]
|
321
|
-
if include_commands
|
322
|
-
@commands.each { |cmd, opts| hash.merge!(cmd.to_sym => opts.to_hash) }
|
323
|
-
end
|
324
|
-
hash
|
325
|
-
end
|
326
|
-
alias to_h to_hash
|
327
|
-
|
328
|
-
# Enumerable interface. Yields each Slop::Option.
|
329
|
-
def each(&block)
|
330
|
-
options.each(&block)
|
331
|
-
end
|
332
|
-
|
333
|
-
# Specify code to be executed when these options are parsed.
|
334
|
-
#
|
335
|
-
# callable - An object responding to a call method.
|
336
|
-
#
|
337
|
-
# yields - The instance of Slop parsing these options
|
338
|
-
# An Array of unparsed arguments
|
339
|
-
#
|
340
|
-
# Example:
|
341
|
-
#
|
342
|
-
# Slop.parse do
|
343
|
-
# on :v, :verbose
|
344
|
-
#
|
345
|
-
# run do |opts, args|
|
346
|
-
# puts "Arguments: #{args.inspect}" if opts.verbose?
|
347
|
-
# end
|
348
|
-
# end
|
349
|
-
def run(callable = nil, &block)
|
350
|
-
@runner = callable || block
|
351
|
-
unless @runner.respond_to?(:call)
|
352
|
-
raise ArgumentError, "You must specify a callable object or a block to #run"
|
353
|
-
end
|
354
|
-
end
|
355
|
-
|
356
|
-
# Check for an options presence.
|
357
|
-
#
|
358
|
-
# Examples:
|
359
|
-
#
|
360
|
-
# opts.parse %w( --foo )
|
361
|
-
# opts.present?(:foo) #=> true
|
362
|
-
# opts.present?(:bar) #=> false
|
363
|
-
#
|
364
|
-
# Returns true if all of the keys are present in the parsed arguments.
|
365
|
-
def present?(*keys)
|
366
|
-
keys.all? { |key| (opt = fetch_option(key)) && opt.count > 0 }
|
367
|
-
end
|
368
|
-
|
369
|
-
# Override this method so we can check if an option? method exists.
|
370
|
-
#
|
371
|
-
# Returns true if this option key exists in our list of options.
|
372
|
-
def respond_to_missing?(method_name, include_private = false)
|
373
|
-
options.any? { |o| o.key == method_name.to_s.chop } || super
|
374
|
-
end
|
375
|
-
|
376
|
-
# Fetch a list of options which were missing from the parsed list.
|
377
|
-
#
|
378
|
-
# Examples:
|
379
|
-
#
|
380
|
-
# opts = Slop.new do
|
381
|
-
# on :n, :name=
|
382
|
-
# on :p, :password=
|
383
|
-
# end
|
384
|
-
#
|
385
|
-
# opts.parse %w[ --name Lee ]
|
386
|
-
# opts.missing #=> ['password']
|
387
|
-
#
|
388
|
-
# Returns an Array of Strings representing missing options.
|
389
|
-
def missing
|
390
|
-
(options - @triggered_options).map(&:key)
|
391
|
-
end
|
392
|
-
|
393
|
-
# Fetch a Slop::Option object.
|
394
|
-
#
|
395
|
-
# key - The Symbol or String option key.
|
396
|
-
#
|
397
|
-
# Examples:
|
398
|
-
#
|
399
|
-
# opts.on(:foo, 'Something fooey', :argument => :optional)
|
400
|
-
# opt = opts.fetch_option(:foo)
|
401
|
-
# opt.class #=> Slop::Option
|
402
|
-
# opt.accepts_optional_argument? #=> true
|
403
|
-
#
|
404
|
-
# Returns an Option or nil if none were found.
|
405
|
-
def fetch_option(key)
|
406
|
-
options.find { |option| [option.long, option.short].include?(clean(key)) }
|
407
|
-
end
|
408
|
-
|
409
|
-
# Fetch a Slop object associated with this command.
|
410
|
-
#
|
411
|
-
# command - The String or Symbol name of the command.
|
412
|
-
#
|
413
|
-
# Examples:
|
414
|
-
#
|
415
|
-
# opts.command :foo do
|
416
|
-
# on :v, :verbose, 'Enable verbose mode'
|
417
|
-
# end
|
418
|
-
#
|
419
|
-
# # ruby run.rb foo -v
|
420
|
-
# opts.fetch_command(:foo).verbose? #=> true
|
421
|
-
def fetch_command(command)
|
422
|
-
@commands[command.to_s]
|
423
|
-
end
|
424
|
-
|
425
|
-
# Add a callback.
|
426
|
-
#
|
427
|
-
# label - The Symbol identifier to attach this callback.
|
428
|
-
#
|
429
|
-
# Returns nothing.
|
430
|
-
def add_callback(label, &block)
|
431
|
-
(@callbacks[label] ||= []) << block
|
432
|
-
end
|
433
|
-
|
434
|
-
# Add string separators between options.
|
435
|
-
#
|
436
|
-
# text - The String text to print.
|
437
|
-
def separator(text)
|
438
|
-
if @separators[options.size]
|
439
|
-
@separators[options.size] << "\n#{text}"
|
440
|
-
else
|
441
|
-
@separators[options.size] = text
|
442
|
-
end
|
443
|
-
end
|
444
|
-
|
445
|
-
# Print a handy Slop help string.
|
446
|
-
#
|
447
|
-
# Returns the banner followed by available option help strings.
|
448
|
-
def to_s
|
449
|
-
heads = options.reject(&:tail?)
|
450
|
-
tails = (options - heads)
|
451
|
-
opts = (heads + tails).select(&:help).map(&:to_s)
|
452
|
-
optstr = opts.each_with_index.map { |o, i|
|
453
|
-
(str = @separators[i + 1]) ? [o, str].join("\n") : o
|
454
|
-
}.join("\n")
|
455
|
-
|
456
|
-
if @commands.any?
|
457
|
-
optstr << "\n" if !optstr.empty?
|
458
|
-
optstr << "\nAvailable commands:\n\n"
|
459
|
-
optstr << commands_to_help
|
460
|
-
optstr << "\n\nSee `<command> --help` for more information on a specific command."
|
461
|
-
end
|
462
|
-
|
463
|
-
banner = config[:banner]
|
464
|
-
if banner.nil?
|
465
|
-
banner = "Usage: #{File.basename($0, '.*')}"
|
466
|
-
banner << " #{@command}" if @command
|
467
|
-
banner << " [command]" if @commands.any?
|
468
|
-
banner << " [options]"
|
469
|
-
end
|
470
|
-
if banner
|
471
|
-
"#{banner}\n#{@separators[0] ? "#{@separators[0]}\n" : ''}#{optstr}"
|
472
|
-
else
|
473
|
-
optstr
|
474
|
-
end
|
475
|
-
end
|
476
|
-
alias help to_s
|
477
|
-
|
478
|
-
private
|
479
|
-
|
480
|
-
# Convenience method for present?(:option).
|
481
|
-
#
|
482
|
-
# Examples:
|
483
|
-
#
|
484
|
-
# opts.parse %( --verbose )
|
485
|
-
# opts.verbose? #=> true
|
486
|
-
# opts.other? #=> false
|
487
|
-
#
|
488
|
-
# Returns true if this option is present. If this method does not end
|
489
|
-
# with a ? character it will instead call super().
|
490
|
-
def method_missing(method, *args, &block)
|
491
|
-
meth = method.to_s
|
492
|
-
if meth.end_with?('?')
|
493
|
-
meth.chop!
|
494
|
-
present?(meth) || present?(meth.gsub('_', '-'))
|
495
|
-
else
|
496
|
-
super
|
497
|
-
end
|
498
|
-
end
|
499
|
-
|
500
|
-
# Process a list item, figure out if it's an option, execute any
|
501
|
-
# callbacks, assign any option arguments, and do some sanity checks.
|
502
|
-
#
|
503
|
-
# items - The Array of items to process.
|
504
|
-
# index - The current Integer index of the item we want to process.
|
505
|
-
# block - An optional block which when passed will yield non options.
|
506
|
-
#
|
507
|
-
# Returns nothing.
|
508
|
-
def process_item(items, index, &block)
|
509
|
-
return unless item = items[index]
|
510
|
-
option, argument = extract_option(item) if item.start_with?('-')
|
511
|
-
|
512
|
-
if option
|
513
|
-
option.count += 1 unless item.start_with?('--no-')
|
514
|
-
option.count += 1 if option.key[0, 3] == "no-"
|
515
|
-
@trash << index
|
516
|
-
@triggered_options << option
|
517
|
-
|
518
|
-
if option.expects_argument?
|
519
|
-
argument ||= items.at(index + 1)
|
520
|
-
|
521
|
-
if !argument || argument =~ /\A--?[a-zA-Z][a-zA-Z0-9_-]*\z/
|
522
|
-
raise MissingArgumentError, "#{option.key} expects an argument"
|
523
|
-
end
|
524
|
-
|
525
|
-
execute_option(option, argument, index, item)
|
526
|
-
elsif option.accepts_optional_argument?
|
527
|
-
argument ||= items.at(index + 1)
|
528
|
-
|
529
|
-
if argument && argument =~ /\A([^\-?]|-\d)+/
|
530
|
-
execute_option(option, argument, index, item)
|
531
|
-
else
|
532
|
-
option.call(nil)
|
533
|
-
end
|
534
|
-
elsif config[:multiple_switches] && argument
|
535
|
-
execute_multiple_switches(option, argument, items, index)
|
536
|
-
else
|
537
|
-
option.value = option.count > 0
|
538
|
-
option.call(nil)
|
539
|
-
end
|
540
|
-
else
|
541
|
-
@unknown_options << item if strict? && item =~ /\A--?/
|
542
|
-
block.call(item) if block && !@trash.include?(index)
|
543
|
-
end
|
544
|
-
end
|
545
|
-
|
546
|
-
# Execute an option, firing off callbacks and assigning arguments.
|
547
|
-
#
|
548
|
-
# option - The Slop::Option object found by #process_item.
|
549
|
-
# argument - The argument Object to assign to this option.
|
550
|
-
# index - The current Integer index of the object we're processing.
|
551
|
-
# item - The optional String item we're processing.
|
552
|
-
#
|
553
|
-
# Returns nothing.
|
554
|
-
def execute_option(option, argument, index, item = nil)
|
555
|
-
if !option
|
556
|
-
if config[:multiple_switches] && strict?
|
557
|
-
raise InvalidOptionError, "Unknown option -#{item}"
|
558
|
-
end
|
559
|
-
return
|
560
|
-
end
|
561
|
-
|
562
|
-
if argument
|
563
|
-
unless item && item.end_with?("=#{argument}")
|
564
|
-
@trash << index + 1 unless option.argument_in_value
|
565
|
-
end
|
566
|
-
option.value = argument
|
567
|
-
else
|
568
|
-
option.value = option.count > 0
|
569
|
-
end
|
570
|
-
|
571
|
-
if option.match? && !argument.match(option.config[:match])
|
572
|
-
raise InvalidArgumentError, "#{argument} is an invalid argument"
|
573
|
-
end
|
574
|
-
|
575
|
-
option.call(option.value)
|
576
|
-
end
|
577
|
-
|
578
|
-
# Execute a `-abc` type option where a, b and c are all options. This
|
579
|
-
# method is only executed if the multiple_switches argument is true.
|
580
|
-
#
|
581
|
-
# option - The first Option object.
|
582
|
-
# argument - The argument to this option. (Split into multiple Options).
|
583
|
-
# items - The Array of items currently being parsed.
|
584
|
-
# index - The index of the current item being processed.
|
585
|
-
#
|
586
|
-
# Returns nothing.
|
587
|
-
def execute_multiple_switches(option, argument, items, index)
|
588
|
-
execute_option(option, nil, index)
|
589
|
-
flags = argument.split('')
|
590
|
-
flags.each do |key|
|
591
|
-
next unless opt = fetch_option(key)
|
592
|
-
opt.count += 1
|
593
|
-
if (opt.expects_argument? || opt.accepts_optional_argument?) &&
|
594
|
-
(flags[-1] == opt.key) && (val = items[index+1])
|
595
|
-
execute_option(opt, val, index, key)
|
596
|
-
else
|
597
|
-
execute_option(opt, nil, index, key)
|
598
|
-
end
|
599
|
-
end
|
600
|
-
end
|
601
|
-
|
602
|
-
# Extract an option from a flag.
|
603
|
-
#
|
604
|
-
# flag - The flag key used to extract an option.
|
605
|
-
#
|
606
|
-
# Returns an Array of [option, argument].
|
607
|
-
def extract_option(flag)
|
608
|
-
option = fetch_option(flag)
|
609
|
-
option ||= fetch_option(flag.downcase) if config[:ignore_case]
|
610
|
-
option ||= fetch_option(flag.gsub(/([^-])-/, '\1_'))
|
611
|
-
|
612
|
-
unless option
|
613
|
-
case flag
|
614
|
-
when /\A--?([^=]+)=(.+)\z/, /\A-([a-zA-Z])(.+)\z/, /\A--no-(.+)\z/
|
615
|
-
option, argument = fetch_option($1), ($2 || false)
|
616
|
-
option.argument_in_value = true if option
|
617
|
-
end
|
618
|
-
end
|
619
|
-
|
620
|
-
[option, argument]
|
621
|
-
end
|
622
|
-
|
623
|
-
# Autocreate an option on the fly. See the :autocreate Slop config option.
|
624
|
-
#
|
625
|
-
# items - The Array of items we're parsing.
|
626
|
-
# index - The current Integer index for the item we're processing.
|
627
|
-
#
|
628
|
-
# Returns nothing.
|
629
|
-
def autocreate(items, index)
|
630
|
-
flag = items[index]
|
631
|
-
if !fetch_option(flag) && !@trash.include?(index)
|
632
|
-
option = build_option(Array(flag))
|
633
|
-
argument = items[index + 1]
|
634
|
-
option.config[:argument] = (argument && argument !~ /\A--?/)
|
635
|
-
option.config[:autocreated] = true
|
636
|
-
options << option
|
637
|
-
end
|
638
|
-
end
|
639
|
-
|
640
|
-
# Build an option from a list of objects.
|
641
|
-
#
|
642
|
-
# objects - An Array of objects used to build this option.
|
643
|
-
#
|
644
|
-
# Returns a new instance of Slop::Option.
|
645
|
-
def build_option(objects, &block)
|
646
|
-
config = {}
|
647
|
-
config[:argument] = true if @config[:arguments]
|
648
|
-
config[:optional_argument] = true if @config[:optional_arguments]
|
649
|
-
|
650
|
-
if objects.last.is_a?(Hash)
|
651
|
-
config.merge!(objects.pop)
|
652
|
-
end
|
653
|
-
|
654
|
-
short = extract_short_flag(objects, config)
|
655
|
-
long = extract_long_flag(objects, config)
|
656
|
-
desc = objects.shift if objects[0].respond_to?(:to_str)
|
657
|
-
|
658
|
-
Option.new(self, short, long, desc, config, &block)
|
659
|
-
end
|
660
|
-
|
661
|
-
def extract_short_flag(objects, config)
|
662
|
-
flag = objects[0].to_s
|
663
|
-
if flag =~ /\A-?\w=?\z/
|
664
|
-
config[:argument] ||= flag.end_with?('=')
|
665
|
-
objects.shift
|
666
|
-
flag.delete('-=')
|
667
|
-
end
|
668
|
-
end
|
669
|
-
|
670
|
-
# Extract the long flag from an item.
|
671
|
-
#
|
672
|
-
# objects - The Array of objects passed from #build_option.
|
673
|
-
# config - The Hash of configuration options built in #build_option.
|
674
|
-
def extract_long_flag(objects, config)
|
675
|
-
flag = objects.first.to_s
|
676
|
-
if flag =~ /\A(?:--?)?[a-zA-Z0-9][a-zA-Z0-9_.-]+\=?\??\z/
|
677
|
-
config[:argument] ||= true if flag.end_with?('=')
|
678
|
-
config[:optional_argument] = true if flag.end_with?('=?')
|
679
|
-
objects.shift
|
680
|
-
clean(flag).sub(/\=\??\z/, '')
|
681
|
-
end
|
682
|
-
end
|
683
|
-
|
684
|
-
# Remove any leading -- characters from a string.
|
685
|
-
#
|
686
|
-
# object - The Object we want to cast to a String and clean.
|
687
|
-
#
|
688
|
-
# Returns the newly cleaned String with leading -- characters removed.
|
689
|
-
def clean(object)
|
690
|
-
object.to_s.sub(/\A--?/, '')
|
691
|
-
end
|
692
|
-
|
693
|
-
def commands_to_help
|
694
|
-
padding = 0
|
695
|
-
@commands.each { |c, _| padding = c.size if c.size > padding }
|
696
|
-
@commands.map do |cmd, opts|
|
697
|
-
" #{cmd}#{' ' * (padding - cmd.size)} #{opts.description}"
|
698
|
-
end.join("\n")
|
699
|
-
end
|
700
|
-
|
701
|
-
end
|