opt-simple 0.9.5 → 0.9.6

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.
Files changed (3) hide show
  1. data/README +52 -33
  2. data/lib/opt_simple.rb +15 -7
  3. metadata +2 -2
data/README CHANGED
@@ -1,4 +1,4 @@
1
- = OptSimple provides a very simple interface to command line parsing.
1
+ == OptSimple provides a very simple interface to command line parsing.
2
2
 
3
3
  == Description
4
4
  Parameter specification, validity checking and argument transformations
@@ -36,62 +36,81 @@ It is recommended to install OptSimple using RubyGems:
36
36
 
37
37
  == Examples
38
38
 
39
- === One example that shows a lot of the behavior you might use
40
- require 'opt_simple'
41
-
42
- min = 5
43
- max = 15
39
+ === One example that shows most of the behavior you might use
40
+ require 'opt_simple'
44
41
 
45
- options = OptSimple.new.parse_opts! do
42
+ defaults = {
43
+ :n => 42,
44
+ :range => [5,10]
45
+ }
46
+
47
+ opts = OptSimple.new(defaults).parse_opts! do
46
48
  argument %w[-i --infile], "Infile, multiple allowed", "FILE" do | arg |
47
49
  accumulate_opt arg
48
50
  end
49
51
 
50
- option %w[-p --pattern --glob-pattern], "glob pattern","PATTERN"
52
+ flag %w[-d --debug],"debug mode"
51
53
 
52
- option %w[-n -num --num-values],"Number of val","VAL" do |arg|
54
+ flag %w[-m --cowbell --morecowbell],"I've got a fever, and this is the only prescription." do
55
+ accumulate_opt
56
+ end
57
+
58
+ option %w[-n -num --num-values],"The answer to everything","VAL" do |arg|
53
59
  set_opt arg.to_i
54
60
  end
55
61
 
56
- option "--range", "range: min,max (both >0) default is #{min},#{max}" do | arg1,arg2 |
57
- min = arg1.to_i
58
- max = arg2.to_i
62
+ option %w[-p --pattern --glob-pattern], "glob pattern","PATTERN"
63
+
64
+ option "--range", "range: min,max (both >0)" do | arg1,arg2 |
65
+ min,max = [arg1.to_i,arg2.to_i]
66
+ set_opt [min,max]
59
67
 
60
68
  error "max must be greater than min" unless max > min
61
69
  error "both must be >=0" if min < 0
62
70
  end
63
-
64
- flag %w[-v --verbose],"Verbosity. the more you set, the more we give" do
65
- accumulate_opt
66
- end
67
-
68
71
  end
69
72
 
70
73
  puts "Options"
71
- puts options
72
- # access the results using method names or hash syntax
73
- puts options.pattern
74
- puts options[:p]
75
- puts options['glob-pattern']
74
+ puts opts
75
+
76
+ # you can use method syntax to access the options
77
+ puts "Infile list: #{opts.infile}"
78
+
79
+ # Flags are set to false by default
80
+ puts "Debug" if opts.debug
81
+
82
+ # If accumulate_opt is used, flags still default to false, but
83
+ # will be integers if set on the CL
84
+ puts "Cowbell: #{opts['cowbell']}"
85
+
86
+ # You can use hash syntax to access the options as strings or symbols
87
+ puts "N: #{opts[:n]}"
88
+
89
+ # You can check to see if Options were set
90
+ puts "Pattern: #{opts.p}" if opts.include?(:p)
91
+
92
+ # Here, range was set to an Array.
93
+ puts "Range: #{opts.range.first} #{opts.range.last}"
76
94
 
77
95
  Which prints out an automatic usage statement:
78
96
  Usage: opt_ex.rb [options]
79
-
97
+
80
98
  MANDATORY ARGS:
81
- -i, --infile FILE Infile, multiple allowed
82
-
99
+ -i, --infile FILE Infile, multiple allowed
100
+
83
101
  OPTIONS:
