baf 0.2.1 → 0.4.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
  SHA1:
3
- metadata.gz: 4ae9e185856672962cc03ee541efbfaeaacf3f64
4
- data.tar.gz: 96e339bd95ee8a9695973df19dba8a0fa26220ed
3
+ metadata.gz: e012a75cf9bb4b0b0a480a05a226734bb99f5bda
4
+ data.tar.gz: 0fec7f1fddc5e577b8b788c3fbd88b4d318ebcf9
5
5
  SHA512:
6
- metadata.gz: 3c43bd18e04dbddb92053d3842bd232ff7b889aa4690da333268ff738ccaa3331115b5462c5accbc981a95fda149baab7a22c2f26fbc7910fffcd283d509d12d
7
- data.tar.gz: aecf2e2f7d9c2b58c95739613a933c9dee2846d699c3f3c09250b29b4fe22b8f708a2a2e873fc914cae502d9ccbfdccc31df97dd4f61bb1c34919424c49db5cc
6
+ metadata.gz: 6f2729161ce545abfa3c88ebaa6f599b56b6858824a11cf41b0b72e954e8deb50be03953f59e5e8490860c0ec10049346e6f0bdaf70c38d0450588e57821a95d
7
+ data.tar.gz: d98e8d9e303aa0bd7cd4d1028cc7275839d88cdb9a39afb061d9d8c9b316cdec4fc44f836e37a643bc3b4ffcdc669fd2711c7a280970bd6d8dd39a1cae8c51b6
@@ -2,6 +2,7 @@ require 'optparse'
2
2
 
3
3
  require 'baf'
4
4
  require 'baf/env'
5
+ require 'baf/flag'
5
6
  require 'baf/option'
6
7
  require 'baf/options_registrant'
7
8
 
@@ -33,9 +34,9 @@ module Baf
33
34
  @env = env
34
35
  @arguments = arguments
35
36
  @parser = opts[:parser] || OptionParser.new
36
- @registrant = opts[:registrant] || OptionsRegistrant.new(env, parser)
37
+ @registrant = opts[:registrant] || OptionsRegistrant.new
37
38
 
38
- registrant.register { setup }
39
+ registrant.register(env, parser) { setup }
39
40
  end
40
41
 
41
42
  def setup
@@ -0,0 +1,21 @@
1
+ require 'baf/option'
2
+
3
+ module Baf
4
+ class Flag < Option
5
+ DESCRIPTION_FORMAT = 'enable %s mode'.freeze
6
+
7
+ def env_definition
8
+ :predicate
9
+ end
10
+
11
+ private
12
+
13
+ def parser_argument_desc
14
+ if desc then super else DESCRIPTION_FORMAT % long end
15
+ end
16
+
17
+ def parser_argument_block env
18
+ -> * { env.send :"#{long}=", true }
19
+ end
20
+ end
21
+ end
@@ -1,27 +1,39 @@
1
1
  module Baf
2
2
  class Option
3
- attr_accessor :short, :long, :arg, :desc, :block, :flag, :tail
3
+ LONG_PREFIX = '--'.freeze
4
+ LONG_NORMALIZE_SEARCH = ?_.freeze
5
+ LONG_NORMALIZE_REPLACE = ?-.freeze
6
+ LONG_WITH_ARG_GLUE = ' '.freeze
7
+ PARSER_MESSAGE = :on
8
+ PARSER_MESSAGE_TAIL = :on_tail
4
9
 
5
- def initialize *args, flag: false, tail: false
10
+ attr_accessor :short, :long, :arg, :desc, :block, :tail
11
+
12
+ def initialize *args, tail: false
6
13
  attrs = args.size > 1 ? build_attrs(*args) : args[0]
7
14
  attrs.each { |k, v| send :"#{k}=", v }
8
- self.flag = flag
9
15
  self.tail = tail
10
16
  end
11
17
 
12
- def flag?
13
- !!flag
18
+ def env_definition
19
+ :accessor
20
+ end
21
+
22
+ def block?
23
+ !!block
14
24
  end
15
25
 
16
26
  def tail?
17
27
  !!tail
18
28
  end
19
29
 
20
- def to_parser_arguments
21
- ["-#{short}", parser_argument_long, desc]
30
+ def to_parser_arguments env
31
+ message = tail? ? PARSER_MESSAGE_TAIL : PARSER_MESSAGE
32
+ mblock = block ? -> * { block[env] } : parser_argument_block(env)
33
+ [message, "-#{short}", parser_argument_long, parser_argument_desc, mblock]
22
34
  end
