options_by_example 3.0.0 → 3.2.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: 98c16609314706667d2b776fa71de92ca874b2fcc803ea8f80387fe4f0245d9a
4
- data.tar.gz: 8cc7023f98016edaa5521d6184985fb4133135e902f0380f81e233b6f52bfb97
3
+ metadata.gz: b5533bd626a90e56af73f7fe32cbd2c0a79630da399242c98abce184ac084dd9
4
+ data.tar.gz: 4583a92414c1e10544ffb42b4e45da0d1c0eac1adc1d38748bd31e24e791337e
5
5
  SHA512:
6
- metadata.gz: a4e54eeb9f5beef88733ac4c70055570eeff930ccacd287f88b80905bd1760b6b1ec3513c5ea144d1c7cdc259af7d4991f81c5be82249cecb2f939d291d29032
7
- data.tar.gz: 9126c11cba93c504fd41939dfa24c62578889b8546b9f472091a9463eed4cc6e3f409ff7e40a8219ce9a70913ae63872486745629431290e8f87dec6edf351be
6
+ metadata.gz: cd7df1507d066462d1031d71dc054fa886764dddca9fe17b4d6f7310f9e17f75673a6a6709e6ef1636d863dd35a537918d9e6838a04981cd947174d402569480
7
+ data.tar.gz: 9f11d4fdae24faefc1069435683e65c648635568d54d19e3e8197040b721db3630be4aa779781403d26b5c2e04dd23b3b6bcc0e3bef11d405855ac07e89a0630
data/README.md CHANGED
@@ -30,15 +30,15 @@ require 'options_by_example'
30
30
 
31
31
  Options = OptionsByExample.read(DATA).parse(ARGV)
32
32
 
33
- puts Options.include_secure?
34
- puts Options.include_verbose?
35
- puts Options.include_retries?
36
- puts Options.include_timeout?
37
- puts Options.argument_retries
38
- puts Options.argument_timeout
39
- puts Options.argument_mode
40
- puts Options.argument_host
41
- puts Options.argument_port
33
+ puts Options.include? :secure
34
+ puts Options.include? :verbose
35
+ puts Options.include? :retries
36
+ puts Options.include? :timeout
37
+ puts Options.get :retries
38
+ puts Options.get :timeout
39
+ puts Options.get :mode
40
+ puts Options.get :host
41
+ puts Options.get :port
42
42
 
43
43
 
44
44
  __END__
@@ -50,8 +50,8 @@ Usage: connect [options] [mode] host port
50
50
  Options:
51
51
  -s, --secure Establish a secure connection (SSL/TSL)
52
52
  -v, --verbose Enable verbose output for detailed information
53
- -r, --retries NUM Specify the number of connection retries (default 3)
54
- -t, --timeout NUM Set the connection timeout in seconds (default 10)
53
+ -r, --retries NUM Number of connection retries (default 3)
54
+ -t, --timeout NUM Set connection timeout in seconds
55
55
 
56
56
  Arguments:
57
57
  [mode] Optional connection mode (active or passive)
@@ -1,7 +1,7 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  class OptionsByExample
4
- VERSION = '3.0.0'
4
+ VERSION = '3.2.0'
5
5
  end
6
6
 
7
7
 
@@ -11,12 +11,25 @@ __END__
11
11
  # Minor version bump when backward-compatible changes or enhancements
12
12
  # Patch version bump when backward-compatible bug fixes, security updates etc
13
13
 
14
+ 3.2.0
15
+
16
+ - New method #get returns argument value or nil
17
+ - New method #fetch returns argument value or raises error
18
+ - Changed internal method #parse_without_exit to private
19
+
20
+ 3.1.0
21
+
22
+ - Support dash in argument and option names
23
+ - Method #if_present passes argument to block if present
24
+ - Method #include? return true if option is present
25
+
14
26
  3.0.0
15
27
 
16
28
  - Support options with default values
17
29
  - Improved support for one-line usage messages
18
30
  - Expand combined shorthand options into their separate components
19
31
  - Shorthand options must be single letter only
32
+ - Support options with typed arguments
20
33
 