102
+ -d, --debug debug mode
103
+
104
+ -m, --cowbell, --morecowbell I've got a fever, and this is the only prescription.
105
+
106
+ -n, -num, --num-values VAL The answer to everything
107
+
84
108
  -p, --pattern, --glob-pattern PATTERN glob pattern
85
-
86
- -n, -num, --num-values VAL Number of val
87
-
88
- --range ARG range: min,max (both >0) default is 5,15
89
-
90
- -v, --verbose Verbosity. the more you set, the more we give
91
-
109
+
110
+ --range ARG range: min,max (both >0) (default is [5, 10])
111
+
92
112
  -h, --help (for this help message)
93
113
 
94
-
95
114
  === A very simple example with no error checking. Use at your own risk!
96
115
 
97
116
  require 'opt_simple'
data/lib/opt_simple.rb CHANGED
@@ -264,8 +264,11 @@ class OptSimple
264
264
  optional_opts.each do | parm |
265
265
  help_str << parm.help_str(@longest_switch_len)
266
266
 
267
- # check to see if we have any defaults set to help in the help doc
268
- intersection = @defaults.keys & parm.names
267
+ # check to see if we have any defaults for options and args only
268
+ # to add to the help doc
269
+ intersection = []
270
+
271
+ intersection = @defaults.keys & parm.names unless parm.class == Flag
269
272
  if intersection.empty?
270
273
  help_str << "\n\n"
271
274
  else
@@ -299,8 +302,8 @@ class OptSimple
299
302
  end
300
303
 
301
304
  # The base class for command line parameter objects from which Flag,
302
- # Option and Argument are derived. Provides documentation methods,
303
- # an 'error method', and the 'set_opt' utility funciton.
305
+ # Option and Argument are derived. Users will probably only use the
306
+ # an 'error method' and the 'set_opt' utility funciton.
304
307
  class Parameter
305
308
  attr_reader :switches,:param_options,:metavar,:block
306
309
 
@@ -312,7 +315,6 @@ class OptSimple
312
315
  @help = help
313
316
  @block = block
314
317
  @param_options = OptSimple::Result.new
315
- @names = nil
316
318
  @param_options.add_alias(self.names)
317
319
  end
318
320
 
@@ -323,7 +325,12 @@ class OptSimple
323
325
 
324
326
  # returns a list of the switches without the leading '-'s
325
327
  def names
326
- @names ||= switches.map {|s| s.sub(/^-+/,'')}
328
+ names = []
329
+ switches.each do |s|
330
+ st = s.sub(/^-+/,'')
331
+ names << st << st.to_sym
332
+ end
333
+ names
327
334
  end
328
335
 
329
336
  def switch_len #:nodoc:
@@ -359,7 +366,7 @@ class OptSimple
359
366
  @param_options[names.first] = val
360
367
  end
361
368
 
362
- # is it mandatory to see this parameter on the command line?
369
+ # is it mandatory to see this parameter on the command line? Returns false unless overidden.
363
370
  def mandatory?; false; end
364
371
 
365
372
  end
@@ -439,6 +446,7 @@ class OptSimple
439
446
  # leading '-'s removed) will be used as keys in the Result
440
447
  # and the values set to the arg following the switch the args array.
441
448
  class Argument < Option
449
+ # returns true in an Argument
442
450
  def mandatory?; true; end
443
451
  end
444
452
 
metadata CHANGED
@@ -2,7 +2,7 @@
2
2
  name: opt-simple
3
3
  version: !ruby/object:Gem::Version
4
4
  prerelease:
5
- version: 0.9.5
5
+ version: 0.9.6
6
6
  platform: ruby
7
7
  authors:
8
8
  - Ethan Stryker
@@ -10,7 +10,7 @@ autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
12
 
13
- date: 2011-03-25 00:00:00 +00:00
13
+ date: 2011-03-26 00:00:00 +00:00
14
14
  default_executable:
15
15
  dependencies: []
16
16