auto-optparse 1.0.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 +7 -0
- data/LICENSE.txt +21 -0
- data/README.md +43 -0
- data/lib/auto/optparse/error.rb +6 -0
- data/lib/auto/optparse/instance_methods.rb +14 -0
- data/lib/auto/optparse/parser_configuration.rb +34 -0
- data/lib/auto/optparse/readers.rb +20 -0
- data/lib/auto/optparse/version.rb +5 -0
- data/lib/auto/optparse.rb +4 -0
- data/lib/auto.rb +22 -0
- metadata +56 -0
checksums.yaml
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
---
|
|
2
|
+
SHA256:
|
|
3
|
+
metadata.gz: e9164e714bf25676bb8cdd7ab1db4c8223c6d7ceb05587af5043e6ec4f3227b4
|
|
4
|
+
data.tar.gz: a39982d820ee0cb60a55c641ca0ce6713add090c37bb2cba1f797524e81c54dc
|
|
5
|
+
SHA512:
|
|
6
|
+
metadata.gz: 13bc244e0d439a7f6d4ffdc969bba6403c46485d6c45d290b1deafa5344a6540d100b207f5e12978f843b2055cf0ae14aefc5662bab4a9b1016ac744ddadef86
|
|
7
|
+
data.tar.gz: f4a1fde786218f4feebbdb9ca52622ff3ba46bcbe303291366ad3159d69350b5db9775933628f703895e1006d67692b35bb624a56a5b862c8422f273099a16f3
|
data/LICENSE.txt
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
The MIT License (MIT)
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Emil Chludziński
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in
|
|
13
|
+
all copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
21
|
+
THE SOFTWARE.
|
data/README.md
ADDED
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
# Auto::Optparse
|
|
2
|
+
|
|
3
|
+
`Auto::Optparse` is a module that speeds up writing short scripts by configuring `OptionParser` from `@options` hash.
|
|
4
|
+
It will cast commonly used types and generate barebones-but-readable `--help` output.
|
|
5
|
+
Also provides `Auto.run` using `#run`, and `#method_missing` implementation for reading `@options`.
|
|
6
|
+
All the boilerplate reduced to one or two lines instead, and your scripts is now easy to configure and use!
|
|
7
|
+
|
|
8
|
+
## Installation
|
|
9
|
+
|
|
10
|
+
```bash
|
|
11
|
+
gem install auto-optparse
|
|
12
|
+
# or
|
|
13
|
+
bundle add auto-optparse
|
|
14
|
+
```
|
|
15
|
+
|
|
16
|
+
## Usage
|
|
17
|
+
|
|
18
|
+
See `integration/datey.rb` for example of usage in a moderately-contrived script.
|
|
19
|
+
|
|
20
|
+
In short, to make the most of this gem:
|
|
21
|
+
1. Have your script's class `include Auto::Optparse::InstanceMethods`,
|
|
22
|
+
2. In `initialize`, fill `@options` with a Hash of default option values of supported types,
|
|
23
|
+
3. Implement method `run` with whatever your script is supposed to do,
|
|
24
|
+
4. Use `Auto.run` as execution guard.
|
|
25
|
+
|
|
26
|
+
There might be some situations in which you are not able to do all of the above easily - for example, if you need to fill default option values based on external data, and consider it poor form to have code that could throw an exception in `initialize`. You should be able to still mix-and-match, possibly using some parts of this gem: like mixing in the instance methods, but doing your own `Auto.run!`-equivalent that calls something between `initialize` and `configure`, and still asking `Auto.run?` instead of typing out the boilerplate execution guard.
|
|
27
|
+
|
|
28
|
+
## Development
|
|
29
|
+
|
|
30
|
+
```bash
|
|
31
|
+
git clone git@gitlab.com:tanstaafl/auto-optparse.git
|
|
32
|
+
cd auto-optparse
|
|
33
|
+
bundle install
|
|
34
|
+
bundle exec rake
|
|
35
|
+
```
|
|
36
|
+
|
|
37
|
+
## Contributing
|
|
38
|
+
|
|
39
|
+
Bug reports and merge requests are welcome on GitLab at https://gitlab.com/tanstaafl/auto-optparse.
|
|
40
|
+
|
|
41
|
+
## License
|
|
42
|
+
|
|
43
|
+
The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
require 'auto/optparse/parser_configuration'
|
|
2
|
+
require 'auto/optparse/readers'
|
|
3
|
+
|
|
4
|
+
module Auto
|
|
5
|
+
module Optparse
|
|
6
|
+
# all the includable helpers, together
|
|
7
|
+
module InstanceMethods
|
|
8
|
+
attr_reader :options
|
|
9
|
+
|
|
10
|
+
include ParserConfiguration
|
|
11
|
+
include Readers
|
|
12
|
+
end
|
|
13
|
+
end
|
|
14
|
+
end
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
require 'optparse'
|
|
2
|
+
|
|
3
|
+
require 'auto/optparse/error'
|
|
4
|
+
|
|
5
|
+
module Auto
|
|
6
|
+
module Optparse
|
|
7
|
+
# helpers for auto-generating OptionParser configuration from @options
|
|
8
|
+
module ParserConfiguration
|
|
9
|
+
def configure(argvies: ARGV)
|
|
10
|
+
parser = OptionParser.new
|
|
11
|
+
options.each do |k, v|
|
|
12
|
+
parser.on(*generate_optparse_entry(k, v), "Default: #{v.inspect}")
|
|
13
|
+
end
|
|
14
|
+
yield parser if block_given?
|
|
15
|
+
parser.parse!(argvies, into: options)
|
|
16
|
+
end
|
|
17
|
+
|
|
18
|
+
class UnsupportedDefaultOption < Error; end
|
|
19
|
+
|
|
20
|
+
def generate_optparse_entry(k, v)
|
|
21
|
+
case v
|
|
22
|
+
when String
|
|
23
|
+
["--#{k}=STRING"]
|
|
24
|
+
when Integer, Float, Array, Regexp
|
|
25
|
+
["--#{k}=#{v.class.name.upcase}", v.class]
|
|
26
|
+
when TrueClass, FalseClass
|
|
27
|
+
["--[no-]#{k}"]
|
|
28
|
+
else
|
|
29
|
+
raise UnsupportedDefaultOption, "Don't know how to convert #{k.inspect} => #{v.inspect} (#{v.class}) to option!"
|
|
30
|
+
end
|
|
31
|
+
end
|
|
32
|
+
end
|
|
33
|
+
end
|
|
34
|
+
end
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
module Auto
|
|
2
|
+
module Optparse
|
|
3
|
+
# helpers for automagically reading @options
|
|
4
|
+
module Readers
|
|
5
|
+
def method_missing(name, *args)
|
|
6
|
+
return super unless respond_to_missing?(name)
|
|
7
|
+
|
|
8
|
+
options[method_missing_name_normalizer(name)]
|
|
9
|
+
end
|
|
10
|
+
|
|
11
|
+
def respond_to_missing?(name, _include_private = false)
|
|
12
|
+
options.key?(method_missing_name_normalizer(name)) || super
|
|
13
|
+
end
|
|
14
|
+
|
|
15
|
+
def method_missing_name_normalizer(method_name)
|
|
16
|
+
method_name.to_s.delete_suffix('?').to_sym
|
|
17
|
+
end
|
|
18
|
+
end
|
|
19
|
+
end
|
|
20
|
+
end
|
data/lib/auto.rb
ADDED
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
# class-level stateless helpers
|
|
2
|
+
module Auto
|
|
3
|
+
def self.run(klass, argvies: ARGV, calling_file: caller_locations.first.path)
|
|
4
|
+
run!(klass, argvies: argvies) if run?(calling_file: calling_file) # otherwise we call from here
|
|
5
|
+
end
|
|
6
|
+
|
|
7
|
+
def self.run!(klass, argvies: ARGV, &block)
|
|
8
|
+
instance = klass.new
|
|
9
|
+
instance.configure(argvies: argvies, &block)
|
|
10
|
+
instance.run
|
|
11
|
+
rescue StandardError => exc
|
|
12
|
+
puts
|
|
13
|
+
puts exc.class.name
|
|
14
|
+
puts exc.message
|
|
15
|
+
puts exc.backtrace.join("\n")
|
|
16
|
+
exit false
|
|
17
|
+
end
|
|
18
|
+
|
|
19
|
+
def self.run?(calling_file: caller_locations.first.path)
|
|
20
|
+
File.expand_path($PROGRAM_NAME) == File.expand_path(calling_file)
|
|
21
|
+
end
|
|
22
|
+
end
|
metadata
ADDED
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
|
2
|
+
name: auto-optparse
|
|
3
|
+
version: !ruby/object:Gem::Version
|
|
4
|
+
version: 1.0.0
|
|
5
|
+
platform: ruby
|
|
6
|
+
authors:
|
|
7
|
+
- Emil Chludziński
|
|
8
|
+
bindir: exe
|
|
9
|
+
cert_chain: []
|
|
10
|
+
date: 1980-01-02 00:00:00.000000000 Z
|
|
11
|
+
dependencies: []
|
|
12
|
+
description: |
|
|
13
|
+
`Auto::Optparse` is a module that speeds up writing short scripts by configuring `OptionParser` from `@options` hash.
|
|
14
|
+
It will cast commonly used types and generate barebones-but-readable `--help` output.
|
|
15
|
+
Also provides `Auto.run` using `#run`, and `#method_missing` implementation for reading `@options`.
|
|
16
|
+
All the boilerplate reduced to one or two lines instead, and your scripts is now easy to configure and use!
|
|
17
|
+
email:
|
|
18
|
+
- tanstaafl@tlen.pl
|
|
19
|
+
executables: []
|
|
20
|
+
extensions: []
|
|
21
|
+
extra_rdoc_files: []
|
|
22
|
+
files:
|
|
23
|
+
- LICENSE.txt
|
|
24
|
+
- README.md
|
|
25
|
+
- lib/auto.rb
|
|
26
|
+
- lib/auto/optparse.rb
|
|
27
|
+
- lib/auto/optparse/error.rb
|
|
28
|
+
- lib/auto/optparse/instance_methods.rb
|
|
29
|
+
- lib/auto/optparse/parser_configuration.rb
|
|
30
|
+
- lib/auto/optparse/readers.rb
|
|
31
|
+
- lib/auto/optparse/version.rb
|
|
32
|
+
homepage: https://gitlab.com/tanstaafl/auto-optparse
|
|
33
|
+
licenses:
|
|
34
|
+
- MIT
|
|
35
|
+
metadata:
|
|
36
|
+
homepage_uri: https://gitlab.com/tanstaafl/auto-optparse
|
|
37
|
+
source_code_uri: https://gitlab.com/tanstaafl/auto-optparse
|
|
38
|
+
rubygems_mfa_required: 'true'
|
|
39
|
+
rdoc_options: []
|
|
40
|
+
require_paths:
|
|
41
|
+
- lib
|
|
42
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
|
43
|
+
requirements:
|
|
44
|
+
- - ">="
|
|
45
|
+
- !ruby/object:Gem::Version
|
|
46
|
+
version: '3.1'
|
|
47
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
|
48
|
+
requirements:
|
|
49
|
+
- - ">="
|
|
50
|
+
- !ruby/object:Gem::Version
|
|
51
|
+
version: '0'
|
|
52
|
+
requirements: []
|
|
53
|
+
rubygems_version: 4.0.6
|
|
54
|
+
specification_version: 4
|
|
55
|
+
summary: auto-optparse configures OptionParser from a hash of default option values
|
|
56
|
+
test_files: []
|