docopt 0.0.2 → 0.0.3
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.
- data/README.md +15 -4
- data/lib/docopt.rb +12 -1
- metadata +1 -1
data/README.md
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
`docopt` – command line option parser, that will make you smile
|
2
2
|
===============================================================================
|
3
3
|
|
4
|
-
|
4
|
+
[](http://travis-ci.org/alexspeller/docopt)
|
5
5
|
|
6
6
|
Isn't it awesome how `optparse` and other option parsers generate help and
|
7
7
|
usage-messages based on your code?!
|
@@ -50,10 +50,21 @@ end
|
|
50
50
|
Hell yeah! The option parser is generated based on `doc` string above, that you
|
51
51
|
pass to the `Docopt` function.
|
52
52
|
|
53
|
-
|
54
|
-
|
53
|
+
```ruby
|
54
|
+
require 'docopt'
|
55
|
+
doc = "Usage: your_program.rb [options]
|
56
|
+
|
57
|
+
-h --help Show this.
|
58
|
+
-v --verbose Print more text.
|
59
|
+
--quiet Print less text.
|
60
|
+
-o FILE Specify output file [default: ./test.txt]"
|
61
|
+
|
62
|
+
options = Docopt(doc, version=nil, help=true)`
|
63
|
+
|
64
|
+
options['--help'] # returns true or false depending on option given
|
65
|
+
|
66
|
+
```
|
55
67
|
|
56
|
-
###`options = Docopt(doc, version=nil, help=true)`
|
57
68
|
|
58
69
|
`Docopt` takes 1 required and 2 optional arguments:
|
59
70
|
|
data/lib/docopt.rb
CHANGED
@@ -29,6 +29,14 @@ class Docopt
|
|
29
29
|
end
|
30
30
|
end
|
31
31
|
|
32
|
+
def set_value val
|
33
|
+
if argcount.zero?
|
34
|
+
@value = true
|
35
|
+
else
|
36
|
+
@value = val
|
37
|
+
end
|
38
|
+
end
|
39
|
+
|
32
40
|
def synonyms
|
33
41
|
([short, long] + symbols).compact
|
34
42
|
end
|
@@ -59,12 +67,15 @@ class Docopt
|
|
59
67
|
end
|
60
68
|
|
61
69
|
GetoptLong.new(*docopts.map(&:getopt)).each do |opt, arg|
|
70
|
+
docopt_option = option(opt)
|
62
71
|
if help and (opt == '--help' or opt == '-h')
|
63
72
|
puts doc.strip
|
64
73
|
exit
|
65
74
|
elsif version and opt == '--version'
|
66
75
|
puts version
|
67
76
|
exit
|
77
|
+
else
|
78
|
+
option.set_value arg
|
68
79
|
end
|
69
80
|
end
|
70
81
|
end
|
@@ -76,7 +87,7 @@ class Docopt
|
|
76
87
|
raise UnknownOptionError.new("#{name} option not found") unless option
|
77
88
|
option
|
78
89
|
end
|
79
|
-
|
90
|
+
|
80
91
|
|
81
92
|
|
82
93
|
def value name
|