public
Rubygem
Fork of alexvollmer/clip
Description: Yet another ruby command-line parser
Homepage: http://blog.livollmers.net
Clone URL: git://github.com/computorist/clip.git
Tighten up validation of options.
short options have to be a single word-character and not "-"
long options have to start with a word-charater
Travis Hume (author)
Fri Jul 11 11:30:28 -0700 2008
commit  bf6f882662cf106ca743bb366a5b97f63c1a4514
tree    df273518136ffff78dc98160070882ada33d129e
parent  ac157ac63e86da76ecd0dd8e21bca73037b0b087
...
57
58
59
 
 
60
61
62
63
64
65
...
104
105
106
107
108
109
110
111
 
 
112
113
114
...
245
246
247
 
 
 
 
 
 
 
 
 
 
 
 
248
249
250
...
57
58
59
60
61
62
63
 
64
65
66
...
105
106
107
 
 
 
108
109
110
111
112
113
114
...
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
0
@@ -57,9 +57,10 @@ module Clip
0
     # comma-separated value can be specified which will then be broken up into
0
     # separate tokens.
0
     def optional(short, long, options={}, &block)
0
+ check_args(short, long)
0
+
0
       short = short.to_sym
0
       long = long.gsub('-', '_').to_sym
0
- check_args(short, long)
0
 
0
       var_name = "@#{long}".to_sym
0
       if block
0
@@ -104,11 +105,10 @@ module Clip
0
     # Valid options are:
0
     # * <tt>desc</tt>: Descriptive text for the flag
0
     def flag(short, long, options={})
0
- short = short.to_sym
0
- long = long.gsub('-', '_').to_sym
0
-
0
       check_args(short, long)
0
 
0
+ short = short.to_sym
0
+ long = long.gsub('-', '_').to_sym
0
       eval <<-EOF
0
         def flag_#{long}
0
           @#{long} = true
0
@@ -245,6 +245,18 @@ module Clip
0
 
0
     private
0
     def check_args(short, long)
0
+ if short.size != 1
0
+ raise IllegalConfiguration.new("Short options must be a single character.")
0
+ end
0
+
0
+ if short !~ /[\w]+/
0
+ raise IllegalConfiguration.new("Illegal option: #{short}. Option names can only use [a-zA-Z_-]")
0
+ end
0
+
0
+ if long !~ /\A\w[\w-]+\z/
0
+ raise IllegalConfiguration.new("Illegal option: #{long}'. Parameter names can only use [a-zA-Z_-]")
0
+ end
0
+
0
       short = short.to_sym
0
       long = long.to_sym
0
 
...
241
242
243
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
244
245
246
...
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
0
@@ -241,6 +241,26 @@ describe Clip do
0
       end.should raise_error(Clip::IllegalConfiguration)
0
     end
0
 
0
+ it "should reject '-' as a short option" do
0
+ misconfig_parser { |c| c.flag '-', 'foo' }
0
+ end
0
+
0
+ it "should reject long parameters that do not start with a word character" do
0
+ misconfig_parser { |c| c.optional 'o', '-foo' }
0
+ misconfig_parser { |c| c.optional 'o', '-' }
0
+ end
0
+
0
+ it "should reject options with non-word characters '-' ok" do
0
+ misconfig_parser { |c| c.flag '#', 'count' }
0
+ misconfig_parser { |c| c.flag 'c', 'bang!' }
0
+ misconfig_parser { |c| c.optional '#', 'count' }
0
+ misconfig_parser { |c| c.optional 'c', 'bang!' }
0
+ end
0
+
0
+ it "should reject short params that aren't single characters" do
0
+ misconfig_parser { |c| c.optional 'foo', 'foo' }
0
+ end
0
+
0
     it "should reject :help as a flag name" do
0
       misconfig_parser { |c| c.flag 'x', 'help' }
0
     end

Comments

  • alexvollmer Mon Jul 14 08:52:53 -0700 2008 at lib/clip.rb

    This is an awesome set of changes.

    If you can merge this change with the previous one, I’ll pull the whole set over and do another release. You need to use git rebase -i on your local box, mark this commit as a join with the previous and push all of the changes back to github. Let me know if you get stuck.

    Nice work dude!