strop 0.3.0 → 0.4.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/lib/strop/version.rb +1 -1
- data/lib/strop.rb +9 -8
- metadata +1 -1
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA256:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: 225cf6a25d9576df114857aab9e157c4f9d2c666f74e066bd21fc012b9a8f83d
|
|
4
|
+
data.tar.gz: 2dfc769e16d1dff9db71788250fedeb23973a46fc2e86633bda69d03b3afef5c
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: 57a71386c4e906a3b444da5dabb81a145faef6795639e420917f20386ee75d7c4e8a1442c398ea72db413ee363f5b284ebee113b0e3875b14867c331968c8a99
|
|
7
|
+
data.tar.gz: a77e623a861b637d2c01e955bf984d556fdd54d4b3c8f9896d22ac8b346fb370702eb04053a1c1a922de51a050e5627e5429ecc1c568bb21f650cf28c7e8f4e5
|
data/lib/strop/version.rb
CHANGED
data/lib/strop.rb
CHANGED
|
@@ -6,17 +6,18 @@ require_relative "strop/version"
|
|
|
6
6
|
# Command-line option parser that builds options from help text
|
|
7
7
|
module Strop
|
|
8
8
|
def self.prefix(name) = (name[1] ? "--" : "-") + name # helper for printing back option names with the right prefix
|
|
9
|
+
def self.name_from_symbol(name) = Symbol === name ? name.to_s.gsub(?_, ?-) : name
|
|
9
10
|
|
|
10
11
|
# Option declaration: names, argument requirement, and canonical label (auto-determined)
|
|
11
12
|
# Optdecl[:f, :foo, arg: :may] #=> Optdecl(names: ["f", "foo"], arg: :may, label: "foo")
|
|
12
13
|
Optdecl = Data.define(:names, :arg, :label) do
|
|
13
14
|
def self.[](*names, arg: nil) = new(names:, arg:) # Custom builder: Optdecl[names, ..., arg: ...]
|
|
14
15
|
def initialize(names:, arg: nil)
|
|
15
|
-
names = [*names].map{
|
|
16
|
-
names[0] = names[0].sub(/[!?]$/, "") unless arg
|
|
17
|
-
arg ||= { ?? => :may, ?! => :must }[$&] || :shant
|
|
18
|
-
label = names.find{ it.size > 1 } || names.first
|
|
19
|
-
%i[must may shant].member? arg or raise "invalid arg"
|
|
16
|
+
names = [*names].map{ Strop.name_from_symbol it } # :foo_bar to "foo-bar" for symbols
|
|
17
|
+
names[0] = names[0].sub(/[!?]$/, "") unless arg # opt? / opt! to opt, and... (unless arg given)
|
|
18
|
+
arg ||= { ?? => :may, ?! => :must }[$&] || :shant # use ?/! to determine arg (unless arg given)
|
|
19
|
+
label = names.find{ it.size > 1 } || names.first # the canonical name used to search for it
|
|
20
|
+
%i[must may shant].member? arg or raise "invalid arg" # validate arg
|
|
20
21
|
super names:, arg:, label:
|
|
21
22
|
end
|
|
22
23
|
|
|
@@ -34,7 +35,7 @@ module Strop
|
|
|
34
35
|
def [](k, ...)
|
|
35
36
|
case k
|
|
36
37
|
in String | Symbol
|
|
37
|
-
s = k
|
|
38
|
+
s = Strop.name_from_symbol k
|
|
38
39
|
found = find{ it.names.member? s } and return found
|
|
39
40
|
found, *others = select{ it.names.any?{ it.start_with? s }} if s[1]
|
|
40
41
|
found if found && others.empty?
|
|
@@ -95,8 +96,8 @@ module Strop
|
|
|
95
96
|
def opts = Result.new(select { Opt === it })
|
|
96
97
|
def [](k, ...)
|
|
97
98
|
case k
|
|
98
|
-
in [String | Symbol => name] then opts.select{ it.decl.names.include?
|
|
99
|
-
in String | Symbol then find{ Opt === it && it.decl.names.member?(k
|
|
99
|
+
in [String | Symbol => name] then opts.select{ it.decl.names.include? Strop.name_from_symbol name }
|
|
100
|
+
in String | Symbol then find{ Opt === it && it.decl.names.member?(Strop.name_from_symbol k) }
|
|
100
101
|
else super(k, ...)
|
|
101
102
|
end
|
|
102
103
|
end
|