21
34
  2.0.0
22
35
 
@@ -8,6 +8,7 @@ class OptionsByExample
8
8
 
9
9
  attr_reader :arguments
10
10
  attr_reader :options
11
+ attr_reader :usage_message
11
12
 
12
13
  def self.read(data)
13
14
  return new data.read
@@ -25,8 +26,8 @@ class OptionsByExample
25
26
 
26
27
  text =~ /Usage: (\$0|\w+)(?: \[options\])?((?: \[\w+\])*)((?: \w+)*)/
27
28
  raise RuntimeError, "Expected usage string, got none" unless $1
28
- @argument_names_optional = $2.to_s.split.map { |match| match.tr('[]', '').downcase }
29
- @argument_names_required = $3.to_s.split.map(&:downcase)
29
+ @argument_names_optional = $2.to_s.split.map { |match| sanitize match.tr('[]', '') }
30
+ @argument_names_required = $3.to_s.split.map { |match| sanitize match }
30
31
 
31
32
  # --- 2) Parse option names ---------------------------------------
32
33
  #
@@ -41,10 +42,10 @@ class OptionsByExample
41
42
 
42
43
  @option_names = {}
43
44
  @default_values = {}
44
- text.scan(/(?:(-\w), ?)?(--(\w+))(?: (\w+))?(?:.*\(default:? (\w+)\))?/) do
45
+ text.scan(/(?:(-\w), ?)?(--([\w-]+))(?: (\w+))?(?:.*\(default:? (\w+)\))?/) do
45
46
  flags = [$1, $2].compact
46
- flags.each { |each| @option_names[each] = [$3, $4] }
47
- @default_values[$3] = $5 if $5
47
+ flags.each { |each| @option_names[each] = [(sanitize $3), $4] }
48
+ @default_values[sanitize $3] = $5 if $5
48
49
  end
49
50
 
50
51
  initialize_argument_accessors
@@ -61,6 +62,27 @@ class OptionsByExample
61
62
  exit 1
62
63
  end
63
64
 
65
+ def fetch(*args, &block)
66
+ @arguments.fetch(*args, &block)
67
+ end
68
+
69
+ def get(name)
70
+ @arguments[name]
71
+ end
72
+
73
+ def if_present(name)
74
+ raise ArgumentError, 'block missing' unless block_given?
75
+
76
+ value = @arguments[name]
77
+ value.nil? ? value : (yield value)
78
+ end
79
+
80
+ def include?(name)
81
+ @options.include?(name)
82
+ end
83
+
84
+ private
85
+
64
86
  def parse_without_exit(argv)
65
87
  parser = Parser.new(
66
88
  @argument_names_required,
@@ -76,8 +98,6 @@ class OptionsByExample
76
98
  return self
77
99
  end
78
100
 
79
- private
80
-
81
101
  def initialize_argument_accessors
82
102
  [
83
103
  *@argument_names_required,
@@ -86,7 +106,7 @@ class OptionsByExample
86
106
  ].each do |argument_name|
87
107
  instance_eval %{
88
108
  def argument_#{argument_name}
89
- val = @arguments["#{argument_name}"]
109
+ val = @arguments[:#{argument_name}]
90
110
  val && block_given? ? (yield val) : val
91
111
  end
92
112
  }
@@ -97,10 +117,14 @@ class OptionsByExample
97
117
  @option_names.each_value do |option_name, _|
98
118
  instance_eval %{
99
119
  def include_#{option_name}?
100
- @options.include? "#{option_name}"
120
+ @options.include? :#{option_name}
101
121
  end
102
122
  }
103
123
  end
104
124
  end
125
+
126
+ def sanitize(string)
127
+ string.tr('^a-zA-Z0-9', '_').downcase.to_sym
128
+ end
105
129
  end
106
130
 
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: options_by_example
3
3
  version: !ruby/object:Gem::Version
4
- version: 3.0.0
4
+ version: 3.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Adrian Kuhn
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2023-05-17 00:00:00.000000000 Z
11
+ date: 2023-06-17 00:00:00.000000000 Z
12
12
  dependencies: []
13
13
  description:
14
14
  email: