optparsegen 0.1.0 → 0.1.1

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
  SHA1:
3
- metadata.gz: c224a84b9f8bc19e937d687806e1171f2b8d9c00
4
- data.tar.gz: aed0a9eda0f3e95194a4f0e4320dee8d263fe4eb
3
+ metadata.gz: 11d862b2289f78169b97befd8f367bca288c7f33
4
+ data.tar.gz: 6a740ed0221851cc867468f1965d4c60a2372d6b
5
5
  SHA512:
6
- metadata.gz: 58d646498865d522f5eb80e81c52005cd77cb26469e153b4acebfb690ff4a4f0d3af6ff6a50f6b6c78313ff720794791c67dccd8c5e04172ffb6c0459d09c5b0
7
- data.tar.gz: fdc547b174c848784af0c117d4310142c48ef8991b375295273a927c519857b2020377b84f2e9f571ff40286df5064d5cf8ef8e7edc7cf0bb1b8bfea00e0dc24
6
+ metadata.gz: fbd749b74065c24fb2e6f21c97ba6b651abbfd181d33d6bcdd9cc5f32b94da0647e87798443ae55d8e59ed3918e31befcb55bfc617775bf6e0e7a323f01c0250
7
+ data.tar.gz: 28763b8476ec6e30948bd86c8abf15decc3012e221bb2bbcce0f4dbe38f1b4c6810ef6fc460f32579e67bd307438856968a5a0d3f57140baa2574fe338f0d328
data/bin/optparsegen CHANGED
@@ -11,6 +11,9 @@ optparsegen converts program usage text into equivalent ruby 'optparse'
11
11
  code. It reads the text from the filename passed as the only argument
12
12
  on the command line or from standard input.
13
13
 
14
+ Example:
15
+ java -jar some-scopt-using.jar --help | optparsegen > optparse.rb
16
+
14
17
  Options:
15
18
  )
16
19
 
@@ -24,4 +27,4 @@ Options:
24
27
  end
25
28
  end.parse!
26
29
 
27
- OptParseGen.generate(ARGF.read, options.format)
30
+ puts OptParseGen.generate(ARGF.read, options.format)
data/lib/optparsegen.rb CHANGED
@@ -1,13 +1,54 @@
1
+ require 'erb'
2
+
1
3
  # The main entry point for OptParseGen.
2
4
  class OptParseGen
3
5
  # Represents a single option.
4
- Option = Struct.new(:name, :short, :description)
6
+ Option = Struct.new(:name, :short, :description) do
7
+ def underscored_name
8
+ name.sub('-', '_')
9
+ end
10
+
11
+ def symbol_name
12
+ ":#{underscored_name}"
13
+ end
14
+
15
+ def opt_args
16
+ args = []
17
+ args.push "--#{name}" if name
18
+ args.push "-#{short}" if short
19
+ args.push description if description
20
+ args
21
+ end
22
+ end
23
+
24
+ # An ERB based renderer.
25
+ class Renderer
26
+ def self.render(options)
27
+ ERB.new(%Q(require 'optparse'
28
+
29
+ options = Struct.new(<%= options.map { |o| o.symbol_name }.join(', ') %>).new
30
+
31
+ OptionParser.new do |opts|
32
+ <% for o in options.select { |o| o.name != "help" } %>
33
+ opts.on(<%= o.opt_args.map { |arg| "'\#{arg}'" }.join(', ') %>) do |v|
34
+ options.<%= o.underscored_name %> = v
35
+ end
36
+
37
+ <% end %>
38
+ opts.on_tail('-h', '--help', 'Shows this usage text.') do
39
+ puts opts
40
+ exit
41
+ end
42
+ end.parse!
43
+ ), 0, '>').result(binding)
44
+ end
45
+ end
5
46
 
6
47
  # Generates 'optparse' code from the given input and format.
7
48
  def self.generate(input, format)
8
49
  require "optparsegen/#{format}"
9
50
  parser = OptParseGen.const_get("#{format.capitalize}Parser").new(input)
10
51
  options = parser.parse
11
- puts options
52
+ Renderer.render(options)
12
53
  end
13
54
  end
@@ -0,0 +1,33 @@
1
+ require 'minitest/autorun'
2
+ require 'optparsegen'
3
+
4
+ class RenderTest < Minitest::Test
5
+ def test_render
6
+ options = [
7
+ OptParseGen::Option.new('test', 't', 'A description.'),
8
+ OptParseGen::Option.new('another', nil, 'Without a short parameter.')
9
+ ]
10
+
11
+ result = OptParseGen::Renderer.render(options)
12
+
13
+ assert_equal(%Q(require 'optparse'
14
+
15
+ options = Struct.new(:test, :another).new
16
+
17
+ OptionParser.new do |opts|
18
+ opts.on('--test', '-t', 'A description.') do |v|
19
+ options.test = v
20
+ end
21
+
22
+ opts.on('--another', 'Without a short parameter.') do |v|
23
+ options.another = v
24
+ end
25
+
26
+ opts.on_tail('-h', '--help', 'Shows this usage text.') do
27
+ puts opts
28
+ exit
29
+ end
30
+ end.parse!
31
+ ), result)
32
+ end
33
+ end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: optparsegen
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.1.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Tyler Sommer
@@ -20,6 +20,7 @@ files:
20
20
  - bin/optparsegen
21
21
  - lib/optparsegen.rb
22
22
  - lib/optparsegen/scopt.rb
23
+ - test/test_render.rb
23
24
  - test/test_scopt.rb
24
25
  homepage:
25
26
  licenses:
@@ -46,4 +47,5 @@ signing_key:
46
47
  specification_version: 4
47
48
  summary: Generate ruby optparse code from usage text.
48
49
  test_files:
50
+ - test/test_render.rb
49
51
  - test/test_scopt.rb