23
35
 
24
- protected
36
+ private
25
37
 
26
38
  def build_attrs short, long, arg_or_desc = nil, desc_or_block = nil
27
39
  {
@@ -45,9 +57,19 @@ module Baf
45
57
 
46
58
  def parser_argument_long
47
59
  [
48
- '--' + long.to_s.tr(?_, ?-),
60
+ LONG_PREFIX + long
61
+ .to_s
62
+ .tr(LONG_NORMALIZE_SEARCH, LONG_NORMALIZE_REPLACE),
49
63
  arg
50
- ].compact.join ' '
64
+ ].compact.join LONG_WITH_ARG_GLUE
65
+ end
66
+
67
+ def parser_argument_desc
68
+ desc
69
+ end
70
+
71
+ def parser_argument_block env
72
+ -> v { env.send :"#{long}=", v }
51
73
  end
52
74
  end
53
75
  end
@@ -1,41 +1,45 @@
1
+ require 'baf/flag'
1
2
  require 'baf/option'
2
3
 
3
4
  module Baf
4
5
  class OptionsRegistrant
5
- SUMMARY_HEADER = "\noptions:".freeze
6
+ SUMMARY_HEADER = "\noptions:".freeze
7
+ HELP_PARSER_ARGS = %w[
8
+ -h
9
+ --help
10
+ print\ this\ message
11
+ ].freeze.each &:freeze
6
12
 
7
- def initialize env, parser, options = []
8
- @env = env
9
- @parser = parser
10
- @options = options
13
+ def initialize options = []
14
+ @options = options
11
15
  end
12
16
 
13
17
  def flag *args, **opts
14
- options << Option.new(*args, flag: true, **opts)
18
+ options << Flag.new(*args, **opts)
15
19
  end
16
20
 
17
21
  def option *args
18
22
  options << Option.new(*args)
19
23
  end
20
24
 
21
- def register
25
+ def register env, parser
22
26
  yield if block_given?
23
27
  parser.separator SUMMARY_HEADER
24
- options.each do |opt|
25
- if opt.flag? then register_flag opt else register_option opt end
26
- end
27
- register_default_options
28
+ options_tail, options_standard = options.partition &:tail?
29
+ options_standard.each { |opt|register_option env, parser, opt }
30
+ register_default_options env, parser
31
+ options_tail.each { |opt|register_option env, parser, opt }
28
32
  end
29
33
 
30
- protected
34
+ private
31
35
 
32
- attr_reader :env, :parser, :options
36
+ attr_reader :options
33
37
 
34
38
  def define_env_accessor env, name
35
39
  (class << env; self end).send :attr_accessor, name
36
40
  end
37
41
 
38
- def define_env_flag env, name
42
+ def define_env_predicate env, name
39
43
  define_env_accessor env, name
40
44
  env.send :"#{name}=", false
41
45
  env.define_singleton_method :"#{name}?" do
@@ -44,41 +48,17 @@ module Baf
44
48
  env.instance_variable_set :"@#{name}", false
45
49
  end
46
50
 
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
51
+ def register_default_options env, parser
56
52
  parser.separator '' if options.any?
57
- parser.on_tail '-h', '--help', 'print this message' do
53
+ parser.on_tail *HELP_PARSER_ARGS do
58
54
  env.print parser
59
55
  end
60
56
  end
61
57
 
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
58
+ def register_option env, parser, opt
59
+ send :"define_env_#{opt.env_definition}", env, opt.long unless opt.block?
60
+ *args, block = opt.to_parser_arguments env
61
+ parser.send *args, &block
82
62
  end
83
63
  end
84
64
  end
@@ -1,3 +1,3 @@
1
1
  module Baf
2
- VERSION = '0.2.1'.freeze
2
+ VERSION = '0.4.0'.freeze
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: baf
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.1
4
+ version: 0.4.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Thibault Jouan
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2016-03-27 00:00:00.000000000 Z
11
+ date: 2016-03-28 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: baf-testing
@@ -47,6 +47,7 @@ files:
47
47
  - lib/baf.rb
48
48
  - lib/baf/cli.rb
49
49
  - lib/baf/env.rb
50
+ - lib/baf/flag.rb
50
51
  - lib/baf/option.rb
51
52
  - lib/baf/options_registrant.rb
52
53
  - lib/baf/testing/cucumber.rb