clio 0.2.0 → 0.3.0
Sign up to get free protection for your applications and to get access to all the features.
- data/CHANGES +19 -1
- data/MANIFEST +2 -1
- data/README +5 -6
- data/RELEASE +10 -5
- data/VERSION +1 -1
- data/lib/clio/commandline.rb +2 -9
- data/lib/clio/string.rb +91 -32
- data/lib/clio/usage.rb +2 -2
- data/lib/clio/usage/argument.rb +21 -19
- data/lib/clio/usage/command.rb +76 -351
- data/lib/clio/usage/option.rb +1 -1
- data/lib/clio/usage/subcommand.rb +455 -0
- data/spec/commandline/autousage.rd +0 -2
- data/spec/commandline/bracket.rd +0 -1
- data/spec/commandline/completion.rd +0 -2
- data/spec/commandline/define.rd +0 -1
- data/spec/commandline/method.rd +0 -1
- data/spec/commandline/parse.rd +0 -1
- data/spec/commandline/scenario.rd +0 -1
- data/spec/commandline/subclass.rd +3 -1
- data/spec/string/unit.rd +16 -1
- data/spec/usage/define.rd +85 -17
- data/spec/usage/parse.rd +0 -2
- metadata +4 -4
- data/lib/clio/usage/main.rb +0 -165
data/spec/commandline/bracket.rd
CHANGED
data/spec/commandline/define.rd
CHANGED
data/spec/commandline/method.rd
CHANGED
data/spec/commandline/parse.rd
CHANGED
@@ -3,6 +3,9 @@
|
|
3
3
|
Require clio/commandline.rb.
|
4
4
|
|
5
5
|
require 'clio/commandline'
|
6
|
+
|
7
|
+
Fake a commandline command.
|
8
|
+
|
6
9
|
$0 = 'c0'
|
7
10
|
|
8
11
|
Create new subclass of Clio::Commandline.
|
@@ -51,4 +54,3 @@ Method missing options work.
|
|
51
54
|
@cli2.o1?.assert == true
|
52
55
|
@cli2.o2?.assert == true
|
53
56
|
|
54
|
-
QED.
|
data/spec/string/unit.rd
CHANGED
@@ -50,9 +50,24 @@ string range index
|
|
50
50
|
e = @s1[0..3]
|
51
51
|
e.assert == r.to_s
|
52
52
|
|
53
|
+
String addition
|
54
|
+
|
55
|
+
s1 = Clio.string("a").red
|
56
|
+
s2 = Clio.string("b").blue
|
57
|
+
s = s1 + s2
|
58
|
+
s.to_s.assert == Clio::ANSICode.red('a') + Clio::ANSICode.blue('b')
|
59
|
+
|
53
60
|
Method #sub can replace a substring.
|
54
61
|
|
55
62
|
r = @c1.sub('Hi', 'Hello')
|
63
|
+
r.to_s.assert == "Hello how are you."
|
64
|
+
|
65
|
+
Method #gsub! can replace many substrings.
|
56
66
|
|
57
|
-
|
67
|
+
s1 = Clio.string("axax").red
|
68
|
+
s2 = Clio.string("axax").blue
|
69
|
+
s = s1 + s2
|
70
|
+
s.to_s.assert == Clio::ANSICode.red('axax') + Clio::ANSICode.blue('axax')
|
71
|
+
s.gsub!('x', 'y')
|
72
|
+
s.to_s.assert == Clio::ANSICode.red('ayay') + Clio::ANSICode.blue('ayay')
|
58
73
|
|
data/spec/usage/define.rd
CHANGED
@@ -1,44 +1,112 @@
|
|
1
|
-
= Usage
|
1
|
+
= Clio::Usage
|
2
2
|
|
3
|
-
|
3
|
+
== Create a Usage object.
|
4
|
+
|
5
|
+
Require usage library.
|
4
6
|
|
5
7
|
require 'clio/usage'
|
8
|
+
|
9
|
+
Fake a commandline invocation.
|
10
|
+
|
6
11
|
$0 = 'test'
|
7
12
|
|
8
13
|
Create a Usage object.
|
9
14
|
|
10
15
|
usage = Clio::Usage.new
|
11
16
|
|
12
|
-
|
17
|
+
== Basic Notation
|
18
|
+
|
19
|
+
Define a usage subcommand using #subcommand.
|
13
20
|
|
14
21
|
usage = Clio::Usage.new
|
15
|
-
usage.
|
22
|
+
usage.subcommand('foo')
|
16
23
|
usage.to_s.assert == 'test foo'
|
17
24
|
|
18
|
-
|
25
|
+
This is also aliased as #command.
|
19
26
|
|
20
|
-
|
21
|
-
|
22
|
-
|
27
|
+
usage = Clio::Usage.new
|
28
|
+
usage.command('foo')
|
29
|
+
usage.to_s.assert == 'test foo'
|
23
30
|
|
24
|
-
|
31
|
+
A toplevel switch/option is defined with #option.
|
25
32
|
|
26
33
|
usage = Clio::Usage.new
|
27
|
-
usage.option('verbose
|
34
|
+
usage.option('verbose')
|
28
35
|
usage.to_s.assert == 'test [--verbose]'
|
29
36
|
|
30
|
-
|
37
|
+
Options can also have aliases.
|
31
38
|
|
32
39
|
usage = Clio::Usage.new
|
33
|
-
usage.option(:verbose
|
40
|
+
usage.option(:verbose, :v)
|
34
41
|
usage.to_s.assert == 'test [-v --verbose]'
|
35
42
|
|
36
|
-
|
43
|
+
Arguments are defined with #argument, which takes
|
44
|
+
a type, or name and type. Multiple arguments can
|
45
|
+
be defined, and are processed in the order defined.
|
37
46
|
|
38
47
|
usage = Clio::Usage.new
|
39
|
-
usage.
|
40
|
-
usage.
|
41
|
-
usage.to_s.assert == 'test
|
48
|
+
usage.argument('TYPE1')
|
49
|
+
usage.argument('name:TYPE2')
|
50
|
+
usage.to_s.assert == 'test <TYPE1> <name:TYPE2>'
|
51
|
+
|
52
|
+
But arguments can be index to sepcify the index. Argument
|
53
|
+
index start with 1 (not 0).
|
54
|
+
|
55
|
+
usage = Clio::Usage.new
|
56
|
+
usage.argument(1, 'TYPE1')
|
57
|
+
usage.argument(2, 'name:TYPE2')
|
58
|
+
usage.to_s.assert == 'test <TYPE1> <name:TYPE2>'
|
59
|
+
|
60
|
+
Final arguments can consume all remaining arguments.
|
61
|
+
Define this with #splat.
|
62
|
+
|
63
|
+
usage = Clio::Usage.new
|
64
|
+
usage.argument('TYPE').splat(true)
|
65
|
+
usage.to_s.assert == 'test <TYPE...>'
|
66
|
+
|
67
|
+
Usage cannot have both subcommands and arguments.
|
68
|
+
|
69
|
+
usage = Clio::Usage.new
|
70
|
+
expect ArgumentError do
|
71
|
+
usage.subcommand('foo')
|
72
|
+
usage.argument('bar')
|
73
|
+
end
|
74
|
+
|
75
|
+
Anything define on a Usage object, can also be
|
76
|
+
defined for a subcommand, since Usage.new actually
|
77
|
+
returns and subclass of Subcommand.
|
78
|
+
|
79
|
+
usage = Clio::Usage.new
|
80
|
+
usage.class.assert < Clio::Usage::Subcommand
|
81
|
+
|
82
|
+
Bringing it all together in a .
|
83
|
+
|
84
|
+
usage = Clio::Usage.new
|
85
|
+
usage.option(:verbose, :v)
|
86
|
+
usage.command('foo').argument('TYPE')
|
87
|
+
usage.to_s.assert == 'test [-v --verbose] foo <TYPE>'
|
88
|
+
|
89
|
+
== Bracket Notation
|
90
|
+
|
91
|
+
Subcommand, options and arguments can also be defined using #[].
|
92
|
+
They are distinighished by syntax used. For instance, to define
|
93
|
+
a subcommand, use bare syntax.
|
94
|
+
|
95
|
+
usage = Clio::Usage.new
|
96
|
+
usage['foo']
|
97
|
+
usage.to_s.assert == 'test foo'
|
98
|
+
|
99
|
+
To define an option prefix the syntax with '--' or '-', and
|
100
|
+
aliases are separated by spaces.
|
101
|
+
|
102
|
+
usage = Clio::Usage.new
|
103
|
+
usage['--verbose -v']
|
104
|
+
usage.to_s.assert == 'test [-v --verbose]'
|
105
|
+
|
106
|
+
Arguments are defined by wrapping the type or name:type in < > brackets.
|
107
|
+
|
108
|
+
usage = Clio::Usage.new
|
109
|
+
usage['<TYPE>']
|
110
|
+
usage.to_s.assert == 'test <TYPE>'
|
42
111
|
|
43
|
-
QED.
|
44
112
|
|
data/spec/usage/parse.rd
CHANGED
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: clio
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.3.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- 7rans <transfire@gmail.com>
|
@@ -9,7 +9,7 @@ autorequire:
|
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
11
|
|
12
|
-
date: 2008-11-
|
12
|
+
date: 2008-11-19 00:00:00 -05:00
|
13
13
|
default_executable:
|
14
14
|
dependencies: []
|
15
15
|
|
@@ -31,6 +31,7 @@ files:
|
|
31
31
|
- lib
|
32
32
|
- meta
|
33
33
|
- spec
|
34
|
+
- MANIFEST
|
34
35
|
- CHANGES
|
35
36
|
- RELEASE
|
36
37
|
- README
|
@@ -52,11 +53,11 @@ files:
|
|
52
53
|
- lib/clio/string.rb
|
53
54
|
- lib/clio/facets/kernel.rb
|
54
55
|
- lib/clio/facets/string.rb
|
55
|
-
- lib/clio/usage/main.rb
|
56
56
|
- lib/clio/usage/signature.rb
|
57
57
|
- lib/clio/usage/command.rb
|
58
58
|
- lib/clio/usage/parser.rb
|
59
59
|
- lib/clio/usage/argument.rb
|
60
|
+
- lib/clio/usage/subcommand.rb
|
60
61
|
- lib/clio/usage/interface.rb
|
61
62
|
- lib/clio/usage/option.rb
|
62
63
|
- lib/clio/layout/list.rb
|
@@ -87,7 +88,6 @@ files:
|
|
87
88
|
- spec/commandline/parse.rd
|
88
89
|
- spec/commandline/scenario.rd
|
89
90
|
- spec/string/unit.rd
|
90
|
-
- MANIFEST
|
91
91
|
has_rdoc: true
|
92
92
|
homepage: http://clio.rubyforge.org
|
93
93
|
post_install_message:
|
data/lib/clio/usage/main.rb
DELETED
@@ -1,165 +0,0 @@
|
|
1
|
-
require 'clio/usage/command'
|
2
|
-
|
3
|
-
module Clio
|
4
|
-
|
5
|
-
module Usage #:nodoc:
|
6
|
-
|
7
|
-
# = Main
|
8
|
-
#
|
9
|
-
# Main specifies the toplevel "Command".
|
10
|
-
#
|
11
|
-
class Main < Command
|
12
|
-
|
13
|
-
# New Usage.
|
14
|
-
#
|
15
|
-
def initialize(name=nil, &block)
|
16
|
-
name ||= File.basename($0)
|
17
|
-
super(name, &block)
|
18
|
-
end
|
19
|
-
|
20
|
-
# Define a command.
|
21
|
-
#
|
22
|
-
# command('remote')
|
23
|
-
# command('remote','add')
|
24
|
-
#
|
25
|
-
#def command(name, &block)
|
26
|
-
# raise "Command cannot have both arguments and subcommands (eg. #{name})." unless arguments.empty?
|
27
|
-
# key = name.to_s.strip
|
28
|
-
# if cmd = @commands.find{|c| c === key}
|
29
|
-
# else
|
30
|
-
# cmd = Command.new(key, self)
|
31
|
-
# @commands << cmd
|
32
|
-
# end
|
33
|
-
# cmd.instance_eval(&block) if block
|
34
|
-
# cmd
|
35
|
-
#end
|
36
|
-
|
37
|
-
=begin
|
38
|
-
#alias_method :[], :command
|
39
|
-
|
40
|
-
|
41
|
-
# ARRAY NOTATION
|
42
|
-
#-------------------------------------------------------------
|
43
|
-
|
44
|
-
#
|
45
|
-
def [](*args)
|
46
|
-
res = nil
|
47
|
-
head, *tail = *args
|
48
|
-
case head.to_s
|
49
|
-
when /^-/
|
50
|
-
x = []
|
51
|
-
opts = args.map do |o|
|
52
|
-
o = o.to_s
|
53
|
-
if i = o.index('=')
|
54
|
-
x << o[i+1..-1]
|
55
|
-
o[0...i]
|
56
|
-
else
|
57
|
-
o
|
58
|
-
end
|
59
|
-
end
|
60
|
-
x = x.uniq
|
61
|
-
|
62
|
-
res = opt(*opts)
|
63
|
-
else
|
64
|
-
args.each do |name|
|
65
|
-
res = command(name)
|
66
|
-
end
|
67
|
-
end
|
68
|
-
return res
|
69
|
-
end
|
70
|
-
=end
|
71
|
-
|
72
|
-
=begin
|
73
|
-
# Usage text.
|
74
|
-
#
|
75
|
-
def to_s
|
76
|
-
#s = [full_name]
|
77
|
-
s = [name]
|
78
|
-
|
79
|
-
case options.size
|
80
|
-
when 0
|
81
|
-
when 1, 2, 3
|
82
|
-
s.concat(options.collect{ |o| "[#{o.to_s.strip}]" })
|
83
|
-
else
|
84
|
-
s << "[switches]"
|
85
|
-
end
|
86
|
-
# switches? vs. options
|
87
|
-
s << arguments.join(' ') unless arguments.empty?
|
88
|
-
|
89
|
-
case commands.size
|
90
|
-
when 0
|
91
|
-
when 1
|
92
|
-
s << commands.join('')
|
93
|
-
when 2, 3
|
94
|
-
s << '[' + commands.join(' | ') + ']'
|
95
|
-
else
|
96
|
-
s << 'command'
|
97
|
-
end
|
98
|
-
|
99
|
-
s.flatten.join(' ')
|
100
|
-
end
|
101
|
-
|
102
|
-
# Help text.
|
103
|
-
#
|
104
|
-
def to_s_help
|
105
|
-
s = []
|
106
|
-
unless help.empty?
|
107
|
-
s << help
|
108
|
-
s << ''
|
109
|
-
end
|
110
|
-
s << "Usage:"
|
111
|
-
s << " " + to_s
|
112
|
-
unless commands.empty?
|
113
|
-
s << ''
|
114
|
-
s << 'Commands:'
|
115
|
-
s.concat(commands.collect{ |x| " %-20s %s" % [x.key, x.help] }.sort)
|
116
|
-
end
|
117
|
-
unless arguments.empty?
|
118
|
-
s << ''
|
119
|
-
s << "Arguments:"
|
120
|
-
s.concat(arguments.collect{ |x| " %-20s %s" % [x, x.help] })
|
121
|
-
end
|
122
|
-
unless options.empty?
|
123
|
-
s << ''
|
124
|
-
s << 'Switches:'
|
125
|
-
s.concat(options.collect{ |x| " %-20s %s" % [x, x.help] })
|
126
|
-
end
|
127
|
-
s.flatten.join("\n")
|
128
|
-
end
|
129
|
-
=end
|
130
|
-
|
131
|
-
def parse(argv)
|
132
|
-
Parser.new(self, argv).parse #(argv)
|
133
|
-
end
|
134
|
-
|
135
|
-
# Cache usage into a per-user cache file for reuse.
|
136
|
-
# This can be used to greatly speed up tab completion.
|
137
|
-
#
|
138
|
-
def cache
|
139
|
-
File.open(cache_file, 'w'){ |f| f << to_yaml }
|
140
|
-
end
|
141
|
-
|
142
|
-
private
|
143
|
-
|
144
|
-
# TODO: Use XDG
|
145
|
-
|
146
|
-
def cache_file
|
147
|
-
File.join(File.expand_path('~'), '.cache', 'clio', "#{name}.yaml")
|
148
|
-
end
|
149
|
-
|
150
|
-
def self.cache_file
|
151
|
-
File.join(File.expand_path('~'), '.cache', 'clio', "#{name}.yaml")
|
152
|
-
end
|
153
|
-
|
154
|
-
def self.load_cache
|
155
|
-
if File.file?(cache_file)
|
156
|
-
YAML.load(File.new(cache_file))
|
157
|
-
end
|
158
|
-
end
|
159
|
-
|
160
|
-
end#class Main
|
161
|
-
|
162
|
-
end#module Usage
|
163
|
-
|
164
|
-
end#module Clio
|
165
|
-
|