strop 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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 7f104516e3a61250be80d317bb041ab59f8e39fadf0fd2b4aefe18de8af90165
4
- data.tar.gz: b55d4717c195ec9373b495b933758435eb764fd4f51073761a45456e2de47784
3
+ metadata.gz: c021ef142a7a5155da860f70e400c232afdf91bfe2b160d4bf12fd44356714b9
4
+ data.tar.gz: ba3a9549ff23d7636d09f4eece2aad318490b2143b78f0af86bd7e0178e75885
5
5
  SHA512:
6
- metadata.gz: 1e629cc1a18457b54482398d8d1578d787f57ddd182c627a076f2ce7d2be9c639be3c893734422d0a23cd2be5fa2a534c527888f1437d1474a527078f5365a63
7
- data.tar.gz: 8ef9f135caede03849470cd17ee470c8440b34838736bc100ef5c41502e41b35d7c99caa18c6f832edeeac72c9c67f6c926b03cc6d393f0679d34609ac8f112f
6
+ metadata.gz: ef12ddce52bac421a5a9320bd07b554bbb1ead04a4f0b7627e43ba835ac9c90bc9a7262d74fdbc4dad23e1cb834180575e60b5eb4a4d2c25aac54f57bcf4f24e
7
+ data.tar.gz: edb2a597fd1fff45d38b90c2b62c97b06742055c0f3ec9d12c5220c0dbf1ab80bcc595388db2a4b1183e71c5b3e2b894f4948ab0c77e9e9983f0a56320dd93c8
data/README.md CHANGED
@@ -58,10 +58,11 @@ puts Strop.parse_help(help_text).to_s(:case)
58
58
  ## Result members (Array of Opt, Arg, Sep)
59
59
 
60
60
  ```ruby
61
- res.opts # all Opt objects
62
- res.args # all Arg objects
63
- res.rest # args after -- separator
64
- res["flag"] # find opt by name
61
+ res.opts # all Opt objects
62
+ res.args # all Arg objects
63
+ res.rest # args after -- separator
64
+ res["flag"] # find opt by name
65
+ res[["flag"]] # find all opts matching name
65
66
 
66
67
  opt = res.opts.first
67
68
  arg = res.args.first
@@ -73,6 +74,7 @@ opt.no? # true if --no-foo variant used
73
74
  opt.yes? # opposite of `no?`
74
75
  arg.value # positional argument
75
76
  arg.arg # same as .value, useful for pattern matching
77
+ arg.to_s # implicit string conversion (same as .value)
76
78
  Sep # -- end of options marker; Const, not instantiated
77
79
  ```
78
80
 
@@ -118,6 +120,7 @@ cmd -fVAL, --foo=VAL # attached values
118
120
  cmd -f VAL, --foo VAL # separate values
119
121
  cmd --foo val -- --bar # --bar becomes positional after --
120
122
  cmd --intermixed args and --options # flexible ordering
123
+ cmd --ver # partial matching (--ver matches --verbose if unique)
121
124
  ```
122
125
 
123
126
  ## Manual option declaration building
@@ -141,6 +144,7 @@ Optdecl["foo_bar"] # --foo_bar: but not in strings.
141
144
  ```ruby
142
145
  optlist = Optlist[optdecl1, optdecl2] # combine decls into optlist
143
146
  optlist["f"] # lookup by name
147
+ optlist["fl"] # partial match (finds "flag" if unique)
144
148
  ```
145
149
 
146
150
  ## Argument requirements
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.2.0"
4
+ VERSION = "0.3.0"
5
5
  end
data/lib/strop.rb CHANGED
@@ -30,7 +30,18 @@ module Strop
30
30
  # Optlist[decl1, decl2] #=> [Optdecl(...), Optdecl(...)]
31
31
  class Optlist < Array # a list of Optdecls
32
32
  def self.from_help(doc) = Strop.parse_help(doc) #=> Optlist[decl, ...] # Build from help text
33
- def [](k, ...) = [String, Symbol].any?{ it === k } ? self.find{ it.names.member? k.to_s } : super(k, ...)
33
+
34
+ def [](k, ...)
35
+ case k
36
+ in String | Symbol
37
+ s = k.to_s
38
+ found = find{ it.names.member? s } and return found
39
+ found, *others = select{ it.names.any?{ it.start_with? s }} if s[1]
40
+ found if found && others.empty?
41
+ else super(k, ...)
42
+ end
43
+ end
44
+
34
45
  def to_s(as=:plain)
35
46
  case as
36
47
  when :plain then join("\n")
@@ -58,6 +69,7 @@ module Strop
58
69
  Arg = Data.define :value, :arg do
59
70
  def initialize(value:) = super(value:, arg: value)
60
71
  alias to_s value
72
+ alias to_str value
61
73
  end
62
74
 
63
75
  # Parsed option with declaration, invocation name, value, and negation state. Used internally. Seen as member of Result.
@@ -73,7 +85,7 @@ module Strop
73
85
  end
74
86
 
75
87
  # Const for parsed `--` end of option markers. Seen as member of Result.
76
- Sep = :end_marker
88
+ Sep = :sep
77
89
 
78
90
  # Parse result containing options, arguments, and separators; Returned by `parse`
79
91
  # Result[opt1, arg1, Sep] #=> [Opt(...), Arg(...), Sep]
@@ -83,7 +95,8 @@ module Strop
83
95
  def opts = Result.new(select { Opt === it })
84
96
  def [](k, ...)
85
97
  case k
86
- when String, Symbol then find{ Opt === it && it.decl.names.member?(k.to_s) }
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) }
87
100
  else super(k, ...)
88
101
  end
89
102
  end
@@ -123,7 +136,7 @@ module Strop
123
136
  res = Result.new
124
137
  ctx = :top
125
138
  name, token, opt = nil
126
- rx_value = /\A[^-]|\A\z/ # not an opt
139
+ rx_value = /\A[^-]|\A-?\z/ # not an opt
127
140
  loop do
128
141
  case ctx
129
142
  in :end then return res.concat tokens.map{ Arg[it] } # opt parsing ended, rest is positional args
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.2.0
4
+ version: 0.3.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Caio Chassot