clip 0.0.2 → 0.0.3
Sign up to get free protection for your applications and to get access to all the features.
- data/README.txt +16 -3
- data/lib/clip.rb +25 -2
- data/spec/clip_spec.rb +23 -0
- metadata +3 -3
data/README.txt
CHANGED
@@ -3,9 +3,9 @@
|
|
3
3
|
== DESCRIPTION:
|
4
4
|
|
5
5
|
Yeah yeah yeah. Why in heaven's name do we need yet another
|
6
|
-
command-line parser? Well, OptionParser is all well and good, but
|
7
|
-
doesn't grease the skids as much as I'd like.
|
8
|
-
|
6
|
+
command-line parser? Well, OptionParser is all well and good[1], but
|
7
|
+
doesn't grease the skids as much as I'd like. Simple things should be
|
8
|
+
dead simple (1 LOC), and more flexibility is there if you need it.
|
9
9
|
|
10
10
|
Cheers!
|
11
11
|
|
@@ -68,6 +68,19 @@ a named option or flag. Whatever remains on the command line that doesn't fit
|
|
68
68
|
either a flag or an option/value pair will be made available via the
|
69
69
|
<tt>remainder</tt> method of the returned object.
|
70
70
|
|
71
|
+
Sometimes even passing a block is overkill. Say you want to grab just
|
72
|
+
a hash from a set of name/value argument pairs provided:
|
73
|
+
|
74
|
+
$ my_clip_script subcommand -c config.yml # Allows:
|
75
|
+
Clip.hash == { 'c' => 'config.yml' }
|
76
|
+
|
77
|
+
$ my_clip_script -c config.yml --mode optimistic # Allows:
|
78
|
+
Clip.hash == { 'c' => 'config.yml', 'mode' => 'optimistic' }
|
79
|
+
|
80
|
+
----------------------------------------
|
81
|
+
|
82
|
+
[1] - Not really.
|
83
|
+
|
71
84
|
== LICENSE:
|
72
85
|
|
73
86
|
(The MIT License)
|
data/lib/clip.rb
CHANGED
@@ -13,7 +13,7 @@ def Clip(args=ARGV)
|
|
13
13
|
end
|
14
14
|
|
15
15
|
module Clip
|
16
|
-
VERSION = "0.0.
|
16
|
+
VERSION = "0.0.3"
|
17
17
|
|
18
18
|
##
|
19
19
|
# Indicates that the parser was incorrectly configured in the
|
@@ -338,4 +338,27 @@ module Clip
|
|
338
338
|
sprintf('-%-2s --%-10s %s', @short, @long, @description)
|
339
339
|
end
|
340
340
|
end
|
341
|
-
|
341
|
+
|
342
|
+
HASHER_REGEX = /^--?\w+/
|
343
|
+
##
|
344
|
+
# Turns ARGV into a hash.
|
345
|
+
#
|
346
|
+
# my_clip_script -c config.yml # Clip.hash == { 'c' => 'config.yml' }
|
347
|
+
# my_clip_script command -c config.yml # Clip.hash == { 'c' => 'config.yml' }
|
348
|
+
# my_clip_script com -c config.yml -d # Clip.hash == { 'c' => 'config.yml' }
|
349
|
+
# my_clip_script -c config.yml --mode optimistic
|
350
|
+
# # Clip.hash == { 'c' => 'config.yml', 'mode' => 'optimistic' }
|
351
|
+
def self.hash(argv = ARGV.dup, values = [])
|
352
|
+
@hash ||= begin
|
353
|
+
argv.shift until argv.first =~ HASHER_REGEX
|
354
|
+
while argv.first =~ HASHER_REGEX and argv.size >= 2 do
|
355
|
+
values += [argv.shift.sub(/^--?/, ''), argv.shift]
|
356
|
+
end
|
357
|
+
Hash[*values]
|
358
|
+
end
|
359
|
+
end
|
360
|
+
|
361
|
+
##
|
362
|
+
# Clear the cached hash value. Probably only useful for tests, but whatever.
|
363
|
+
def Clip.reset_hash!; @hash = nil end
|
364
|
+
end
|
data/spec/clip_spec.rb
CHANGED
@@ -288,4 +288,27 @@ describe Clip do
|
|
288
288
|
opts.value.should == 123
|
289
289
|
end
|
290
290
|
end
|
291
|
+
|
292
|
+
describe "when parsing ARGV as a hash" do
|
293
|
+
setup { Clip.reset_hash! }
|
294
|
+
|
295
|
+
it "should make sense of '-c my_config.yml'" do
|
296
|
+
Clip.hash(['-c', 'config.yml']).should == { 'c' => 'config.yml' }
|
297
|
+
end
|
298
|
+
|
299
|
+
it "should only use pairs of dash + value args" do
|
300
|
+
Clip.hash(['-c', 'config.yml',
|
301
|
+
'-d']).should == { 'c' => 'config.yml' }
|
302
|
+
end
|
303
|
+
|
304
|
+
it "should ignore leading/trailing non-dashed arguments" do
|
305
|
+
Clip.hash(['subcommand', '-c', 'config.yml',
|
306
|
+
'do']).should == { 'c' => 'config.yml' }
|
307
|
+
end
|
308
|
+
|
309
|
+
it "should allow -s (short) or --long arguments" do
|
310
|
+
Clip.hash(['-c', 'config.yml', '--mode', 'optimistic']).
|
311
|
+
should == { 'c' => 'config.yml', 'mode' => 'optimistic' }
|
312
|
+
end
|
313
|
+
end
|
291
314
|
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: clip
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.3
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Alex Vollmer
|
@@ -9,7 +9,7 @@ autorequire:
|
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
11
|
|
12
|
-
date: 2008-05
|
12
|
+
date: 2008-06-05 00:00:00 -07:00
|
13
13
|
default_executable:
|
14
14
|
dependencies:
|
15
15
|
- !ruby/object:Gem::Dependency
|
@@ -60,7 +60,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
60
60
|
requirements: []
|
61
61
|
|
62
62
|
rubyforge_project: clip
|
63
|
-
rubygems_version: 1.1.
|
63
|
+
rubygems_version: 1.1.1
|
64
64
|
signing_key:
|
65
65
|
specification_version: 2
|
66
66
|
summary: Command-line parsing made short and sweet
|