optio 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 +1 -2
- data/VERSION +1 -1
- data/lib/optio/exceptions.rb +8 -1
- data/lib/optio/parser.rb +9 -4
- data/lib/optio/short_switch_registry.rb +41 -0
- data/lib/optio/switch.rb +23 -0
- data/optio.gemspec +3 -3
- metadata +48 -4
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: ab15e38b8250f34d485d2d561565cc0073293e1c
|
4
|
+
data.tar.gz: 4548155ad36f70ff96c2865736414a96d13a5221
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: c19b466a2d5c34f4ba42111784ad4b2f65d0cc18c03b238e7171c7d95e79233e57b98b4a1241fb139d0c2a9a22e96300362ac539e8c27381940151a3adccf332
|
7
|
+
data.tar.gz: 9afefd10c6c4e6c13f4887799fba6b021d58bc19a52de479905a119b876355231fee261b73b0b53438798e037ca071234f6382a827f0e6a744c1f9d7f9b27ba7
|
data/README.md
CHANGED
@@ -2,7 +2,6 @@
|
|
2
2
|
|
3
3
|
Welcome to your new gem! In this directory, you'll find the files you need to be able to package up your Ruby library into a gem. Put your Ruby code in the file `lib/optio`. To experiment with that code, run `bin/console` for an interactive prompt.
|
4
4
|
|
5
|
-
TODO: Delete this and the text above, and describe your gem
|
6
5
|
|
7
6
|
## Installation
|
8
7
|
|
@@ -22,7 +21,7 @@ Or install it yourself as:
|
|
22
21
|
|
23
22
|
## Usage
|
24
23
|
|
25
|
-
|
24
|
+
Write usage instructions here
|
26
25
|
|
27
26
|
## Development
|
28
27
|
|
data/VERSION
CHANGED
@@ -1 +1 @@
|
|
1
|
-
0.
|
1
|
+
0.3.0
|
data/lib/optio/exceptions.rb
CHANGED
@@ -1,6 +1,13 @@
|
|
1
1
|
module Optio
|
2
2
|
module Exceptions
|
3
|
-
|
3
|
+
# Raised when the same switched is registered more
|
4
|
+
# than once
|
5
|
+
class SwitchAlreadyExistsError < RuntimeError
|
6
|
+
end
|
7
|
+
|
8
|
+
# Raised when trying to register more than one
|
9
|
+
# switch with the same short switch name
|
10
|
+
class ShortSwitchAlreadyExistsError < RuntimeError
|
4
11
|
end
|
5
12
|
end
|
6
13
|
end
|
data/lib/optio/parser.rb
CHANGED
@@ -1,4 +1,5 @@
|
|
1
1
|
require 'optparse'
|
2
|
+
require 'optio/switch'
|
2
3
|
|
3
4
|
module Optio
|
4
5
|
# A wrapper class for Ruby's {OptionParser}[https://ruby-doc.org/stdlib-2.4.1/libdoc/optparse/rdoc/OptionParser.html]
|
@@ -17,6 +18,8 @@ module Optio
|
|
17
18
|
# Optio::Parser.new do |parser|
|
18
19
|
# parser.subcommand :dance
|
19
20
|
# end
|
21
|
+
#
|
22
|
+
# TODO expose the option to auto assign short switch
|
20
23
|
class Parser
|
21
24
|
def initialize(&block)
|
22
25
|
@switches = {}
|
@@ -40,7 +43,8 @@ module Optio
|
|
40
43
|
# * +:type+ - Expected argument type, {see OptionParser for details}[https://ruby-doc.org/stdlib-2.4.1/libdoc/optparse/rdoc/OptionParser.html#class-OptionParser-label-Type+Coercion]
|
41
44
|
# * +:short:+ - The short switch, i,e +-h+ for +--help+
|
42
45
|
def switch(switch_name, opts = {})
|
43
|
-
|
46
|
+
switch_obj = Switch.new(switch_name, opts)
|
47
|
+
store_parameter(switch_name, switch_obj, @switches)
|
44
48
|
end
|
45
49
|
|
46
50
|
# TODO support sub commands
|
@@ -74,9 +78,10 @@ module Optio
|
|
74
78
|
|
75
79
|
def rb_parser(parsed_params)
|
76
80
|
OptionParser.new do |rb_parser|
|
77
|
-
@switches.each do |
|
78
|
-
rb_parser.
|
79
|
-
|
81
|
+
@switches.each do |switch_name, switch_obj|
|
82
|
+
rb_parser.banner = @banner if @banner
|
83
|
+
rb_parser.on(*(switch_obj.rb_parser_args)) do |param|
|
84
|
+
parsed_params[switch_name] = param
|
80
85
|
end
|
81
86
|
end
|
82
87
|
end
|
@@ -0,0 +1,41 @@
|
|
1
|
+
require 'optio/exceptions'
|
2
|
+
|
3
|
+
module Optio
|
4
|
+
class ShortSwitchRegistry
|
5
|
+
attr_reader :registered
|
6
|
+
attr_reader :pool
|
7
|
+
|
8
|
+
def initialize
|
9
|
+
@registered = {}
|
10
|
+
@pool = ('a'..'z').to_a
|
11
|
+
end
|
12
|
+
|
13
|
+
def register(switch_name, short_name)
|
14
|
+
if !@pool.include?(short_name)
|
15
|
+
raise Exceptions::ShortSwitchAlreadyExistsError,
|
16
|
+
"#{short_name} already registered with #{switch_name}"
|
17
|
+
end
|
18
|
+
@pool.delete(short_name)
|
19
|
+
@registered[switch_name] = short_name
|
20
|
+
end
|
21
|
+
|
22
|
+
def get(switch_name)
|
23
|
+
switch_list = switch_name.to_s.chars.to_a.uniq
|
24
|
+
chosen_switch = switch_list.find do |short_switch|
|
25
|
+
@pool.include?(short_switch)
|
26
|
+
end
|
27
|
+
if !chosen_switch
|
28
|
+
chosen_switch = random_short_switch
|
29
|
+
end
|
30
|
+
@pool.delete(chosen_switch)
|
31
|
+
@registered[switch_name] = chosen_switch
|
32
|
+
chosen_switch
|
33
|
+
end
|
34
|
+
|
35
|
+
private
|
36
|
+
|
37
|
+
def random_short_switch
|
38
|
+
@pool.shuffle.first
|
39
|
+
end
|
40
|
+
end
|
41
|
+
end
|
data/lib/optio/switch.rb
ADDED
@@ -0,0 +1,23 @@
|
|
1
|
+
|
2
|
+
module Optio
|
3
|
+
class Switch
|
4
|
+
# TODO mandatory switch
|
5
|
+
def initialize(name, opts)
|
6
|
+
@name = name
|
7
|
+
@opts = opts
|
8
|
+
end
|
9
|
+
|
10
|
+
def rb_parser_args
|
11
|
+
args = ["--#{@name} #{@name.upcase}"]
|
12
|
+
if @opts[:short]
|
13
|
+
args.unshift("-#{@opts[:short]} #{@name.upcase}")
|
14
|
+
end
|
15
|
+
[:type, :desc].each do |key|
|
16
|
+
if @opts[key]
|
17
|
+
args << @opts[key]
|
18
|
+
end
|
19
|
+
end
|
20
|
+
args
|
21
|
+
end
|
22
|
+
end
|
23
|
+
end
|
data/optio.gemspec
CHANGED
@@ -9,9 +9,9 @@ Gem::Specification.new do |spec|
|
|
9
9
|
spec.authors = ["Amit Goldberg"]
|
10
10
|
spec.email = ["amit.goldberg@gmail.com"]
|
11
11
|
|
12
|
-
spec.summary = %q{}
|
13
|
-
spec.description =
|
14
|
-
spec.homepage = "https://
|
12
|
+
spec.summary = %q{A wrapper for Ruby's OptionParser library}
|
13
|
+
spec.description = File.read('README.md')
|
14
|
+
spec.homepage = "https://rubygems.org/gems/optio"
|
15
15
|
spec.license = "MIT"
|
16
16
|
|
17
17
|
spec.files = `git ls-files -z`.split("\x0").reject do |f|
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: optio
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.3.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Amit Goldberg
|
@@ -52,7 +52,49 @@ dependencies:
|
|
52
52
|
- - "~>"
|
53
53
|
- !ruby/object:Gem::Version
|
54
54
|
version: '5.0'
|
55
|
-
description:
|
55
|
+
description: |
|
56
|
+
# Optio
|
57
|
+
|
58
|
+
Welcome to your new gem! In this directory, you'll find the files you need to be able to package up your Ruby library into a gem. Put your Ruby code in the file `lib/optio`. To experiment with that code, run `bin/console` for an interactive prompt.
|
59
|
+
|
60
|
+
|
61
|
+
## Installation
|
62
|
+
|
63
|
+
Add this line to your application's Gemfile:
|
64
|
+
|
65
|
+
```ruby
|
66
|
+
gem 'optio'
|
67
|
+
```
|
68
|
+
|
69
|
+
And then execute:
|
70
|
+
|
71
|
+
$ bundle
|
72
|
+
|
73
|
+
Or install it yourself as:
|
74
|
+
|
75
|
+
$ gem install optio
|
76
|
+
|
77
|
+
## Usage
|
78
|
+
|
79
|
+
Write usage instructions here
|
80
|
+
|
81
|
+
## Development
|
82
|
+
|
83
|
+
After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake test` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
|
84
|
+
|
85
|
+
To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
|
86
|
+
|
87
|
+
## Contributing
|
88
|
+
|
89
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/optio. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [Contributor Covenant](http://contributor-covenant.org) code of conduct.
|
90
|
+
|
91
|
+
## License
|
92
|
+
|
93
|
+
The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
|
94
|
+
|
95
|
+
## Code of Conduct
|
96
|
+
|
97
|
+
Everyone interacting in the Optio project’s codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/[USERNAME]/optio/blob/master/CODE_OF_CONDUCT.md).
|
56
98
|
email:
|
57
99
|
- amit.goldberg@gmail.com
|
58
100
|
executables:
|
@@ -124,9 +166,11 @@ files:
|
|
124
166
|
- lib/optio.rb
|
125
167
|
- lib/optio/exceptions.rb
|
126
168
|
- lib/optio/parser.rb
|
169
|
+
- lib/optio/short_switch_registry.rb
|
170
|
+
- lib/optio/switch.rb
|
127
171
|
- lib/optio/version.rb
|
128
172
|
- optio.gemspec
|
129
|
-
homepage: https://
|
173
|
+
homepage: https://rubygems.org/gems/optio
|
130
174
|
licenses:
|
131
175
|
- MIT
|
132
176
|
metadata: {}
|
@@ -149,5 +193,5 @@ rubyforge_project:
|
|
149
193
|
rubygems_version: 2.6.11
|
150
194
|
signing_key:
|
151
195
|
specification_version: 4
|
152
|
-
summary: '
|
196
|
+
summary: A wrapper for Ruby's OptionParser library
|
153
197
|
test_files: []
|