parseopt 0.2.0 → 0.3.0
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.
- checksums.yaml +4 -4
- data/README.md +39 -0
- data/Rakefile +7 -0
- data/lib/parseopt.rb +28 -3
- data/test/test_optparse.rb +8 -0
- metadata +5 -3
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 6b4e4cca586e2ff40e257ebcc12528f9227f983f12243370ee09169aa03686cb
|
4
|
+
data.tar.gz: d058140b9cc13b30de2edafce3d5ac57c806f4be6458a6ec4eef1f337b21676c
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: ae40e7ba95a36edc00b5cd0a11031a10bc859c263fd781fc0e4a290da422ea9d610e15aefd173ee786cb6b43adf8609a65dbd7b9a9324dffa8fddaea8612661f
|
7
|
+
data.tar.gz: eeabc5a66a1ad9607a7fadf4681fed615895032fd74fa6610038a4dbe70b309744b1008ac9e46f6f5661625bf42e5ec40c2f04667f93ae32fc129521ed57940f
|
data/README.md
ADDED
@@ -0,0 +1,39 @@
|
|
1
|
+
A very simple option parser.
|
2
|
+
|
3
|
+
```ruby
|
4
|
+
require 'parseopt'
|
5
|
+
|
6
|
+
opts = ParseOpt.new('my command')
|
7
|
+
|
8
|
+
# only short
|
9
|
+
opts.on('b') do |v|
|
10
|
+
$bool = v
|
11
|
+
end
|
12
|
+
|
13
|
+
# short and long
|
14
|
+
opts.on('s', 'string') do |v|
|
15
|
+
$string = v
|
16
|
+
end
|
17
|
+
|
18
|
+
# short, long, and help
|
19
|
+
opts.on('n', 'number', 'Number') do |v|
|
20
|
+
$number = v.to_i
|
21
|
+
end
|
22
|
+
|
23
|
+
opts.parse
|
24
|
+
```
|
25
|
+
|
26
|
+
Running with `--help` gives:
|
27
|
+
|
28
|
+
```
|
29
|
+
usage: my command
|
30
|
+
-b
|
31
|
+
-s, --string
|
32
|
+
-n, --number Number
|
33
|
+
```
|
34
|
+
|
35
|
+
## Installation
|
36
|
+
|
37
|
+
Simply install the gem:
|
38
|
+
|
39
|
+
gem install parseopt
|
data/Rakefile
ADDED
data/lib/parseopt.rb
CHANGED
@@ -1,6 +1,4 @@
|
|
1
1
|
class ParseOpt
|
2
|
-
attr_writer :usage
|
3
|
-
|
4
2
|
class Option
|
5
3
|
attr_reader :short, :long, :help
|
6
4
|
|
@@ -15,17 +13,38 @@ class ParseOpt
|
|
15
13
|
@block.call(v) || true
|
16
14
|
end
|
17
15
|
end
|
16
|
+
private_constant :Option
|
18
17
|
|
19
|
-
|
18
|
+
# Creates a new instance.
|
19
|
+
#
|
20
|
+
# Yields itself if called with a block.
|
21
|
+
#
|
22
|
+
# @param [String] usage usage banner.
|
23
|
+
#
|
24
|
+
# @yieldparam [ParseOpt] self Option parser object
|
25
|
+
def initialize(usage = nil)
|
20
26
|
@list = {}
|
27
|
+
@usage = usage
|
28
|
+
yield self if block_given?
|
21
29
|
end
|
22
30
|
|
31
|
+
# Creates an option.
|
32
|
+
#
|
33
|
+
# The block is called with the value when the option is found.
|
34
|
+
#
|
35
|
+
# @example Simple boolean
|
36
|
+
# opts.on('b') { $bool = true }
|
37
|
+
# @example Simple string with value
|
38
|
+
# opts.on('s', 'string') { |value| $string = value }
|
39
|
+
#
|
40
|
+
# @yieldparam value value parsed
|
23
41
|
def on(short, long = nil, help = nil, &block)
|
24
42
|
opt = Option.new(short, long, help, &block)
|
25
43
|
@list[short] = opt if short
|
26
44
|
@list[long] = opt if long
|
27
45
|
end
|
28
46
|
|
47
|
+
# Parses all the command line arguments
|
29
48
|
def parse(args = ARGV)
|
30
49
|
if args.member?('-h') or args.member?('--help')
|
31
50
|
usage
|
@@ -50,6 +69,12 @@ class ParseOpt
|
|
50
69
|
end
|
51
70
|
end
|
52
71
|
|
72
|
+
# Sets the usage banner
|
73
|
+
def usage=(value)
|
74
|
+
@usage = value
|
75
|
+
end
|
76
|
+
|
77
|
+
# Generates the usage output (similar to `--help`)
|
53
78
|
def usage
|
54
79
|
puts 'usage: %s' % @usage
|
55
80
|
@list.values.uniq.each do |opt|
|
data/test/test_optparse.rb
CHANGED
@@ -58,6 +58,14 @@ class ParseOptTest < Test::Unit::TestCase
|
|
58
58
|
assert(!bool)
|
59
59
|
end
|
60
60
|
|
61
|
+
def test_init
|
62
|
+
bool = false
|
63
|
+
ParseOpt.new('test script') do |opts|
|
64
|
+
opts.on('b', 'bool') { |v| bool = v }
|
65
|
+
end.parse(%w[-b])
|
66
|
+
assert(bool)
|
67
|
+
end
|
68
|
+
|
61
69
|
def test_usage
|
62
70
|
expected = <<~EOF
|
63
71
|
usage:
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: parseopt
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.3.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Felipe Contreras
|
@@ -10,12 +10,14 @@ bindir: bin
|
|
10
10
|
cert_chain: []
|
11
11
|
date: 2021-05-26 00:00:00.000000000 Z
|
12
12
|
dependencies: []
|
13
|
-
description:
|
13
|
+
description:
|
14
14
|
email: felipe.contreras@gmail.com
|
15
15
|
executables: []
|
16
16
|
extensions: []
|
17
17
|
extra_rdoc_files: []
|
18
18
|
files:
|
19
|
+
- README.md
|
20
|
+
- Rakefile
|
19
21
|
- lib/parseopt.rb
|
20
22
|
- test/test_optparse.rb
|
21
23
|
homepage: https://rubygems.org/gems/parseopt
|
@@ -40,6 +42,6 @@ requirements: []
|
|
40
42
|
rubygems_version: 3.2.15
|
41
43
|
signing_key:
|
42
44
|
specification_version: 4
|
43
|
-
summary:
|
45
|
+
summary: A very simple option parser.
|
44
46
|
test_files:
|
45
47
|
- test/test_optparse.rb
|