clive 0.5.0 → 0.6.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/Rakefile +0 -44
- data/lib/clive.rb +2 -11
- data/lib/clive/command.rb +0 -1
- data/lib/clive/output.rb +56 -0
- data/lib/clive/parser.rb +65 -0
- metadata +18 -62
- data/.document +0 -2
- data/.gitignore +0 -25
- data/VERSION +0 -1
- data/clive.gemspec +0 -78
- data/test/bin_test +0 -59
- data/test/helper.rb +0 -14
- data/test/test_boolean.rb +0 -55
- data/test/test_clive.rb +0 -276
- data/test/test_command.rb +0 -44
- data/test/test_exceptions.rb +0 -28
- data/test/test_flag.rb +0 -160
- data/test/test_switch.rb +0 -18
- data/test/test_token.rb +0 -68
data/Rakefile
CHANGED
@@ -1,53 +1,9 @@
|
|
1
1
|
require 'rubygems'
|
2
2
|
require 'rake'
|
3
3
|
|
4
|
-
begin
|
5
|
-
require 'jeweler'
|
6
|
-
Jeweler::Tasks.new do |gem|
|
7
|
-
gem.name = "clive"
|
8
|
-
gem.summary = %Q{Imagine if optparse and gli had a son called clive.}
|
9
|
-
gem.description = %Q{Clive is a DSL for creating a command line interface. It is for people who, like me, love OptionParser's syntax and love GLI's commands.}
|
10
|
-
gem.email = "m@hawx.me"
|
11
|
-
gem.homepage = "http://github.com/hawx/clive"
|
12
|
-
gem.authors = ["Joshua Hawxwell"]
|
13
|
-
gem.add_development_dependency "thoughtbot-shoulda", ">= 0"
|
14
|
-
gem.add_development_dependency "yard", ">= 0"
|
15
|
-
# gem is a Gem::Specification... see http://www.rubygems.org/read/chapter/20 for additional settings
|
16
|
-
end
|
17
|
-
Jeweler::GemcutterTasks.new
|
18
|
-
rescue LoadError
|
19
|
-
puts "Jeweler (or a dependency) not available. Install it with: gem install jeweler"
|
20
|
-
end
|
21
|
-
|
22
4
|
require 'rake/testtask'
|
23
5
|
Rake::TestTask.new(:test) do |test|
|
24
6
|
test.libs << 'lib' << 'test'
|
25
7
|
test.pattern = 'test/**/test_*.rb'
|
26
8
|
test.verbose = true
|
27
9
|
end
|
28
|
-
|
29
|
-
begin
|
30
|
-
require 'rcov/rcovtask'
|
31
|
-
Rcov::RcovTask.new do |test|
|
32
|
-
test.libs << 'test'
|
33
|
-
test.pattern = 'test/**/test_*.rb'
|
34
|
-
test.verbose = true
|
35
|
-
end
|
36
|
-
rescue LoadError
|
37
|
-
task :rcov do
|
38
|
-
abort "RCov is not available. In order to run rcov, you must: sudo gem install spicycode-rcov"
|
39
|
-
end
|
40
|
-
end
|
41
|
-
|
42
|
-
task :test => :check_dependencies
|
43
|
-
|
44
|
-
task :default => :test
|
45
|
-
|
46
|
-
begin
|
47
|
-
require 'yard'
|
48
|
-
YARD::Rake::YardocTask.new
|
49
|
-
rescue LoadError
|
50
|
-
task :yardoc do
|
51
|
-
abort "YARD is not available. In order to run yardoc, you must: sudo gem install yard"
|
52
|
-
end
|
53
|
-
end
|
data/lib/clive.rb
CHANGED
@@ -1,14 +1,5 @@
|
|
1
|
-
|
2
|
-
|
3
|
-
require 'clive/exceptions'
|
4
|
-
require 'clive/tokens'
|
5
|
-
require 'clive/ext'
|
6
|
-
|
7
|
-
require 'clive/option'
|
8
|
-
require 'clive/command'
|
9
|
-
require 'clive/switch'
|
10
|
-
require 'clive/flag'
|
11
|
-
require 'clive/bool'
|
1
|
+
require 'clive/parser'
|
2
|
+
require 'clive/output'
|
12
3
|
|
13
4
|
# Clive is a simple dsl for creating command line interfaces
|
14
5
|
#
|
data/lib/clive/command.rb
CHANGED
data/lib/clive/output.rb
ADDED
@@ -0,0 +1,56 @@
|
|
1
|
+
# This should control the character output to the command line.
|
2
|
+
# It will allow you to set different colours, using:
|
3
|
+
#
|
4
|
+
# "I'm red".red
|
5
|
+
# "I'm red and bold".red.bold
|
6
|
+
#
|
7
|
+
#
|
8
|
+
|
9
|
+
class Clive
|
10
|
+
module Output
|
11
|
+
|
12
|
+
end
|
13
|
+
end
|
14
|
+
|
15
|
+
# Monkey patches for colour
|
16
|
+
class String
|
17
|
+
|
18
|
+
# @example
|
19
|
+
#
|
20
|
+
# require 'clive/output'
|
21
|
+
#
|
22
|
+
# puts "bold".bold
|
23
|
+
# puts "underline".underline
|
24
|
+
# puts "blink".blink
|
25
|
+
# puts "green".green
|
26
|
+
# puts "red".red
|
27
|
+
# puts "magenta".magenta
|
28
|
+
# puts "yellow".yellow
|
29
|
+
# puts "blue".blue
|
30
|
+
# puts "grey".grey
|
31
|
+
#
|
32
|
+
# puts "combo".blue.bold.underline.blink
|
33
|
+
#
|
34
|
+
def colour(code)
|
35
|
+
"#{code}#{self}\e[0m"
|
36
|
+
end
|
37
|
+
|
38
|
+
{
|
39
|
+
"bold" => "1",
|
40
|
+
"underline" => "4",
|
41
|
+
"blink" => "5",
|
42
|
+
"white" => "37",
|
43
|
+
"green" => "32",
|
44
|
+
"red" => "31",
|
45
|
+
"magenta" => "35",
|
46
|
+
"yellow" => "33",
|
47
|
+
"blue" => "34",
|
48
|
+
"grey" => "90"
|
49
|
+
}.each do |name, code|
|
50
|
+
define_method(name) do
|
51
|
+
colour("\e[#{code}m")
|
52
|
+
end
|
53
|
+
end
|
54
|
+
|
55
|
+
end
|
56
|
+
|
data/lib/clive/parser.rb
ADDED
@@ -0,0 +1,65 @@
|
|
1
|
+
require 'clive/exceptions'
|
2
|
+
require 'clive/tokens'
|
3
|
+
require 'clive/ext'
|
4
|
+
|
5
|
+
require 'clive/option'
|
6
|
+
require 'clive/command'
|
7
|
+
require 'clive/switch'
|
8
|
+
require 'clive/flag'
|
9
|
+
require 'clive/bool'
|
10
|
+
|
11
|
+
class Clive
|
12
|
+
|
13
|
+
# A module wrapping the command line parsing of clive. In the future this
|
14
|
+
# will be the only way of using clive.
|
15
|
+
#
|
16
|
+
# @example
|
17
|
+
#
|
18
|
+
# require 'clive/parser'
|
19
|
+
#
|
20
|
+
# class CLI
|
21
|
+
# include Clive::Parser
|
22
|
+
#
|
23
|
+
# @@opts ||= {}
|
24
|
+
# def self.opts
|
25
|
+
# @@opts
|
26
|
+
# end
|
27
|
+
#
|
28
|
+
# switch :v, :verbose, "Run verbosely" do
|
29
|
+
# opts[:verbose] = true
|
30
|
+
# end
|
31
|
+
# end
|
32
|
+
#
|
33
|
+
# CLI.parse ARGV
|
34
|
+
# p CLI.opts
|
35
|
+
#
|
36
|
+
module Parser
|
37
|
+
|
38
|
+
def self.included(klass)
|
39
|
+
@@klass = klass
|
40
|
+
@@klass.extend(self)
|
41
|
+
@@base = Clive::Command.new(true)
|
42
|
+
end
|
43
|
+
|
44
|
+
def parse(argv)
|
45
|
+
@@base.run(argv)
|
46
|
+
end
|
47
|
+
|
48
|
+
def flag(*args, &block)
|
49
|
+
@@base.flag(*args, &block)
|
50
|
+
end
|
51
|
+
|
52
|
+
def switch(*args, &block)
|
53
|
+
@@base.switch(*args, &block)
|
54
|
+
end
|
55
|
+
|
56
|
+
def command(*args, &block)
|
57
|
+
@@base.command(*args, &block)
|
58
|
+
end
|
59
|
+
|
60
|
+
def bool(*args, &block)
|
61
|
+
@@base.bool(*args, &block)
|
62
|
+
end
|
63
|
+
|
64
|
+
end
|
65
|
+
end
|
metadata
CHANGED
@@ -4,9 +4,9 @@ version: !ruby/object:Gem::Version
|
|
4
4
|
prerelease: false
|
5
5
|
segments:
|
6
6
|
- 0
|
7
|
-
-
|
8
|
-
-
|
9
|
-
version: 0.
|
7
|
+
- 6
|
8
|
+
- 1
|
9
|
+
version: 0.6.1
|
10
10
|
platform: ruby
|
11
11
|
authors:
|
12
12
|
- Joshua Hawxwell
|
@@ -14,77 +14,40 @@ autorequire:
|
|
14
14
|
bindir: bin
|
15
15
|
cert_chain: []
|
16
16
|
|
17
|
-
date: 2010-
|
17
|
+
date: 2010-12-20 00:00:00 +00:00
|
18
18
|
default_executable:
|
19
|
-
dependencies:
|
20
|
-
|
21
|
-
|
22
|
-
prerelease: false
|
23
|
-
requirement: &id001 !ruby/object:Gem::Requirement
|
24
|
-
none: false
|
25
|
-
requirements:
|
26
|
-
- - ">="
|
27
|
-
- !ruby/object:Gem::Version
|
28
|
-
segments:
|
29
|
-
- 0
|
30
|
-
version: "0"
|
31
|
-
type: :development
|
32
|
-
version_requirements: *id001
|
33
|
-
- !ruby/object:Gem::Dependency
|
34
|
-
name: yard
|
35
|
-
prerelease: false
|
36
|
-
requirement: &id002 !ruby/object:Gem::Requirement
|
37
|
-
none: false
|
38
|
-
requirements:
|
39
|
-
- - ">="
|
40
|
-
- !ruby/object:Gem::Version
|
41
|
-
segments:
|
42
|
-
- 0
|
43
|
-
version: "0"
|
44
|
-
type: :development
|
45
|
-
version_requirements: *id002
|
46
|
-
description: Clive is a DSL for creating a command line interface. It is for people who, like me, love OptionParser's syntax and love GLI's commands.
|
19
|
+
dependencies: []
|
20
|
+
|
21
|
+
description: " Clive is a DSL for creating a command line interface. It is for people \n who, like me, love OptionParser's syntax and love GLI's commands.\n"
|
47
22
|
email: m@hawx.me
|
48
23
|
executables: []
|
49
24
|
|
50
25
|
extensions: []
|
51
26
|
|
52
|
-
extra_rdoc_files:
|
53
|
-
|
54
|
-
- README.md
|
27
|
+
extra_rdoc_files: []
|
28
|
+
|
55
29
|
files:
|
56
|
-
- .document
|
57
|
-
- .gitignore
|
58
|
-
- LICENSE
|
59
30
|
- README.md
|
60
31
|
- Rakefile
|
61
|
-
-
|
62
|
-
- clive.gemspec
|
63
|
-
- lib/clive.rb
|
32
|
+
- LICENSE
|
64
33
|
- lib/clive/bool.rb
|
65
34
|
- lib/clive/command.rb
|
66
35
|
- lib/clive/exceptions.rb
|
67
36
|
- lib/clive/ext.rb
|
68
37
|
- lib/clive/flag.rb
|
69
38
|
- lib/clive/option.rb
|
39
|
+
- lib/clive/output.rb
|
40
|
+
- lib/clive/parser.rb
|
70
41
|
- lib/clive/switch.rb
|
71
42
|
- lib/clive/tokens.rb
|
72
|
-
-
|
73
|
-
|
74
|
-
- test/test_boolean.rb
|
75
|
-
- test/test_clive.rb
|
76
|
-
- test/test_command.rb
|
77
|
-
- test/test_exceptions.rb
|
78
|
-
- test/test_flag.rb
|
79
|
-
- test/test_switch.rb
|
80
|
-
- test/test_token.rb
|
81
|
-
has_rdoc: true
|
43
|
+
- lib/clive.rb
|
44
|
+
has_rdoc: false
|
82
45
|
homepage: http://github.com/hawx/clive
|
83
46
|
licenses: []
|
84
47
|
|
85
48
|
post_install_message:
|
86
|
-
rdoc_options:
|
87
|
-
|
49
|
+
rdoc_options: []
|
50
|
+
|
88
51
|
require_paths:
|
89
52
|
- lib
|
90
53
|
required_ruby_version: !ruby/object:Gem::Requirement
|
@@ -110,12 +73,5 @@ rubygems_version: 1.3.7
|
|
110
73
|
signing_key:
|
111
74
|
specification_version: 3
|
112
75
|
summary: Imagine if optparse and gli had a son called clive.
|
113
|
-
test_files:
|
114
|
-
|
115
|
-
- test/test_boolean.rb
|
116
|
-
- test/test_clive.rb
|
117
|
-
- test/test_command.rb
|
118
|
-
- test/test_exceptions.rb
|
119
|
-
- test/test_flag.rb
|
120
|
-
- test/test_switch.rb
|
121
|
-
- test/test_token.rb
|
76
|
+
test_files: []
|
77
|
+
|
data/.document
DELETED
data/.gitignore
DELETED
data/VERSION
DELETED
@@ -1 +0,0 @@
|
|
1
|
-
0.5.0
|
data/clive.gemspec
DELETED
@@ -1,78 +0,0 @@
|
|
1
|
-
# Generated by jeweler
|
2
|
-
# DO NOT EDIT THIS FILE DIRECTLY
|
3
|
-
# Instead, edit Jeweler::Tasks in Rakefile, and run the gemspec command
|
4
|
-
# -*- encoding: utf-8 -*-
|
5
|
-
|
6
|
-
Gem::Specification.new do |s|
|
7
|
-
s.name = %q{clive}
|
8
|
-
s.version = "0.5.0"
|
9
|
-
|
10
|
-
s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
|
11
|
-
s.authors = ["Joshua Hawxwell"]
|
12
|
-
s.date = %q{2010-11-16}
|
13
|
-
s.description = %q{Clive is a DSL for creating a command line interface. It is for people who, like me, love OptionParser's syntax and love GLI's commands.}
|
14
|
-
s.email = %q{m@hawx.me}
|
15
|
-
s.extra_rdoc_files = [
|
16
|
-
"LICENSE",
|
17
|
-
"README.md"
|
18
|
-
]
|
19
|
-
s.files = [
|
20
|
-
".document",
|
21
|
-
".gitignore",
|
22
|
-
"LICENSE",
|
23
|
-
"README.md",
|
24
|
-
"Rakefile",
|
25
|
-
"VERSION",
|
26
|
-
"clive.gemspec",
|
27
|
-
"lib/clive.rb",
|
28
|
-
"lib/clive/bool.rb",
|
29
|
-
"lib/clive/command.rb",
|
30
|
-
"lib/clive/exceptions.rb",
|
31
|
-
"lib/clive/ext.rb",
|
32
|
-
"lib/clive/flag.rb",
|
33
|
-
"lib/clive/option.rb",
|
34
|
-
"lib/clive/switch.rb",
|
35
|
-
"lib/clive/tokens.rb",
|
36
|
-
"test/bin_test",
|
37
|
-
"test/helper.rb",
|
38
|
-
"test/test_boolean.rb",
|
39
|
-
"test/test_clive.rb",
|
40
|
-
"test/test_command.rb",
|
41
|
-
"test/test_exceptions.rb",
|
42
|
-
"test/test_flag.rb",
|
43
|
-
"test/test_switch.rb",
|
44
|
-
"test/test_token.rb"
|
45
|
-
]
|
46
|
-
s.homepage = %q{http://github.com/hawx/clive}
|
47
|
-
s.rdoc_options = ["--charset=UTF-8"]
|
48
|
-
s.require_paths = ["lib"]
|
49
|
-
s.rubygems_version = %q{1.3.7}
|
50
|
-
s.summary = %q{Imagine if optparse and gli had a son called clive.}
|
51
|
-
s.test_files = [
|
52
|
-
"test/helper.rb",
|
53
|
-
"test/test_boolean.rb",
|
54
|
-
"test/test_clive.rb",
|
55
|
-
"test/test_command.rb",
|
56
|
-
"test/test_exceptions.rb",
|
57
|
-
"test/test_flag.rb",
|
58
|
-
"test/test_switch.rb",
|
59
|
-
"test/test_token.rb"
|
60
|
-
]
|
61
|
-
|
62
|
-
if s.respond_to? :specification_version then
|
63
|
-
current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
|
64
|
-
s.specification_version = 3
|
65
|
-
|
66
|
-
if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then
|
67
|
-
s.add_development_dependency(%q<thoughtbot-shoulda>, [">= 0"])
|
68
|
-
s.add_development_dependency(%q<yard>, [">= 0"])
|
69
|
-
else
|
70
|
-
s.add_dependency(%q<thoughtbot-shoulda>, [">= 0"])
|
71
|
-
s.add_dependency(%q<yard>, [">= 0"])
|
72
|
-
end
|
73
|
-
else
|
74
|
-
s.add_dependency(%q<thoughtbot-shoulda>, [">= 0"])
|
75
|
-
s.add_dependency(%q<yard>, [">= 0"])
|
76
|
-
end
|
77
|
-
end
|
78
|
-
|
data/test/bin_test
DELETED
@@ -1,59 +0,0 @@
|
|
1
|
-
#!/usr/bin/env ruby
|
2
|
-
$LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
|
3
|
-
require 'clive'
|
4
|
-
|
5
|
-
options = {}
|
6
|
-
c = Clive.new do
|
7
|
-
|
8
|
-
switch :v, :verbose, "Run verbosely" do
|
9
|
-
options[:verbose] = true
|
10
|
-
end
|
11
|
-
|
12
|
-
flag :t, :type, "TYPE", "Change type to TYPE" do |i|
|
13
|
-
options[:type] = i
|
14
|
-
end
|
15
|
-
|
16
|
-
flag(:long_only, "A flag with only a long") {}
|
17
|
-
|
18
|
-
flag :p, :print, "[TEXT]", "Print TEXT, defaults to 'hey'" do |i|
|
19
|
-
i ||= 'hey'
|
20
|
-
p i
|
21
|
-
end
|
22
|
-
|
23
|
-
flag(:z, :apple, "More applez") {}
|
24
|
-
|
25
|
-
command :add, "Add something" do
|
26
|
-
|
27
|
-
header "Usage: bin_test add [options]\n" +
|
28
|
-
"Use add to add things to add, which in turn can be added."
|
29
|
-
|
30
|
-
switch :f, :full, "Use full" do
|
31
|
-
options[:full] = true
|
32
|
-
end
|
33
|
-
|
34
|
-
# Use with app-name add -e
|
35
|
-
switch :e, :empty, "Use empty" do
|
36
|
-
options[:full] = false
|
37
|
-
end
|
38
|
-
|
39
|
-
footer "If the header was a little confusing, don't worry the footer thought so too."
|
40
|
-
|
41
|
-
end
|
42
|
-
|
43
|
-
command :remove, :delete, "Get rid of something" do
|
44
|
-
switch :f, :force, "Force" do
|
45
|
-
p "deleting! now"
|
46
|
-
end
|
47
|
-
end
|
48
|
-
|
49
|
-
|
50
|
-
option_missing do |name|
|
51
|
-
puts "hi, #{name}"
|
52
|
-
end
|
53
|
-
|
54
|
-
end
|
55
|
-
|
56
|
-
p ARGV
|
57
|
-
p c.parse(ARGV)
|
58
|
-
p options
|
59
|
-
|
data/test/helper.rb
DELETED
@@ -1,14 +0,0 @@
|
|
1
|
-
require 'duvet'
|
2
|
-
Duvet.start :filter => 'clive/lib'
|
3
|
-
|
4
|
-
require 'test/unit'
|
5
|
-
require 'shoulda'
|
6
|
-
require 'rr'
|
7
|
-
|
8
|
-
$LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
|
9
|
-
$LOAD_PATH.unshift(File.dirname(__FILE__))
|
10
|
-
require File.join(File.dirname(__FILE__), '..', 'lib', 'clive')
|
11
|
-
|
12
|
-
class Test::Unit::TestCase
|
13
|
-
include RR::Adapters::TestUnit
|
14
|
-
end
|
data/test/test_boolean.rb
DELETED
@@ -1,55 +0,0 @@
|
|
1
|
-
require 'helper'
|
2
|
-
|
3
|
-
class TestBoolean < Test::Unit::TestCase
|
4
|
-
|
5
|
-
context "A new boolean switch" do
|
6
|
-
|
7
|
-
setup do
|
8
|
-
@c = Clive.new do
|
9
|
-
bool(:v, :verbose, "Run verbosely") {|i| puts(i)}
|
10
|
-
end
|
11
|
-
end
|
12
|
-
|
13
|
-
should "create two switches" do
|
14
|
-
assert_equal 2, @c.bools.length
|
15
|
-
end
|
16
|
-
|
17
|
-
should "raise error when no long name given" do
|
18
|
-
assert_raise Clive::MissingLongName do
|
19
|
-
Clive::Bool.new(:v, "Run verbosely") {}
|
20
|
-
end
|
21
|
-
end
|
22
|
-
|
23
|
-
context "the true switch" do
|
24
|
-
should "have a short name" do
|
25
|
-
assert_contains @c.bools["verbose"].names, "v"
|
26
|
-
end
|
27
|
-
|
28
|
-
should "pass true to the block" do
|
29
|
-
mock($stdout).puts(true)
|
30
|
-
@c.parse(["--verbose"])
|
31
|
-
end
|
32
|
-
|
33
|
-
should "create summary" do
|
34
|
-
assert_equal "-v, --[no-]verbose Run verbosely", @c.bools["verbose"].summary(0, 0)
|
35
|
-
end
|
36
|
-
end
|
37
|
-
|
38
|
-
context "the false switch" do
|
39
|
-
should "not have short name" do
|
40
|
-
assert_does_not_contain @c.bools["no-verbose"].names, "v"
|
41
|
-
end
|
42
|
-
|
43
|
-
should "pass false to the block" do
|
44
|
-
mock($stdout).puts(false)
|
45
|
-
@c.parse(["--no-verbose"])
|
46
|
-
end
|
47
|
-
|
48
|
-
should "not create summary" do
|
49
|
-
assert_equal nil, @c.bools["no-verbose"].summary
|
50
|
-
end
|
51
|
-
end
|
52
|
-
|
53
|
-
end
|
54
|
-
|
55
|
-
end
|
data/test/test_clive.rb
DELETED
@@ -1,276 +0,0 @@
|
|
1
|
-
require 'helper'
|
2
|
-
|
3
|
-
class TestClive < Test::Unit::TestCase
|
4
|
-
|
5
|
-
should "create flag" do
|
6
|
-
c = Clive.new do
|
7
|
-
flag(:v) {}
|
8
|
-
end
|
9
|
-
assert_equal 1, c.flags.length
|
10
|
-
assert_instance_of Clive::Flag, c.flags[0]
|
11
|
-
end
|
12
|
-
|
13
|
-
should "create switch" do
|
14
|
-
c = Clive.new do
|
15
|
-
switch(:v) {}
|
16
|
-
end
|
17
|
-
assert_equal 2, c.switches.length # plus help
|
18
|
-
assert_instance_of Clive::Switch, c.switches[0]
|
19
|
-
end
|
20
|
-
|
21
|
-
should "create command" do
|
22
|
-
c = Clive.new do
|
23
|
-
command(:add) {}
|
24
|
-
end
|
25
|
-
assert_equal 1, c.commands.length
|
26
|
-
assert_instance_of Clive::Command, c.commands[0]
|
27
|
-
end
|
28
|
-
|
29
|
-
should "create boolean" do
|
30
|
-
c = Clive.new do
|
31
|
-
boolean(:verbose) {}
|
32
|
-
end
|
33
|
-
assert_equal 2, c.bools.length
|
34
|
-
assert_instance_of Clive::Bool, c.bools[0]
|
35
|
-
end
|
36
|
-
|
37
|
-
context "When parsing input" do
|
38
|
-
|
39
|
-
should "recognise flags with equals" do
|
40
|
-
opts = {}
|
41
|
-
c = Clive.new do
|
42
|
-
flag(:type) {|i| opts[:type] = i}
|
43
|
-
end
|
44
|
-
c.parse(["--type=big"])
|
45
|
-
r = {:type => 'big'}
|
46
|
-
assert_equal r, opts
|
47
|
-
end
|
48
|
-
|
49
|
-
should "recognise flags without equals" do
|
50
|
-
opts = {}
|
51
|
-
c = Clive.new do
|
52
|
-
flag(:type) {|i| opts[:type] = i}
|
53
|
-
end
|
54
|
-
c.parse(["--type", "big"])
|
55
|
-
r = {:type => 'big'}
|
56
|
-
assert_equal r, opts
|
57
|
-
end
|
58
|
-
|
59
|
-
should "recongise short flags" do
|
60
|
-
opts = {}
|
61
|
-
c = Clive.new do
|
62
|
-
flag(:t) {|i| opts[:type] = i}
|
63
|
-
end
|
64
|
-
c.parse(["-t", "big"])
|
65
|
-
r = {:type => 'big'}
|
66
|
-
assert_equal r, opts
|
67
|
-
end
|
68
|
-
|
69
|
-
should "recognise multiple flags" do
|
70
|
-
opts = {}
|
71
|
-
c = Clive.new do
|
72
|
-
flag(:type) {|i| opts[:type] = i}
|
73
|
-
flag(:lang) {|i| opts[:lang] = i}
|
74
|
-
flag(:e) {|i| opts[:e] = i}
|
75
|
-
end
|
76
|
-
c.parse(["--type=big", "--lang", "eng", "-e", "true"])
|
77
|
-
r = {:type => 'big', :lang => 'eng', :e => 'true'}
|
78
|
-
assert_equal r, opts
|
79
|
-
end
|
80
|
-
|
81
|
-
|
82
|
-
should "recognise switches" do
|
83
|
-
opts = {}
|
84
|
-
c = Clive.new do
|
85
|
-
switch(:v, :verbose) {opts[:verbose] = true}
|
86
|
-
end
|
87
|
-
c.parse(["--verbose"])
|
88
|
-
r = {:verbose => true}
|
89
|
-
assert_equal r, opts
|
90
|
-
end
|
91
|
-
|
92
|
-
should "recognise short switches" do
|
93
|
-
opts = {}
|
94
|
-
c = Clive.new do
|
95
|
-
switch(:v, :verbose) {opts[:verbose] = true}
|
96
|
-
end
|
97
|
-
c.parse(["-v"])
|
98
|
-
r = {:verbose => true}
|
99
|
-
assert_equal r, opts
|
100
|
-
end
|
101
|
-
|
102
|
-
should "recognise multiple switches" do
|
103
|
-
opts = {}
|
104
|
-
c = Clive.new do
|
105
|
-
switch(:v, :verbose) {opts[:verbose] = true}
|
106
|
-
switch(:r, :recursive) {opts[:recursive] = true}
|
107
|
-
end
|
108
|
-
c.parse(["--verbose", "-r"])
|
109
|
-
r = {:verbose => true, :recursive => true}
|
110
|
-
assert_equal r, opts
|
111
|
-
end
|
112
|
-
|
113
|
-
should "recognise multiple combined short switches" do
|
114
|
-
opts = {}
|
115
|
-
c = Clive.new do
|
116
|
-
switch(:v, :verbose) {opts[:verbose] = true}
|
117
|
-
switch(:r, :recursive) {opts[:recursive] = true}
|
118
|
-
end
|
119
|
-
c.parse(["-vr"])
|
120
|
-
r = {:verbose => true, :recursive => true}
|
121
|
-
assert_equal r, opts
|
122
|
-
end
|
123
|
-
|
124
|
-
|
125
|
-
should "recognise commands" do
|
126
|
-
opts = {}
|
127
|
-
c = Clive.new do
|
128
|
-
command(:add) {opts[:add] = true}
|
129
|
-
end
|
130
|
-
c.parse(["add"])
|
131
|
-
r = {:add => true}
|
132
|
-
assert_equal r, opts
|
133
|
-
end
|
134
|
-
|
135
|
-
should "recognise flags and switches within commands" do
|
136
|
-
opts = {}
|
137
|
-
c = Clive.new do
|
138
|
-
command(:add) {
|
139
|
-
opts[:add] = true
|
140
|
-
|
141
|
-
switch(:v, :verbose) {opts[:verbose] = true}
|
142
|
-
flag(:type) {|i| opts[:type] = i}
|
143
|
-
}
|
144
|
-
end
|
145
|
-
c.parse(["add", "--verbose", "--type=big"])
|
146
|
-
r = {:add => true, :verbose => true, :type => "big"}
|
147
|
-
assert_equal r, opts
|
148
|
-
end
|
149
|
-
|
150
|
-
should "recognise an argument" do
|
151
|
-
c = Clive.new do
|
152
|
-
bool(:v, :verbose, "Run verbosely") {}
|
153
|
-
end
|
154
|
-
args = c.parse ["argument"]
|
155
|
-
assert_equal ["argument"], args
|
156
|
-
end
|
157
|
-
|
158
|
-
should "recognise a quoted argument" do
|
159
|
-
c = Clive.new {}
|
160
|
-
args = c.parse ['a quoted argument']
|
161
|
-
assert_equal ["a quoted argument"], args
|
162
|
-
end
|
163
|
-
|
164
|
-
should "recognise multiple arguments" do
|
165
|
-
c = Clive.new do
|
166
|
-
bool(:v, :verbose, "Run verbosely") {}
|
167
|
-
end
|
168
|
-
args = c.parse ["argument", "and", "another"]
|
169
|
-
assert_equal ["argument", "and", "another"], args
|
170
|
-
end
|
171
|
-
|
172
|
-
should "parse a mixture properly" do
|
173
|
-
opts = {}
|
174
|
-
c = Clive.new do
|
175
|
-
switch(:v) {opts[:v] = true}
|
176
|
-
flag(:h) {opts[:h] = true}
|
177
|
-
command(:add) {
|
178
|
-
switch(:full) {opts[:full] = true}
|
179
|
-
}
|
180
|
-
end
|
181
|
-
c.parse(["-v", "add", "--full"])
|
182
|
-
r = {:v => true, :full => true}
|
183
|
-
assert_equal r, opts
|
184
|
-
end
|
185
|
-
|
186
|
-
|
187
|
-
should "return unused arguments" do
|
188
|
-
opts = {}
|
189
|
-
c = Clive.new do
|
190
|
-
switch(:v) {opts[:v] = true}
|
191
|
-
flag(:h) {opts[:h] = true}
|
192
|
-
command(:add) {
|
193
|
-
switch(:full) {opts[:full] = true}
|
194
|
-
}
|
195
|
-
end
|
196
|
-
result = c.parse(["-v", "add", "--full", "truearg"])
|
197
|
-
assert_equal ["truearg"], result
|
198
|
-
end
|
199
|
-
|
200
|
-
should "return multiple unused arguments" do
|
201
|
-
opts = {}
|
202
|
-
c = Clive.new do
|
203
|
-
switch(:v) {opts[:v] = true}
|
204
|
-
flag(:h) {opts[:h] = true}
|
205
|
-
command(:add) {
|
206
|
-
switch(:full) {opts[:full] = true}
|
207
|
-
}
|
208
|
-
end
|
209
|
-
result = c.parse(["-v", "onearg", "twoarg", "/usr/bin/env"])
|
210
|
-
assert_equal ["onearg", "twoarg", "/usr/bin/env"], result
|
211
|
-
end
|
212
|
-
|
213
|
-
end
|
214
|
-
|
215
|
-
|
216
|
-
context "When parsing ridiculous edge tests" do
|
217
|
-
|
218
|
-
should "parse this crazy guy" do
|
219
|
-
opts = {}
|
220
|
-
c = Clive.new do
|
221
|
-
switch(:v) {opts[:v] = true}
|
222
|
-
flag(:h) {opts[:h] = true}
|
223
|
-
|
224
|
-
command(:add) {
|
225
|
-
opts[:add] = {}
|
226
|
-
switch(:full) {opts[:add][:full] = true}
|
227
|
-
flag(:breed) {|i| opts[:add][:breed] = i}
|
228
|
-
|
229
|
-
command(:init) {
|
230
|
-
opts[:add][:init] = {}
|
231
|
-
switch(:base) {opts[:add][:init][:base] = true}
|
232
|
-
flag(:name) {|i| opts[:add][:init][:name] = i}
|
233
|
-
}
|
234
|
-
}
|
235
|
-
end
|
236
|
-
c.parse(["-v", "add", "--full", "init", "--base", "--name=Works"])
|
237
|
-
r = {:v => true, :add => {:full => true, :init => {:base => true, :name => 'Works'}} }
|
238
|
-
assert_equal r, opts
|
239
|
-
end
|
240
|
-
|
241
|
-
should "parse the one in the readme" do
|
242
|
-
opts = {}
|
243
|
-
c = Clive.new do
|
244
|
-
bool(:v, :verbose, "Run verbosely") {|i| opts[:verbose] = i}
|
245
|
-
|
246
|
-
command(:add, "Add a new project") do
|
247
|
-
opts[:add] = {}
|
248
|
-
|
249
|
-
switch(:force, "Force overwrite") {opts[:add][:force] = true}
|
250
|
-
flag(:framework, "Add framework") do |i|
|
251
|
-
opts[:add][:framework] ||= []
|
252
|
-
opts[:add][:framework] << i
|
253
|
-
end
|
254
|
-
|
255
|
-
command(:init, "Initialize the project after creating") do
|
256
|
-
switch(:m, :minimum, "Use minimum settings") {opts[:add][:min] = true}
|
257
|
-
flag(:w, :width) {|i| opts[:add][:width] = i.to_i}
|
258
|
-
end
|
259
|
-
|
260
|
-
end
|
261
|
-
|
262
|
-
switch(:version, "Show version") do
|
263
|
-
puts "1.0.0"
|
264
|
-
exit
|
265
|
-
end
|
266
|
-
end
|
267
|
-
argv = %w(-v add --framework=blueprint init -m -w 200 ~/Desktop/new_thing ~/Desktop/another_thing)
|
268
|
-
args = c.parse(argv)
|
269
|
-
|
270
|
-
opts_r = {:add => {:min => true, :width => 200, :framework => ["blueprint"]}, :verbose => true}
|
271
|
-
assert_equal opts_r, opts
|
272
|
-
assert_equal ["~/Desktop/new_thing", "~/Desktop/another_thing"], args
|
273
|
-
end
|
274
|
-
|
275
|
-
end
|
276
|
-
end
|
data/test/test_command.rb
DELETED
@@ -1,44 +0,0 @@
|
|
1
|
-
require 'helper'
|
2
|
-
|
3
|
-
class TestCommand < Test::Unit::TestCase
|
4
|
-
|
5
|
-
context "A new command" do
|
6
|
-
|
7
|
-
setup do
|
8
|
-
@c = Clive.new do
|
9
|
-
command(:add, "Add something") {puts "called!"}
|
10
|
-
end
|
11
|
-
end
|
12
|
-
|
13
|
-
context "with multiple names" do
|
14
|
-
setup do
|
15
|
-
@c = Clive.new do
|
16
|
-
command(:add, :init, :new, "Add something") {puts "called!"}
|
17
|
-
end
|
18
|
-
end
|
19
|
-
|
20
|
-
should "be called with first name" do
|
21
|
-
mock($stdout).puts("called!")
|
22
|
-
@c.parse ["add"]
|
23
|
-
end
|
24
|
-
|
25
|
-
should "be called with second name" do
|
26
|
-
mock($stdout).puts("called!")
|
27
|
-
@c.parse ["init"]
|
28
|
-
end
|
29
|
-
|
30
|
-
should "be called with third name" do
|
31
|
-
mock($stdout).puts("called!")
|
32
|
-
@c.parse ["new"]
|
33
|
-
end
|
34
|
-
end
|
35
|
-
|
36
|
-
|
37
|
-
should "only execute it's block when called" do
|
38
|
-
mock($stdout).puts("called!")
|
39
|
-
@c.parse(["a"])
|
40
|
-
@c.parse(["add"])
|
41
|
-
end
|
42
|
-
|
43
|
-
end
|
44
|
-
end
|
data/test/test_exceptions.rb
DELETED
@@ -1,28 +0,0 @@
|
|
1
|
-
require 'helper'
|
2
|
-
|
3
|
-
class TestClive < Test::Unit::TestCase
|
4
|
-
|
5
|
-
context "When given bad input" do
|
6
|
-
|
7
|
-
should "warn of missing arguments" do
|
8
|
-
c = Clive.new do
|
9
|
-
flag(:hello) {}
|
10
|
-
end
|
11
|
-
assert_raise Clive::MissingArgument do
|
12
|
-
c.parse(["--hello"])
|
13
|
-
end
|
14
|
-
end
|
15
|
-
|
16
|
-
should "warn of invalid options" do
|
17
|
-
c = Clive.new do
|
18
|
-
switch(:hello) {}
|
19
|
-
end
|
20
|
-
assert_raise Clive::InvalidOption do
|
21
|
-
c.parse(["--what"])
|
22
|
-
end
|
23
|
-
end
|
24
|
-
|
25
|
-
|
26
|
-
end
|
27
|
-
|
28
|
-
end
|
data/test/test_flag.rb
DELETED
@@ -1,160 +0,0 @@
|
|
1
|
-
require 'helper'
|
2
|
-
|
3
|
-
class TestFlag < Test::Unit::TestCase
|
4
|
-
|
5
|
-
context "A new flag" do
|
6
|
-
|
7
|
-
setup do
|
8
|
-
@c = Clive.new do
|
9
|
-
flag(:t, :type, "TEXT", "Change type to TYPE") {|i| puts(i)}
|
10
|
-
end
|
11
|
-
end
|
12
|
-
|
13
|
-
should "pass an argument to block" do
|
14
|
-
mock($stdout).puts("text")
|
15
|
-
@c.parse(["--type", "text"])
|
16
|
-
end
|
17
|
-
|
18
|
-
should "have a arguments" do
|
19
|
-
r = {:name => "TEXT", :optional => false, :type => String}
|
20
|
-
assert_equal r, @c.flags["t"].args[0]
|
21
|
-
end
|
22
|
-
|
23
|
-
should "not be optional" do
|
24
|
-
assert_equal false, @c.flags["t"].args[0][:optional]
|
25
|
-
end
|
26
|
-
end
|
27
|
-
|
28
|
-
context "A new flag with default" do
|
29
|
-
|
30
|
-
setup do
|
31
|
-
@c = Clive.new do
|
32
|
-
flag(:t, :type, "[TEXT]", "Change type to TYPE") do |i|
|
33
|
-
i ||= 'dog'
|
34
|
-
$stdout.puts(i)
|
35
|
-
end
|
36
|
-
end
|
37
|
-
end
|
38
|
-
|
39
|
-
should "have an arg name" do
|
40
|
-
r = {:name => "TEXT", :optional => true, :type => String}
|
41
|
-
assert_equal r, @c.flags["t"].args[0]
|
42
|
-
end
|
43
|
-
|
44
|
-
should "be optional" do
|
45
|
-
assert_equal true, @c.flags["t"].args[0][:optional]
|
46
|
-
end
|
47
|
-
|
48
|
-
should "pass an argument to block" do
|
49
|
-
mock($stdout).puts("text")
|
50
|
-
@c.parse(["--type", "text"])
|
51
|
-
end
|
52
|
-
|
53
|
-
should "pass nil to the block if no argument" do
|
54
|
-
mock($stdout).puts("dog")
|
55
|
-
@c.parse(["--type"])
|
56
|
-
end
|
57
|
-
end
|
58
|
-
|
59
|
-
context "A new flag with multiple arguments" do
|
60
|
-
|
61
|
-
setup do
|
62
|
-
@c = Clive.new do
|
63
|
-
flag(:s, :send, "FROM TO", "Send the message from FROM to TO") do |from, to|
|
64
|
-
puts from
|
65
|
-
puts to
|
66
|
-
end
|
67
|
-
end
|
68
|
-
end
|
69
|
-
|
70
|
-
should "pass two arguments to the block" do
|
71
|
-
mock($stdout).puts("John")
|
72
|
-
mock($stdout).puts("Dave")
|
73
|
-
@c.parse(["--send", "John", "Dave"])
|
74
|
-
end
|
75
|
-
|
76
|
-
should "have a useful summary" do
|
77
|
-
s = "-s, --send FROM TO Send the message from FROM to TO"
|
78
|
-
assert_equal s, @c.flags['s'].summary(0, 0)
|
79
|
-
end
|
80
|
-
|
81
|
-
should "work for more than two arguments" do
|
82
|
-
mock($stdout).puts("from: Josh, to: John, via: Hong Kong, because: It's far away")
|
83
|
-
c = Clive.new do
|
84
|
-
flag(:s, :send, "FROM TO VIA BECAUSE", "Send the message...etc") do |f, t, v, b|
|
85
|
-
puts "from: #{f}, to: #{t}, via: #{v}, because: #{b}"
|
86
|
-
end
|
87
|
-
end
|
88
|
-
c.parse ['-s', 'Josh', 'John', "Hong Kong", "It's far away"]
|
89
|
-
end
|
90
|
-
|
91
|
-
context "and optional arguments" do
|
92
|
-
setup do
|
93
|
-
@c = Clive.new do
|
94
|
-
flag(:s, :send, "FROM [VIA] TO [BECAUSE]", "Send the message...etc") do |f, v, t, b|
|
95
|
-
s = "from: #{f}"
|
96
|
-
s << ", via: #{v}" if v
|
97
|
-
s << ", to: #{t}"
|
98
|
-
s << ", because: #{b}" if b
|
99
|
-
puts s
|
100
|
-
end
|
101
|
-
end
|
102
|
-
end
|
103
|
-
|
104
|
-
should "correctly parse argument string" do
|
105
|
-
r = [
|
106
|
-
{:name => "FROM", :optional => false, :type => String},
|
107
|
-
{:name => "VIA", :optional => true, :type => String},
|
108
|
-
{:name => "TO", :optional => false, :type => String},
|
109
|
-
{:name => "BECAUSE", :optional => true, :type => String}
|
110
|
-
]
|
111
|
-
assert_equal r, @c.flags[:s].args
|
112
|
-
end
|
113
|
-
|
114
|
-
should "give all arguments when found in input" do
|
115
|
-
mock($stdout).puts("from: Josh, via: Hong Kong, to: John, because: It's far away")
|
116
|
-
@c.parse ["-s", "Josh", "Hong Kong", "John", "It's far away"]
|
117
|
-
end
|
118
|
-
|
119
|
-
should "try to give only needed arguments if some missing" do
|
120
|
-
mock($stdout).puts("from: Josh, to: John")
|
121
|
-
@c.parse ["-s", "Josh", "John"]
|
122
|
-
end
|
123
|
-
|
124
|
-
should "fill optional arguments starting from left" do
|
125
|
-
mock($stdout).puts("from: Josh, via: Hong Kong, to: John")
|
126
|
-
@c.parse ["-s", "Josh", "Hong Kong", "John"]
|
127
|
-
end
|
128
|
-
|
129
|
-
end
|
130
|
-
end
|
131
|
-
|
132
|
-
context "A new flag with a list of acceptable arguments" do
|
133
|
-
|
134
|
-
setup do
|
135
|
-
@c = Clive.new do
|
136
|
-
flag(:t, :type, ["large", "medium", "small"], "Choose the type to use") {}
|
137
|
-
end
|
138
|
-
end
|
139
|
-
|
140
|
-
should "raise error when correct argument not found" do
|
141
|
-
assert_raise Clive::InvalidArgument do
|
142
|
-
@c.parse(["--type", "apple"])
|
143
|
-
end
|
144
|
-
end
|
145
|
-
|
146
|
-
should "not raise error when correct argument given" do
|
147
|
-
@c.parse(["--type", "large"])
|
148
|
-
end
|
149
|
-
|
150
|
-
should "make list arguments" do
|
151
|
-
r = ["large", "medium", "small"]
|
152
|
-
assert_equal r, @c.flags['t'].args
|
153
|
-
end
|
154
|
-
|
155
|
-
should "have a useful summary" do
|
156
|
-
s = "-t, --type {large, medium, small} Choose the type to use"
|
157
|
-
assert_equal s, @c.flags['t'].summary(0, 0)
|
158
|
-
end
|
159
|
-
end
|
160
|
-
end
|
data/test/test_switch.rb
DELETED
@@ -1,18 +0,0 @@
|
|
1
|
-
require 'helper'
|
2
|
-
|
3
|
-
class TestSwitch < Test::Unit::TestCase
|
4
|
-
|
5
|
-
context "A new switch" do
|
6
|
-
|
7
|
-
setup do
|
8
|
-
@c = Clive.new do
|
9
|
-
switch(:d, :debug, "Use debug mode") {}
|
10
|
-
end
|
11
|
-
end
|
12
|
-
|
13
|
-
should "create summary" do
|
14
|
-
assert_equal "-d, --debug Use debug mode", @c.switches["d"].summary(0, 0)
|
15
|
-
end
|
16
|
-
|
17
|
-
end
|
18
|
-
end
|
data/test/test_token.rb
DELETED
@@ -1,68 +0,0 @@
|
|
1
|
-
require 'helper'
|
2
|
-
|
3
|
-
class TestToken < Test::Unit::TestCase
|
4
|
-
|
5
|
-
context "When converting" do
|
6
|
-
setup do
|
7
|
-
@array = ["add", "-al", "--verbose"]
|
8
|
-
@array_split = ["add", "-a", "-l", "--verbose"]
|
9
|
-
@tokens = [[:word, "add"], [:short, "a"], [:short, "l"], [:long, "verbose"]]
|
10
|
-
end
|
11
|
-
|
12
|
-
should "convert an array to tokens" do
|
13
|
-
assert_equal @tokens, Clive::Tokens.to_tokens(@array)
|
14
|
-
end
|
15
|
-
|
16
|
-
should "convert tokens to an array" do
|
17
|
-
assert_equal @array_split, Clive::Tokens.to_array(@tokens)
|
18
|
-
end
|
19
|
-
end
|
20
|
-
|
21
|
-
context "A new tokens instance" do
|
22
|
-
|
23
|
-
setup do
|
24
|
-
@array = ["add", "-al", "--verbose"]
|
25
|
-
@array_split = ["add", "-a", "-l", "--verbose"]
|
26
|
-
@tokens = [[:word, "add"], [:short, "a"], [:short, "l"], [:long, "verbose"]]
|
27
|
-
end
|
28
|
-
|
29
|
-
should "be created from an array" do
|
30
|
-
t = Clive::Tokens.new(@tokens)
|
31
|
-
assert_equal @tokens, t.tokens
|
32
|
-
assert_equal @array_split, t.array
|
33
|
-
end
|
34
|
-
|
35
|
-
should "be created from tokens" do
|
36
|
-
t = Clive::Tokens.new(@array)
|
37
|
-
assert_equal @tokens, t.tokens
|
38
|
-
assert_equal @array_split, t.array
|
39
|
-
end
|
40
|
-
|
41
|
-
should "tell whether an array is a token" do
|
42
|
-
t = Clive::Tokens.new
|
43
|
-
assert_equal false, t.token?(["a", "normal", "array"])
|
44
|
-
assert_equal true, t.token?([:word, "help"])
|
45
|
-
end
|
46
|
-
|
47
|
-
should "turn token to string" do
|
48
|
-
t = Clive::Tokens.new
|
49
|
-
assert_equal "-v", t.token_to_string([:short, "v"])
|
50
|
-
assert_equal "--verbose", t.token_to_string([:long, "verbose"])
|
51
|
-
assert_equal "word", t.token_to_string([:word, "word"])
|
52
|
-
end
|
53
|
-
|
54
|
-
should "add new string values" do
|
55
|
-
t = Clive::Tokens.new
|
56
|
-
t << "add" << "-al" << "--verbose"
|
57
|
-
assert_equal @tokens, t.tokens
|
58
|
-
end
|
59
|
-
|
60
|
-
should "add new token values" do
|
61
|
-
t = Clive::Tokens.new
|
62
|
-
t << [:word, "add"] << [:short, "a"] << [:short, "l"] << [:long, "verbose"]
|
63
|
-
assert_equal @array_split, t.array
|
64
|
-
end
|
65
|
-
|
66
|
-
end
|
67
|
-
|
68
|
-
end
|