options_by_example 1.3.0 → 2.0.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 26769b40ccdbee8bcb810e85755f7e3774145dda2ea8860edda1c99a65e168a5
4
- data.tar.gz: 5d11ede203262af12bb65613ea5d153bb699188443453d82f8fb5bccb13068a1
3
+ metadata.gz: eb87e6b0c01a7f99d70d600c6adc142d96cae3fc18b122c2259c926504fb198b
4
+ data.tar.gz: e21e7dd4f0a4a77fb7e2a84b4edc77293023730388e0fcbf4abe2d0e5344d5c5
5
5
  SHA512:
6
- metadata.gz: 1cb7a3d1732b19543557ed113094204451f9d24e2fae91029aa19d3f2985482e5536cc8c185e1dbef15fd77d330dd7ea76e7d0a6c596f7e198278f4afdf3bc74
7
- data.tar.gz: 61a4d2a408fbe795040f7963e6d9cdb82076872bf7118c7fbb633b940684bd5eed9884f2d4a3f73cc4f321eb4e976d9ff5b252a17694a6316c4849314a5aa586
6
+ metadata.gz: 7e5b59dc4a4b9ad41edeaadaf98fa3ee23abe4f262cfbb9ba93752dba66ff5868b704024a3d534b43386977caefe6a4eecea4a5e80c22f5c351f679ec2488bc3
7
+ data.tar.gz: 7ecce96f859a83e3dc702740c3d5e72c957d44a42387dff4a70db249bc54fc1df45484bf5ff099e9bbd66d9dae06a548d179f107aa5f6cb87395a36a54551bc0
@@ -2,6 +2,10 @@
2
2
 
3
3
 
4
4
  class OptionsByExample
5
+
6
+ class PrintUsageMessage < StandardError
7
+ end
8
+
5
9
  class Parser
6
10
 
7
11
  attr_reader :options
@@ -46,7 +50,7 @@ class OptionsByExample
46
50
  @chunks.each do |option, *args|
47
51
  case option
48
52
  when '-h', '--help'
49
- raise "puts @usage_message"
53
+ raise PrintUsageMessage
50
54
  end
51
55
  end
52
56
  end
@@ -1,7 +1,7 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  class OptionsByExample
4
- VERSION = '1.3.0'
4
+ VERSION = '2.0.0'
5
5
  end
6
6
 
7
7
 
@@ -11,9 +11,14 @@ __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
+ 2.0.0
15
+
16
+ - Replaced dynamic methods with explicit methods for options and arguments
17
+ - Removed ability to call dynamic methods with undeclared names
18
+
14
19
  1.3.0
15
20
 
16
- - Extract parsing functionality into class
21
+ - Extracted parsing functionality into class
17
22
  - Better error messages
18
23
 
19
24
  1.2.0
@@ -6,20 +6,14 @@ require 'options_by_example/parser'
6
6
 
7
7
  class OptionsByExample
8
8
 
9
- attr_reader :argument_names_optional
10
- attr_reader :argument_names_required
11
- attr_reader :option_names
12
-
13
9
  attr_reader :arguments
14
10
  attr_reader :options
15
11
 
16
-
17
12
  def self.read(data)
18
13
  return new data.read
19
14
  end
20
15
 
21
16
  def initialize(text)
22
- @settings = {exit_on_error: true}
23
17
  @usage_message = text.gsub('$0', File.basename($0)).gsub(/\n+\Z/, "\n\n")
24
18
 
25
19
  # --- 1) Parse argument names -------------------------------------
@@ -50,15 +44,22 @@ class OptionsByExample
50
44
  opts = $1.split(", ")
51
45
  opts.each { |each| @option_names[each] = [opts.last.tr('-', ''), $4] }
52
46
  end
53
- end
54
47
 
55
- def use(settings)
56
- @settings.update settings
57
-
58
- return self
48
+ initialize_argument_accessors
49
+ initialize_option_accessors
59
50
  end
60
51
 
61
52
  def parse(argv)
53
+ parse_without_exit argv
54
+ rescue PrintUsageMessage
55
+ puts @usage_message
56
+ exit 0
57
+ rescue RuntimeError => err
58
+ puts "ERROR: #{err.message}"
59
+ exit 1
60
+ end
61
+
62
+ def parse_without_exit(argv)
62
63
  parser = Parser.new(
63
64
  @argument_names_required,
64
65
  @argument_names_optional,
@@ -66,33 +67,36 @@ class OptionsByExample
66
67
  )
67
68
 
68
69
  parser.parse argv
69
-
70
- @options = parser.options
71
70
  @arguments = parser.arguments
71
+ @options = parser.options
72
72
 
73
73
  return self
74
- rescue RuntimeError => err
75
- raise unless @settings[:exit_on_error]
74
+ end
76
75
 
77
- if err.message == "puts @usage_message"
78
- puts @usage_message
79
- else
80
- puts "ERROR: #{err.message}"
76
+ private
77
+
78
+ def initialize_argument_accessors
79
+ [
80
+ *@argument_names_required,
81
+ *@argument_names_optional,
82
+ *@option_names.values.select(&:last).map(&:first),
83
+ ].each do |argument_name|
84
+ instance_eval %{
85
+ def argument_#{argument_name}
86
+ val = @arguments["#{argument_name}"]
87
+ val && block_given? ? (yield val) : val
88
+ end
89
+ }
81
90
  end
82
-
83
- exit 1
84
91
  end
85
92
 
86
-
87
- def method_missing(sym, *args, &block)
88
- case sym
89
- when /^argument_(\w+)$/
90
- val = @arguments[$1]
91
- block && val ? block.call(val) : val
92
- when /^include_(\w+)\?$/
93
- @options[$1]
94
- else
95
- super
93
+ def initialize_option_accessors
94
+ @option_names.each_value do |option_name, _|
95
+ instance_eval %{
96
+ def include_#{option_name}?
97
+ @options.include? "#{option_name}"
98
+ end
99
+ }
96
100
  end
97
101
  end
98
102
  end
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: 1.3.0
4
+ version: 2.0.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-04-29 00:00:00.000000000 Z
11
+ date: 2023-04-30 00:00:00.000000000 Z
12
12
  dependencies: []
13
13
  description:
14
14
  email:
@@ -27,6 +27,7 @@ licenses:
27
27
  metadata:
28
28
  homepage_uri: https://github.com/akuhn/options_by_example
29
29
  source_code_uri: https://github.com/akuhn/options_by_example
30
+ changelog_uri: https://github.com/akuhn/options_by_example/blob/master/lib/options_by_example/version.rb
30
31
  post_install_message:
31
32
  rdoc_options: []
32
33
  require_paths: