baf 0.2.0 → 0.2.1

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
  SHA1:
3
- metadata.gz: 150c3cca7fba34de369d231d80ac5d40570b788e
4
- data.tar.gz: eae88fb291406234386f09da17458982395d4af3
3
+ metadata.gz: 4ae9e185856672962cc03ee541efbfaeaacf3f64
4
+ data.tar.gz: 96e339bd95ee8a9695973df19dba8a0fa26220ed
5
5
  SHA512:
6
- metadata.gz: acf3c301be34c11193d54e90234ce1cfba6420ac1ed8f721ac032fa7a47d342f41978df191c3f9283329e1f2a420c51786b3c80d24cd304973db3a30d343de32
7
- data.tar.gz: e5993ca64496e85ee17ebb676a1a996348afa12b5da7201c7b471cce4f2bbda5f9bbb6f86363bd5fb7f56425e012c38daa077fffb772660b6f0f91b212c1d0dc
6
+ metadata.gz: 3c43bd18e04dbddb92053d3842bd232ff7b889aa4690da333268ff738ccaa3331115b5462c5accbc981a95fda149baab7a22c2f26fbc7910fffcd283d509d12d
7
+ data.tar.gz: aecf2e2f7d9c2b58c95739613a933c9dee2846d699c3f3c09250b29b4fe22b8f708a2a2e873fc914cae502d9ccbfdccc31df97dd4f61bb1c34919424c49db5cc
@@ -35,8 +35,7 @@ module Baf
35
35
  @parser = opts[:parser] || OptionParser.new
36
36
  @registrant = opts[:registrant] || OptionsRegistrant.new(env, parser)
37
37
 
38
- registrant.register_default_options
39
- setup
38
+ registrant.register { setup }
40
39
  end
41
40
 
42
41
  def setup
@@ -47,15 +46,16 @@ module Baf
47
46
  end
48
47
 
49
48
  def flag_debug
50
- flag :d, 'debug'
49
+ flag :d, :debug
51
50
  end
52
51
 
53
52
  def flag_verbose
54
- flag :v, 'verbose'
53
+ flag :v, :verbose
55
54
  end
56
55
 
57
56
  def flag_version version
58
- flag :V, 'version', 'print version', -> env { env.puts version }
57
+ flag :V, :version, 'print version', -> env { env.puts version },
58
+ tail: true
59
59
  end
60
60
 
61
61
  def option *args
@@ -1,10 +1,20 @@
1
1
  module Baf
2
2
  class Option
3
- attr_accessor :short, :long, :arg, :desc, :block
3
+ attr_accessor :short, :long, :arg, :desc, :block, :flag, :tail
4
4
 
5
- def initialize *args
5
+ def initialize *args, flag: false, tail: false
6
6
  attrs = args.size > 1 ? build_attrs(*args) : args[0]
7
7
  attrs.each { |k, v| send :"#{k}=", v }
8
+ self.flag = flag
9
+ self.tail = tail
10
+ end
11
+
12
+ def flag?
13
+ !!flag
14
+ end
15
+
16
+ def tail?
17
+ !!tail
8
18
  end
9
19
 
10
20
  def to_parser_arguments
@@ -13,7 +23,7 @@ module Baf
13
23
 
14
24
  protected
15
25
 
16
- def build_attrs(short, long, arg_or_desc = nil, desc_or_block = nil)
26
+ def build_attrs short, long, arg_or_desc = nil, desc_or_block = nil
17
27
  {
18
28
  short: short,
19
29
  long: long
@@ -1,41 +1,35 @@
1
+ require 'baf/option'
2
+
1
3
  module Baf
2
4
  class OptionsRegistrant
3
- def initialize env, parser
4
- @env = env
5
- @parser = parser
6
- end
5
+ SUMMARY_HEADER = "\noptions:".freeze
7
6
 
8
- def register_default_options
9
- parser.separator ''
10
- parser.separator 'options:'
11
- parser.on_tail '-h', '--help', 'print this message' do
12
- env.print parser
13
- end
7
+ def initialize env, parser, options = []
8
+ @env = env
9
+ @parser = parser
10
+ @options = options
14
11
  end
15
12
 
16
- def flag *args
17
- opt = Option.new(*args)
18
- if opt.block
19
- parser.on(*opt.to_parser_arguments) { opt.block[env] }
20
- else
21
- define_env_flag env, opt.long
22
- parser.on "-#{opt.short}", "--#{opt.long}", "enable #{opt.long} mode" do
23
- env.send :"#{opt.long}=", true
24
- end
25
- end
13
+ def flag *args, **opts
14
+ options << Option.new(*args, flag: true, **opts)
26
15
  end
27
16
 
28
17
  def option *args
29
- opt = Option.new(*args)
30
- define_env_accessor env, opt.long
31
- parser.on *opt.to_parser_arguments do |v|
32
- env.send :"#{opt.long}=", v
18
+ options << Option.new(*args)
19
+ end
20
+
21
+ def register
22
+ yield if block_given?
23
+ parser.separator SUMMARY_HEADER
24
+ options.each do |opt|
25
+ if opt.flag? then register_flag opt else register_option opt end
33
26
  end
27
+ register_default_options
34
28
  end
35
29
 
36
30
  protected
37
31
 
38
- attr_reader :env, :parser, :config
32
+ attr_reader :env, :parser, :options
39
33
 
40
34
  def define_env_accessor env, name
41
35
  (class << env; self end).send :attr_accessor, name
@@ -49,5 +43,42 @@ module Baf
49
43
  end
50
44
  env.instance_variable_set :"@#{name}", false
51
45
  end
46
+
47
+ def env_flag! name
48
+ env_flag_set name, true
49
+ end
50
+
51
+ def env_flag_set name, value
52
+ env.send :"#{name}=", value
53
+ end
54
+
55
+ def register_default_options
56
+ parser.separator '' if options.any?
57
+ parser.on_tail '-h', '--help', 'print this message' do
58
+ env.print parser
59
+ end
60
+ end
61
+
62
+ def register_flag opt
63
+ position = opt.tail ? :on_tail : :on
64
+ if opt.block
65
+ parser.send position, *opt.to_parser_arguments do
66
+ opt.block[env]
67
+ end
68
+ else
69
+ define_env_flag env, opt.long
70
+ parser.send position,
71
+ "-#{opt.short}", "--#{opt.long}", "enable #{opt.long} mode" do
72
+ env_flag! opt.long
73
+ end
74
+ end
75
+ end
76
+
77
+ def register_option opt
78
+ define_env_accessor env, opt.long
79
+ parser.on *opt.to_parser_arguments do |v|
80
+ self.env_flag_set opt.long, v
81
+ end
82
+ end
52
83
  end
53
84
  end
@@ -1,3 +1,3 @@
1
1
  module Baf
2
- VERSION = '0.2.0'.freeze
2
+ VERSION = '0.2.1'.freeze
3
3
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: baf
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.0
4
+ version: 0.2.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Thibault Jouan