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.
Files changed (4) hide show
  1. checksums.yaml +4 -4
  2. data/lib/strop/version.rb +1 -1
  3. data/lib/strop.rb +9 -8
  4. metadata +1 -1
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: c021ef142a7a5155da860f70e400c232afdf91bfe2b160d4bf12fd44356714b9
4
- data.tar.gz: ba3a9549ff23d7636d09f4eece2aad318490b2143b78f0af86bd7e0178e75885
3
+ metadata.gz: 225cf6a25d9576df114857aab9e157c4f9d2c666f74e066bd21fc012b9a8f83d
4
+ data.tar.gz: 2dfc769e16d1dff9db71788250fedeb23973a46fc2e86633bda69d03b3afef5c
5
5
  SHA512:
6
- metadata.gz: ef12ddce52bac421a5a9320bd07b554bbb1ead04a4f0b7627e43ba835ac9c90bc9a7262d74fdbc4dad23e1cb834180575e60b5eb4a4d2c25aac54f57bcf4f24e
7
- data.tar.gz: edb2a597fd1fff45d38b90c2b62c97b06742055c0f3ec9d12c5220c0dbf1ab80bcc595388db2a4b1183e71c5b3e2b894f4948ab0c77e9e9983f0a56320dd93c8
6
+ metadata.gz: 57a71386c4e906a3b444da5dabb81a145faef6795639e420917f20386ee75d7c4e8a1442c398ea72db413ee363f5b284ebee113b0e3875b14867c331968c8a99
7
+ data.tar.gz: a77e623a861b637d2c01e955bf984d556fdd54d4b3c8f9896d22ac8b346fb370702eb04053a1c1a922de51a050e5627e5429ecc1c568bb21f650cf28c7e8f4e5
data/lib/strop/version.rb CHANGED
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Strop
4
- VERSION = "0.3.0"
4
+ VERSION = "0.4.0"
5
5
  end
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{ Symbol === it ? it.to_s.gsub(?_, ?-) : it } # :foo_bar to "foo-bar" for symbols
16
- names[0] = names[0].sub(/[!?]$/, "") unless arg # opt? / opt! to opt, and... (unless arg given)
17
- arg ||= { ?? => :may, ?! => :must }[$&] || :shant # use ?/! to determine arg (unless arg given)
18
- label = names.find{ it.size > 1 } || names.first # the canonical name used to search for it
19
- %i[must may shant].member? arg or raise "invalid arg" # validate 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.to_s
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? name.to_s }
99
- in String | Symbol then find{ Opt === it && it.decl.names.member?(k.to_s) }
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
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: strop
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.3.0
4
+ version: 0.4.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Caio Chassot