clap 0.0.1 → 0.0.2
Sign up to get free protection for your applications and to get access to all the features.
- data/README.markdown +3 -3
- data/clap.gemspec +1 -1
- data/lib/clap.rb +16 -5
- data/test/clap_test.rb +11 -1
- metadata +3 -3
data/README.markdown
CHANGED
@@ -16,7 +16,7 @@ Usage
|
|
16
16
|
Using Clap is simple: just pass `ARGV` and a hash of flags, and it will extract
|
17
17
|
the arguments as needed.
|
18
18
|
|
19
|
-
Clap.run
|
19
|
+
Clap.run ARGV,
|
20
20
|
"-a" => lambda { |param| ... },
|
21
21
|
"-b" => lambda { ... }
|
22
22
|
|
@@ -26,7 +26,7 @@ an array of strings.
|
|
26
26
|
If you want your command line application to require a file or display a
|
27
27
|
version number, you can configure it like this:
|
28
28
|
|
29
|
-
Clap.run
|
29
|
+
Clap.run %w(-r foo -v),
|
30
30
|
"-r" => lambda { |file| require file },
|
31
31
|
"-v" => lambda { puts VERSION }
|
32
32
|
|
@@ -37,7 +37,7 @@ of the passed lambda.
|
|
37
37
|
Another example, for an application that takes a `-v` flag and also a list of
|
38
38
|
files:
|
39
39
|
|
40
|
-
files = Clap.run
|
40
|
+
files = Clap.run %w(-v foo bar),
|
41
41
|
"-v" => lambda { puts VERSION }
|
42
42
|
|
43
43
|
files == %w(foo bar)
|
data/clap.gemspec
CHANGED
@@ -1,6 +1,6 @@
|
|
1
1
|
Gem::Specification.new do |s|
|
2
2
|
s.name = "clap"
|
3
|
-
s.version = "0.0.
|
3
|
+
s.version = "0.0.2"
|
4
4
|
s.summary = "Command line argument parsing for simple applications."
|
5
5
|
s.description = "Clap is a small library that can be bundled with your command line application. It covers the simple case of executing code based on the flags or parameters passed, and it does so with just under 30 lines of code."
|
6
6
|
s.authors = ["Michel Martens"]
|
data/lib/clap.rb
CHANGED
@@ -1,5 +1,5 @@
|
|
1
1
|
class Clap
|
2
|
-
VERSION = "0.0.
|
2
|
+
VERSION = "0.0.2"
|
3
3
|
|
4
4
|
attr :argv
|
5
5
|
attr :opts
|
@@ -9,7 +9,7 @@ class Clap
|
|
9
9
|
end
|
10
10
|
|
11
11
|
def initialize(argv, opts)
|
12
|
-
@argv = argv.
|
12
|
+
@argv = argv.dup
|
13
13
|
@opts = opts
|
14
14
|
end
|
15
15
|
|
@@ -17,13 +17,24 @@ class Clap
|
|
17
17
|
args = []
|
18
18
|
|
19
19
|
while argv.any?
|
20
|
-
item = argv.pop
|
21
20
|
|
22
|
-
|
21
|
+
item = argv.shift
|
22
|
+
flag = opts[item]
|
23
|
+
|
24
|
+
if flag
|
25
|
+
|
26
|
+
# Work around lambda semantics in 1.8.7.
|
27
|
+
arity = [flag.arity, 0].max
|
28
|
+
|
29
|
+
# Raise if there are not enough parameters
|
30
|
+
# available for the flag.
|
31
|
+
if argv.size < arity
|
32
|
+
raise ArgumentError
|
33
|
+
end
|
23
34
|
|
24
35
|
# Call the lambda with N items from argv,
|
25
36
|
# where N is the lambda's arity.
|
26
|
-
|
37
|
+
flag.call(*argv.shift(arity))
|
27
38
|
else
|
28
39
|
|
29
40
|
# Collect the items that don't correspond to
|
data/test/clap_test.rb
CHANGED
@@ -1,10 +1,15 @@
|
|
1
1
|
require File.expand_path("../lib/clap", File.dirname(__FILE__))
|
2
2
|
|
3
|
-
test "flag with argument" do
|
3
|
+
test "flag with one argument" do
|
4
4
|
result = Clap.run %w(-x y), "-x" => lambda { |flag| assert flag == "y" }
|
5
5
|
assert result == []
|
6
6
|
end
|
7
7
|
|
8
|
+
test "flag with more than one argument" do
|
9
|
+
result = Clap.run %w(-x y z), "-x" => lambda { |y, z| assert [y, z] == %w(y z) }
|
10
|
+
assert result == []
|
11
|
+
end
|
12
|
+
|
8
13
|
test "flag with wrong number of arguments" do
|
9
14
|
assert_raise(ArgumentError) do
|
10
15
|
Clap.run %w(-x), "-x" => lambda { |flag| }
|
@@ -16,6 +21,11 @@ test "extract flags with parameters" do
|
|
16
21
|
assert result == %w(a b c)
|
17
22
|
end
|
18
23
|
|
24
|
+
test "extract flags with zero arity" do
|
25
|
+
result = Clap.run %w(a b -x c), "-x" => lambda { || }
|
26
|
+
assert result == %w(a b c)
|
27
|
+
end
|
28
|
+
|
19
29
|
test "extract flags with no parameters" do
|
20
30
|
result = Clap.run %w(a b -x c), "-x" => lambda {}
|
21
31
|
assert result == %w(a b c)
|
metadata
CHANGED
@@ -5,8 +5,8 @@ version: !ruby/object:Gem::Version
|
|
5
5
|
segments:
|
6
6
|
- 0
|
7
7
|
- 0
|
8
|
-
-
|
9
|
-
version: 0.0.
|
8
|
+
- 2
|
9
|
+
version: 0.0.2
|
10
10
|
platform: ruby
|
11
11
|
authors:
|
12
12
|
- Michel Martens
|
@@ -14,7 +14,7 @@ autorequire:
|
|
14
14
|
bindir: bin
|
15
15
|
cert_chain: []
|
16
16
|
|
17
|
-
date: 2010-10-
|
17
|
+
date: 2010-10-14 00:00:00 -03:00
|
18
18
|
default_executable:
|
19
19
|
dependencies:
|
20
20
|
- !ruby/object:Gem::Dependency
|