thor-exclude_pattern 0.18.0
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/.document +5 -0
- data/CHANGELOG.md +130 -0
- data/LICENSE.md +20 -0
- data/README.md +34 -0
- data/Thorfile +30 -0
- data/bin/rake2thor +86 -0
- data/bin/thor +6 -0
- data/lib/thor.rb +458 -0
- data/lib/thor/actions.rb +318 -0
- data/lib/thor/actions/create_file.rb +105 -0
- data/lib/thor/actions/create_link.rb +60 -0
- data/lib/thor/actions/directory.rb +119 -0
- data/lib/thor/actions/empty_directory.rb +153 -0
- data/lib/thor/actions/file_manipulation.rb +314 -0
- data/lib/thor/actions/inject_into_file.rb +109 -0
- data/lib/thor/base.rb +649 -0
- data/lib/thor/command.rb +136 -0
- data/lib/thor/core_ext/hash_with_indifferent_access.rb +80 -0
- data/lib/thor/core_ext/io_binary_read.rb +12 -0
- data/lib/thor/core_ext/ordered_hash.rb +100 -0
- data/lib/thor/error.rb +32 -0
- data/lib/thor/exclude_pattern/version.rb +5 -0
- data/lib/thor/group.rb +287 -0
- data/lib/thor/invocation.rb +172 -0
- data/lib/thor/parser.rb +4 -0
- data/lib/thor/parser/argument.rb +74 -0
- data/lib/thor/parser/arguments.rb +171 -0
- data/lib/thor/parser/option.rb +121 -0
- data/lib/thor/parser/options.rb +218 -0
- data/lib/thor/rake_compat.rb +72 -0
- data/lib/thor/runner.rb +322 -0
- data/lib/thor/shell.rb +88 -0
- data/lib/thor/shell/basic.rb +393 -0
- data/lib/thor/shell/color.rb +148 -0
- data/lib/thor/shell/html.rb +127 -0
- data/lib/thor/util.rb +270 -0
- data/lib/thor/version.rb +3 -0
- data/spec/actions/create_file_spec.rb +170 -0
- data/spec/actions/create_link_spec.rb +95 -0
- data/spec/actions/directory_spec.rb +169 -0
- data/spec/actions/empty_directory_spec.rb +130 -0
- data/spec/actions/file_manipulation_spec.rb +382 -0
- data/spec/actions/inject_into_file_spec.rb +135 -0
- data/spec/actions_spec.rb +331 -0
- data/spec/base_spec.rb +294 -0
- data/spec/command_spec.rb +80 -0
- data/spec/core_ext/hash_with_indifferent_access_spec.rb +48 -0
- data/spec/core_ext/ordered_hash_spec.rb +115 -0
- data/spec/exit_condition_spec.rb +19 -0
- data/spec/fixtures/application.rb +2 -0
- data/spec/fixtures/app{1}/README +3 -0
- data/spec/fixtures/bundle/execute.rb +6 -0
- data/spec/fixtures/bundle/main.thor +1 -0
- data/spec/fixtures/command.thor +10 -0
- data/spec/fixtures/doc/%file_name%.rb.tt +1 -0
- data/spec/fixtures/doc/COMMENTER +11 -0
- data/spec/fixtures/doc/README +3 -0
- data/spec/fixtures/doc/block_helper.rb +3 -0
- data/spec/fixtures/doc/config.rb +1 -0
- data/spec/fixtures/doc/config.yaml.tt +1 -0
- data/spec/fixtures/doc/excluding/%file_name%.rb.tt +1 -0
- data/spec/fixtures/enum.thor +10 -0
- data/spec/fixtures/group.thor +128 -0
- data/spec/fixtures/invoke.thor +112 -0
- data/spec/fixtures/path with spaces b/data/spec/fixtures/path with → spaces +0 -0
- data/spec/fixtures/preserve/script.sh +3 -0
- data/spec/fixtures/script.thor +199 -0
- data/spec/fixtures/subcommand.thor +17 -0
- data/spec/group_spec.rb +216 -0
- data/spec/helper.rb +67 -0
- data/spec/invocation_spec.rb +100 -0
- data/spec/parser/argument_spec.rb +53 -0
- data/spec/parser/arguments_spec.rb +66 -0
- data/spec/parser/option_spec.rb +202 -0
- data/spec/parser/options_spec.rb +400 -0
- data/spec/rake_compat_spec.rb +72 -0
- data/spec/register_spec.rb +197 -0
- data/spec/runner_spec.rb +241 -0
- data/spec/sandbox/application.rb +2 -0
- data/spec/sandbox/app{1}/README +3 -0
- data/spec/sandbox/bundle/execute.rb +6 -0
- data/spec/sandbox/bundle/main.thor +1 -0
- data/spec/sandbox/command.thor +10 -0
- data/spec/sandbox/doc/%file_name%.rb.tt +1 -0
- data/spec/sandbox/doc/COMMENTER +11 -0
- data/spec/sandbox/doc/README +4 -0
- data/spec/sandbox/doc/block_helper.rb +3 -0
- data/spec/sandbox/doc/config.rb +1 -0
- data/spec/sandbox/doc/config.yaml.tt +1 -0
- data/spec/sandbox/doc/excluding/%file_name%.rb.tt +1 -0
- data/spec/sandbox/enum.thor +10 -0
- data/spec/sandbox/group.thor +128 -0
- data/spec/sandbox/invoke.thor +112 -0
- data/spec/sandbox/path with spaces b/data/spec/sandbox/path with → spaces +0 -0
- data/spec/sandbox/preserve/script.sh +3 -0
- data/spec/sandbox/script.thor +199 -0
- data/spec/sandbox/subcommand.thor +17 -0
- data/spec/shell/basic_spec.rb +311 -0
- data/spec/shell/color_spec.rb +95 -0
- data/spec/shell/html_spec.rb +32 -0
- data/spec/shell_spec.rb +47 -0
- data/spec/subcommand_spec.rb +30 -0
- data/spec/thor_spec.rb +469 -0
- data/spec/util_spec.rb +196 -0
- data/thor.gemspec +24 -0
- metadata +232 -0
data/spec/helper.rb
ADDED
@@ -0,0 +1,67 @@
|
|
1
|
+
$TESTING=true
|
2
|
+
|
3
|
+
require 'simplecov'
|
4
|
+
require 'coveralls'
|
5
|
+
|
6
|
+
SimpleCov.formatter = SimpleCov::Formatter::MultiFormatter[
|
7
|
+
SimpleCov::Formatter::HTMLFormatter,
|
8
|
+
Coveralls::SimpleCov::Formatter
|
9
|
+
]
|
10
|
+
SimpleCov.start
|
11
|
+
|
12
|
+
$:.unshift(File.join(File.dirname(__FILE__), "..", "lib"))
|
13
|
+
require 'thor'
|
14
|
+
require 'thor/group'
|
15
|
+
require 'stringio'
|
16
|
+
|
17
|
+
require 'rdoc'
|
18
|
+
require 'rspec'
|
19
|
+
require 'diff/lcs' # You need diff/lcs installed to run specs (but not to run Thor).
|
20
|
+
require 'fakeweb' # You need fakeweb installed to run specs (but not to run Thor).
|
21
|
+
|
22
|
+
# Set shell to basic
|
23
|
+
$0 = "thor"
|
24
|
+
$thor_runner = true
|
25
|
+
ARGV.clear
|
26
|
+
Thor::Base.shell = Thor::Shell::Basic
|
27
|
+
|
28
|
+
# Load fixtures
|
29
|
+
load File.join(File.dirname(__FILE__), "fixtures", "enum.thor")
|
30
|
+
load File.join(File.dirname(__FILE__), "fixtures", "group.thor")
|
31
|
+
load File.join(File.dirname(__FILE__), "fixtures", "invoke.thor")
|
32
|
+
load File.join(File.dirname(__FILE__), "fixtures", "script.thor")
|
33
|
+
load File.join(File.dirname(__FILE__), "fixtures", "subcommand.thor")
|
34
|
+
load File.join(File.dirname(__FILE__), "fixtures", "command.thor")
|
35
|
+
|
36
|
+
RSpec.configure do |config|
|
37
|
+
config.before do
|
38
|
+
ARGV.replace []
|
39
|
+
end
|
40
|
+
|
41
|
+
config.expect_with :rspec do |c|
|
42
|
+
c.syntax = :expect
|
43
|
+
end
|
44
|
+
|
45
|
+
def capture(stream)
|
46
|
+
begin
|
47
|
+
stream = stream.to_s
|
48
|
+
eval "$#{stream} = StringIO.new"
|
49
|
+
yield
|
50
|
+
result = eval("$#{stream}").string
|
51
|
+
ensure
|
52
|
+
eval("$#{stream} = #{stream.upcase}")
|
53
|
+
end
|
54
|
+
|
55
|
+
result
|
56
|
+
end
|
57
|
+
|
58
|
+
def source_root
|
59
|
+
File.join(File.dirname(__FILE__), 'fixtures')
|
60
|
+
end
|
61
|
+
|
62
|
+
def destination_root
|
63
|
+
File.join(File.dirname(__FILE__), 'sandbox')
|
64
|
+
end
|
65
|
+
|
66
|
+
alias :silence :capture
|
67
|
+
end
|
@@ -0,0 +1,100 @@
|
|
1
|
+
require 'helper'
|
2
|
+
require 'thor/base'
|
3
|
+
|
4
|
+
describe Thor::Invocation do
|
5
|
+
describe "#invoke" do
|
6
|
+
it "invokes a command inside another command" do
|
7
|
+
expect(capture(:stdout) { A.new.invoke(:two) }).to eq("2\n3\n")
|
8
|
+
end
|
9
|
+
|
10
|
+
it "invokes a command just once" do
|
11
|
+
expect(capture(:stdout) { A.new.invoke(:one) }).to eq("1\n2\n3\n")
|
12
|
+
end
|
13
|
+
|
14
|
+
it "invokes a command just once even if they belongs to different classes" do
|
15
|
+
expect(capture(:stdout) { Defined.new.invoke(:one) }).to eq("1\n2\n3\n4\n5\n")
|
16
|
+
end
|
17
|
+
|
18
|
+
it "invokes a command with arguments" do
|
19
|
+
expect(A.new.invoke(:five, [5])).to be_true
|
20
|
+
expect(A.new.invoke(:five, [7])).to be_false
|
21
|
+
end
|
22
|
+
|
23
|
+
it "invokes the default command if none is given to a Thor class" do
|
24
|
+
content = capture(:stdout) { A.new.invoke("b") }
|
25
|
+
expect(content).to match(/Commands/)
|
26
|
+
expect(content).to match(/LAST_NAME/)
|
27
|
+
end
|
28
|
+
|
29
|
+
it "accepts a class as argument without a command to invoke" do
|
30
|
+
content = capture(:stdout) { A.new.invoke(B) }
|
31
|
+
expect(content).to match(/Commands/)
|
32
|
+
expect(content).to match(/LAST_NAME/)
|
33
|
+
end
|
34
|
+
|
35
|
+
it "accepts a class as argument with a command to invoke" do
|
36
|
+
base = A.new([], :last_name => "Valim")
|
37
|
+
expect(base.invoke(B, :one, ["Jose"])).to eq("Valim, Jose")
|
38
|
+
end
|
39
|
+
|
40
|
+
it "allows customized options to be given" do
|
41
|
+
base = A.new([], :last_name => "Wrong")
|
42
|
+
expect(base.invoke(B, :one, ["Jose"], :last_name => "Valim")).to eq("Valim, Jose")
|
43
|
+
end
|
44
|
+
|
45
|
+
it "reparses options in the new class" do
|
46
|
+
expect(A.start(["invoker", "--last-name", "Valim"])).to eq("Valim, Jose")
|
47
|
+
end
|
48
|
+
|
49
|
+
it "shares initialize options with invoked class" do
|
50
|
+
expect(A.new([], :foo => :bar).invoke("b:two")).to eq({ "foo" => :bar })
|
51
|
+
end
|
52
|
+
|
53
|
+
it "dump configuration values to be used in the invoked class" do
|
54
|
+
base = A.new
|
55
|
+
expect(base.invoke("b:three").shell).to eq(base.shell)
|
56
|
+
end
|
57
|
+
|
58
|
+
it "allow extra configuration values to be given" do
|
59
|
+
base, shell = A.new, Thor::Base.shell.new
|
60
|
+
expect(base.invoke("b:three", [], {}, :shell => shell).shell).to eq(shell)
|
61
|
+
end
|
62
|
+
|
63
|
+
it "invokes a Thor::Group and all of its commands" do
|
64
|
+
expect(capture(:stdout) { A.new.invoke(:c) }).to eq("1\n2\n3\n")
|
65
|
+
end
|
66
|
+
|
67
|
+
it "does not invoke a Thor::Group twice" do
|
68
|
+
base = A.new
|
69
|
+
silence(:stdout){ base.invoke(:c) }
|
70
|
+
expect(capture(:stdout) { base.invoke(:c) }).to be_empty
|
71
|
+
end
|
72
|
+
|
73
|
+
it "does not invoke any of Thor::Group commands twice" do
|
74
|
+
base = A.new
|
75
|
+
silence(:stdout){ base.invoke(:c) }
|
76
|
+
expect(capture(:stdout) { base.invoke("c:one") }).to be_empty
|
77
|
+
end
|
78
|
+
|
79
|
+
it "raises Thor::UndefinedcommandError if the command can't be found" do
|
80
|
+
expect {
|
81
|
+
A.new.invoke("foo:bar")
|
82
|
+
}.to raise_error(Thor::UndefinedCommandError)
|
83
|
+
end
|
84
|
+
|
85
|
+
it "raises Thor::UndefinedcommandError if the command can't be found even if all commands were already executed" do
|
86
|
+
base = C.new
|
87
|
+
silence(:stdout){ base.invoke_all }
|
88
|
+
|
89
|
+
expect {
|
90
|
+
base.invoke("foo:bar")
|
91
|
+
}.to raise_error(Thor::UndefinedCommandError)
|
92
|
+
end
|
93
|
+
|
94
|
+
it "raises an error if a non Thor class is given" do
|
95
|
+
expect {
|
96
|
+
A.new.invoke(Object)
|
97
|
+
}.to raise_error(RuntimeError, "Expected Thor class, got Object")
|
98
|
+
end
|
99
|
+
end
|
100
|
+
end
|
@@ -0,0 +1,53 @@
|
|
1
|
+
require 'helper'
|
2
|
+
require 'thor/parser'
|
3
|
+
|
4
|
+
describe Thor::Argument do
|
5
|
+
|
6
|
+
def argument(name, options={})
|
7
|
+
@argument ||= Thor::Argument.new(name, options)
|
8
|
+
end
|
9
|
+
|
10
|
+
describe "errors" do
|
11
|
+
it "raises an error if name is not supplied" do
|
12
|
+
expect {
|
13
|
+
argument(nil)
|
14
|
+
}.to raise_error(ArgumentError, "Argument name can't be nil.")
|
15
|
+
end
|
16
|
+
|
17
|
+
it "raises an error if type is unknown" do
|
18
|
+
expect {
|
19
|
+
argument(:command, :type => :unknown)
|
20
|
+
}.to raise_error(ArgumentError, "Type :unknown is not valid for arguments.")
|
21
|
+
end
|
22
|
+
|
23
|
+
it "raises an error if argument is required and have default values" do
|
24
|
+
expect {
|
25
|
+
argument(:command, :type => :string, :default => "bar", :required => true)
|
26
|
+
}.to raise_error(ArgumentError, "An argument cannot be required and have default value.")
|
27
|
+
end
|
28
|
+
|
29
|
+
it "raises an error if enum isn't an array" do
|
30
|
+
expect {
|
31
|
+
argument(:command, :type => :string, :enum => "bar")
|
32
|
+
}.to raise_error(ArgumentError, "An argument cannot have an enum other than an array.")
|
33
|
+
end
|
34
|
+
end
|
35
|
+
|
36
|
+
describe "#usage" do
|
37
|
+
it "returns usage for string types" do
|
38
|
+
expect(argument(:foo, :type => :string).usage).to eq("FOO")
|
39
|
+
end
|
40
|
+
|
41
|
+
it "returns usage for numeric types" do
|
42
|
+
expect(argument(:foo, :type => :numeric).usage).to eq("N")
|
43
|
+
end
|
44
|
+
|
45
|
+
it "returns usage for array types" do
|
46
|
+
expect(argument(:foo, :type => :array).usage).to eq("one two three")
|
47
|
+
end
|
48
|
+
|
49
|
+
it "returns usage for hash types" do
|
50
|
+
expect(argument(:foo, :type => :hash).usage).to eq("key:value")
|
51
|
+
end
|
52
|
+
end
|
53
|
+
end
|
@@ -0,0 +1,66 @@
|
|
1
|
+
require 'helper'
|
2
|
+
require 'thor/parser'
|
3
|
+
|
4
|
+
describe Thor::Arguments do
|
5
|
+
def create(opts={})
|
6
|
+
arguments = opts.map do |type, default|
|
7
|
+
options = {:required => default.nil?, :type => type, :default => default}
|
8
|
+
Thor::Argument.new(type.to_s, options)
|
9
|
+
end
|
10
|
+
|
11
|
+
arguments.sort!{ |a,b| b.name <=> a.name }
|
12
|
+
@opt = Thor::Arguments.new(arguments)
|
13
|
+
end
|
14
|
+
|
15
|
+
def parse(*args)
|
16
|
+
@opt.parse(args)
|
17
|
+
end
|
18
|
+
|
19
|
+
describe "#parse" do
|
20
|
+
it "parses arguments in the given order" do
|
21
|
+
create :string => nil, :numeric => nil
|
22
|
+
expect(parse("name", "13")["string"]).to eq("name")
|
23
|
+
expect(parse("name", "13")["numeric"]).to eq(13)
|
24
|
+
end
|
25
|
+
|
26
|
+
it "accepts hashes" do
|
27
|
+
create :string => nil, :hash => nil
|
28
|
+
expect(parse("product", "title:string", "age:integer")["string"]).to eq("product")
|
29
|
+
expect(parse("product", "title:string", "age:integer")["hash"]).to eq({ "title" => "string", "age" => "integer"})
|
30
|
+
expect(parse("product", "url:http://www.amazon.com/gp/product/123")["hash"]).to eq({ "url" => "http://www.amazon.com/gp/product/123" })
|
31
|
+
end
|
32
|
+
|
33
|
+
it "accepts arrays" do
|
34
|
+
create :string => nil, :array => nil
|
35
|
+
expect(parse("product", "title", "age")["string"]).to eq("product")
|
36
|
+
expect(parse("product", "title", "age")["array"]).to eq(%w(title age))
|
37
|
+
end
|
38
|
+
|
39
|
+
describe "with no inputs" do
|
40
|
+
it "and no arguments returns an empty hash" do
|
41
|
+
create
|
42
|
+
expect(parse).to eq({})
|
43
|
+
end
|
44
|
+
|
45
|
+
it "and required arguments raises an error" do
|
46
|
+
create :string => nil, :numeric => nil
|
47
|
+
expect{ parse }.to raise_error(Thor::RequiredArgumentMissingError, "No value provided for required arguments 'string', 'numeric'")
|
48
|
+
end
|
49
|
+
|
50
|
+
it "and default arguments returns default values" do
|
51
|
+
create :string => "name", :numeric => 13
|
52
|
+
expect(parse).to eq({ "string" => "name", "numeric" => 13 })
|
53
|
+
end
|
54
|
+
end
|
55
|
+
|
56
|
+
it "returns the input if it's already parsed" do
|
57
|
+
create :string => nil, :hash => nil, :array => nil, :numeric => nil
|
58
|
+
expect(parse("", 0, {}, [])).to eq({ "string" => "", "numeric" => 0, "hash" => {}, "array" => [] })
|
59
|
+
end
|
60
|
+
|
61
|
+
it "returns the default value if none is provided" do
|
62
|
+
create :string => "foo", :numeric => 3.0
|
63
|
+
expect(parse("bar")).to eq({ "string" => "bar", "numeric" => 3.0 })
|
64
|
+
end
|
65
|
+
end
|
66
|
+
end
|
@@ -0,0 +1,202 @@
|
|
1
|
+
require 'helper'
|
2
|
+
require 'thor/parser'
|
3
|
+
|
4
|
+
describe Thor::Option do
|
5
|
+
def parse(key, value)
|
6
|
+
Thor::Option.parse(key, value)
|
7
|
+
end
|
8
|
+
|
9
|
+
def option(name, options={})
|
10
|
+
@option ||= Thor::Option.new(name, options)
|
11
|
+
end
|
12
|
+
|
13
|
+
describe "#parse" do
|
14
|
+
|
15
|
+
describe "with value as a symbol" do
|
16
|
+
describe "and symbol is a valid type" do
|
17
|
+
it "has type equals to the symbol" do
|
18
|
+
expect(parse(:foo, :string).type).to eq(:string)
|
19
|
+
expect(parse(:foo, :numeric).type).to eq(:numeric)
|
20
|
+
end
|
21
|
+
|
22
|
+
it "has not default value" do
|
23
|
+
expect(parse(:foo, :string).default).to be_nil
|
24
|
+
expect(parse(:foo, :numeric).default).to be_nil
|
25
|
+
end
|
26
|
+
end
|
27
|
+
|
28
|
+
describe "equals to :required" do
|
29
|
+
it "has type equals to :string" do
|
30
|
+
expect(parse(:foo, :required).type).to eq(:string)
|
31
|
+
end
|
32
|
+
|
33
|
+
it "has no default value" do
|
34
|
+
expect(parse(:foo, :required).default).to be_nil
|
35
|
+
end
|
36
|
+
end
|
37
|
+
|
38
|
+
describe "and symbol is not a reserved key" do
|
39
|
+
it "has type equals to :string" do
|
40
|
+
expect(parse(:foo, :bar).type).to eq(:string)
|
41
|
+
end
|
42
|
+
|
43
|
+
it "has no default value" do
|
44
|
+
expect(parse(:foo, :bar).default).to be_nil
|
45
|
+
end
|
46
|
+
end
|
47
|
+
end
|
48
|
+
|
49
|
+
describe "with value as hash" do
|
50
|
+
it "has default type :hash" do
|
51
|
+
expect(parse(:foo, :a => :b).type).to eq(:hash)
|
52
|
+
end
|
53
|
+
|
54
|
+
it "has default value equals to the hash" do
|
55
|
+
expect(parse(:foo, :a => :b).default).to eq({ :a => :b })
|
56
|
+
end
|
57
|
+
end
|
58
|
+
|
59
|
+
describe "with value as array" do
|
60
|
+
it "has default type :array" do
|
61
|
+
expect(parse(:foo, [:a, :b]).type).to eq(:array)
|
62
|
+
end
|
63
|
+
|
64
|
+
it "has default value equals to the array" do
|
65
|
+
expect(parse(:foo, [:a, :b]).default).to eq([:a, :b])
|
66
|
+
end
|
67
|
+
end
|
68
|
+
|
69
|
+
describe "with value as string" do
|
70
|
+
it "has default type :string" do
|
71
|
+
expect(parse(:foo, "bar").type).to eq(:string)
|
72
|
+
end
|
73
|
+
|
74
|
+
it "has default value equals to the string" do
|
75
|
+
expect(parse(:foo, "bar").default).to eq("bar")
|
76
|
+
end
|
77
|
+
end
|
78
|
+
|
79
|
+
describe "with value as numeric" do
|
80
|
+
it "has default type :numeric" do
|
81
|
+
expect(parse(:foo, 2.0).type).to eq(:numeric)
|
82
|
+
end
|
83
|
+
|
84
|
+
it "has default value equals to the numeric" do
|
85
|
+
expect(parse(:foo, 2.0).default).to eq(2.0)
|
86
|
+
end
|
87
|
+
end
|
88
|
+
|
89
|
+
describe "with value as boolean" do
|
90
|
+
it "has default type :boolean" do
|
91
|
+
expect(parse(:foo, true).type).to eq(:boolean)
|
92
|
+
expect(parse(:foo, false).type).to eq(:boolean)
|
93
|
+
end
|
94
|
+
|
95
|
+
it "has default value equals to the boolean" do
|
96
|
+
expect(parse(:foo, true).default).to eq(true)
|
97
|
+
expect(parse(:foo, false).default).to eq(false)
|
98
|
+
end
|
99
|
+
end
|
100
|
+
|
101
|
+
describe "with key as a symbol" do
|
102
|
+
it "sets the name equals to the key" do
|
103
|
+
expect(parse(:foo, true).name).to eq("foo")
|
104
|
+
end
|
105
|
+
end
|
106
|
+
|
107
|
+
describe "with key as an array" do
|
108
|
+
it "sets the first items in the array to the name" do
|
109
|
+
expect(parse([:foo, :bar, :baz], true).name).to eq("foo")
|
110
|
+
end
|
111
|
+
|
112
|
+
it "sets all other items as aliases" do
|
113
|
+
expect(parse([:foo, :bar, :baz], true).aliases).to eq([:bar, :baz])
|
114
|
+
end
|
115
|
+
end
|
116
|
+
end
|
117
|
+
|
118
|
+
it "returns the switch name" do
|
119
|
+
expect(option("foo").switch_name).to eq("--foo")
|
120
|
+
expect(option("--foo").switch_name).to eq("--foo")
|
121
|
+
end
|
122
|
+
|
123
|
+
it "returns the human name" do
|
124
|
+
expect(option("foo").human_name).to eq("foo")
|
125
|
+
expect(option("--foo").human_name).to eq("foo")
|
126
|
+
end
|
127
|
+
|
128
|
+
it "converts underscores to dashes" do
|
129
|
+
expect(option("foo_bar").switch_name).to eq("--foo-bar")
|
130
|
+
end
|
131
|
+
|
132
|
+
it "can be required and have default values" do
|
133
|
+
option = option("foo", :required => true, :type => :string, :default => "bar")
|
134
|
+
expect(option.default).to eq("bar")
|
135
|
+
expect(option).to be_required
|
136
|
+
end
|
137
|
+
|
138
|
+
it "cannot be required and have type boolean" do
|
139
|
+
expect {
|
140
|
+
option("foo", :required => true, :type => :boolean)
|
141
|
+
}.to raise_error(ArgumentError, "An option cannot be boolean and required.")
|
142
|
+
end
|
143
|
+
|
144
|
+
it "allows type predicates" do
|
145
|
+
expect(parse(:foo, :string)).to be_string
|
146
|
+
expect(parse(:foo, :boolean)).to be_boolean
|
147
|
+
expect(parse(:foo, :numeric)).to be_numeric
|
148
|
+
end
|
149
|
+
|
150
|
+
it "raises an error on method missing" do
|
151
|
+
expect {
|
152
|
+
parse(:foo, :string).unknown?
|
153
|
+
}.to raise_error(NoMethodError)
|
154
|
+
end
|
155
|
+
|
156
|
+
describe "#usage" do
|
157
|
+
|
158
|
+
it "returns usage for string types" do
|
159
|
+
expect(parse(:foo, :string).usage).to eq("[--foo=FOO]")
|
160
|
+
end
|
161
|
+
|
162
|
+
it "returns usage for numeric types" do
|
163
|
+
expect(parse(:foo, :numeric).usage).to eq("[--foo=N]")
|
164
|
+
end
|
165
|
+
|
166
|
+
it "returns usage for array types" do
|
167
|
+
expect(parse(:foo, :array).usage).to eq("[--foo=one two three]")
|
168
|
+
end
|
169
|
+
|
170
|
+
it "returns usage for hash types" do
|
171
|
+
expect(parse(:foo, :hash).usage).to eq("[--foo=key:value]")
|
172
|
+
end
|
173
|
+
|
174
|
+
it "returns usage for boolean types" do
|
175
|
+
expect(parse(:foo, :boolean).usage).to eq("[--foo]")
|
176
|
+
end
|
177
|
+
|
178
|
+
it "uses padding when no aliases is given" do
|
179
|
+
expect(parse(:foo, :boolean).usage(4)).to eq(" [--foo]")
|
180
|
+
end
|
181
|
+
|
182
|
+
it "uses banner when supplied" do
|
183
|
+
expect(option(:foo, :required => false, :type => :string, :banner => "BAR").usage).to eq("[--foo=BAR]")
|
184
|
+
end
|
185
|
+
|
186
|
+
it "checkes when banner is an empty string" do
|
187
|
+
expect(option(:foo, :required => false, :type => :string, :banner => "").usage).to eq("[--foo]")
|
188
|
+
end
|
189
|
+
|
190
|
+
describe "with required values" do
|
191
|
+
it "does not show the usage between brackets" do
|
192
|
+
expect(parse(:foo, :required).usage).to eq("--foo=FOO")
|
193
|
+
end
|
194
|
+
end
|
195
|
+
|
196
|
+
describe "with aliases" do
|
197
|
+
it "does not show the usage between brackets" do
|
198
|
+
expect(parse([:foo, "-f", "-b"], :required).usage).to eq("-f, -b, --foo=FOO")
|
199
|
+
end
|
200
|
+
end
|
201
|
+
end
|
202
|
+
end
|