bblib 2.0.1 → 2.0.3

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: f18d374a5d8b70eb58699917b789c1f4107a0041e7ae771fd7531a22ced204b9
4
- data.tar.gz: 5383474dc15df32437bec9a3b940777f781f586595c2952ef893aae4d62bd5e2
3
+ metadata.gz: debe993c7a7d4324816c660a01fb9befab19a636aaf16ee403924051a12cd838
4
+ data.tar.gz: 9e8f4aa2c6f6705ae982b086d536c93073e11725ccc8bf137f581c22a355fde5
5
5
  SHA512:
6
- metadata.gz: 5246e2b4e7bd354aba2ca09497fdc3085be2c889aeb3e4935d3673975cbfebd24b8326cb9d45be64369ed3ff3f75d3f1d39925834941c479565ede2736c060b3
7
- data.tar.gz: 6ad50086b5b92077b1aba60c652d17ead4b2304e8a5fb82df9737967f36d48cbea6267fd1a348df48678aa240dad9d4ccb1ff8af7c514be36d3492bc643de8ff
6
+ metadata.gz: ee2dff5db783cc66d30f0f9254a538014f41fbb58f81d2ec21c721d6900b6a5aa1bc8aee06b45cc3630b6e4d304c635c754a2cb6cc152ed42db61c9298db1f4f
7
+ data.tar.gz: a24156d985bded501a00d8e4e35764508dcc0a509c638c3fa9f31c528e3befe1d43014b549b4cbeb1ae5a3fbe1de6ad553ce8f489e246dc58f698f143abc7661
@@ -3,3 +3,4 @@ require_relative 'cli'
3
3
  require_relative 'system'
4
4
  require_relative 'html'
5
5
  require_relative 'cron'
6
+ require_relative 'fuzzy_matcher'
@@ -1,3 +1,4 @@
1
1
  require_relative 'core'
2
2
  require_relative 'cli/opts_parser'
3
3
  require_relative 'cli/color'
4
+ require_relative 'cli/util'
@@ -8,7 +8,7 @@ module BBLib
8
8
  attr_str :description, aliases: :desc
9
9
  attr_of Object, :default, allow_nil: true, default: nil
10
10
  attr_str :placeholder, default_proc: proc { |x| x.name.upcase }
11
- attr_ary_of String, :flags, arg_at: 1
11
+ attr_ary_of String, :flags
12
12
  attr_of [String, Regexp], :delimiter, default: nil, allow_nil: true
13
13
  attr_str :argument_delimiter, default: ' '
14
14
  attr_bool :raise_errors, default: true
@@ -25,7 +25,7 @@ module BBLib
25
25
  end
26
26
 
27
27
  def self.types
28
- descendants.map(&:type)
28
+ descendants.flat_map(&:type)
29
29
  end
30
30
 
31
31
  def retrieve(args, parsed)
@@ -33,7 +33,7 @@ module BBLib
33
33
  index = 0
34
34
  until index >= args.size
35
35
  begin
36
- unless flag_match?(args[index], index)
36
+ unless flag_match?(args[index].to_s, index)
37
37
  index += 1
38
38
  next
39
39
  end
@@ -5,7 +5,7 @@ module BBLib
5
5
  def extract(index, args)
6
6
  args.delete_at(index)
7
7
  raise MissingArgumentException, "No argument was provided for #{name}" if args[index].nil?
8
- format_value(args.delete_at(index))
8
+ format_value(args.delete_at(index).to_s)
9
9
  end
10
10
 
11
11
  protected
@@ -1,9 +1,14 @@
1
1
  module BBLib
2
2
  class OptsParser
3
3
  class Command < Option
4
+ attr_of [Integer, Range], :position, default: nil, allow_nil: true, arg_at: 0
5
+
6
+ def self.type
7
+ [super, :at]
8
+ end
4
9
 
5
10
  def extract(index, args)
6
- args.delete_at(index)
11
+ args.delete_at(index).to_s
7
12
  end
8
13
 
9
14
  end
@@ -3,7 +3,7 @@ module BBLib
3
3
  class Toggle < Option
4
4
 
5
5
  def extract(index, args)
6
- value = args[index]
6
+ value = args[index].to_s
7
7
  if value =~ /^\-[\w\d]$/ || flags.include?(value)
8
8
  args.delete_at(index)
9
9
  elsif value =~ /^\-[\w\d]+$/
@@ -1,10 +1,18 @@
1
1
  require_relative 'option'
2
2
  require_relative 'exceptions/opts_parser'
3
3
  require_relative 'options/basic_option'
4
-
5
- BBLib.scan_files(File.expand_path('../options', __FILE__), '*.rb') do |file|
6
- require_relative file
7
- end
4
+ require_relative 'options/string'
5
+ require_relative 'options/command'
6
+ require_relative 'options/bool'
7
+ require_relative 'options/date'
8
+ require_relative 'options/time'
9
+ require_relative 'options/float'
10
+ require_relative 'options/integer'
11
+ require_relative 'options/json'
12
+ require_relative 'options/regexp'
13
+ require_relative 'options/symbol'
14
+ require_relative 'options/toggle'
15
+ require_relative 'options/untoggle'
8
16
 
9
17
  module BBLib
10
18
  class OptsParser
@@ -13,11 +21,19 @@ module BBLib
13
21
  attr_ary_of Option, :options, add_rem: true
14
22
  attr_str :usage, default: nil, allow_nil: true
15
23
 
24
+ def self.build(&block)
25
+ new(&block)
26
+ end
27
+
16
28
  def usage(text = nil)
17
29
  @usage = text unless text.nil?
18
30
  @usage
19
31
  end
20
32
 
33
+ def at(position, **opts, &block)
34
+ add_options(opts.merge(type: :at, position: position, processor: block))
35
+ end
36
+
21
37
  def on(*flags, **opts, &block)
22
38
  opts[:type] = :string unless opts[:type]
23
39
  add_options(opts.merge(flags: flags, processor: block))
@@ -28,8 +44,9 @@ module BBLib
28
44
  end
29
45
 
30
46
  def parse!(args = ARGV)
47
+ args = [args] unless args.is_a?(Array)
31
48
  HashStruct.new.tap do |hash|
32
- options.each do |option|
49
+ options.sort_by { |opt| opt.position || 10**100 }.each do |option|
33
50
  option.retrieve(args, hash)
34
51
  end
35
52
  end.merge(arguments: args)
@@ -0,0 +1,46 @@
1
+ require 'io/console'
2
+ require 'io/wait'
3
+
4
+ module BBLib
5
+ module Console
6
+
7
+ DEFAULT_FILE_EDITORS = %w{vim vi notepad++ notepad}.freeze
8
+
9
+ # Simple method to open a file in a system text editor. The
10
+ # text editor can be specified otherwise the first default
11
+ # editor that can be found in the path will be used
12
+ def self.edit_file(file, editor = default_editor)
13
+ pid = spawn("#{editor} \"#{file}\"")
14
+ Process.wait(pid)
15
+ end
16
+
17
+ def self.default_editor
18
+ DEFAULT_FILE_EDITORS.find { |editor| OS.which(editor) }
19
+ end
20
+
21
+ def self.confirm?(message = 'Confirm?', yes: 'y', no: 'n', default: true, enter_is_default: true)
22
+ response = nil
23
+ until response == yes || response == no
24
+ # TODO Support carriage return to overwrite line
25
+ # print "\b" if response
26
+ print "#{message} [#{default ? 'Y/n' : 'y/N'}]: "
27
+ response = STDIN.gets.chomp.downcase
28
+ response = default ? yes : no if enter_is_default && response.empty?
29
+ end
30
+ response == yes
31
+ end
32
+
33
+ # TODO Fix this function. Currently requires two hits of enter to move on.
34
+ def self.get(limit: nil)
35
+ str = ''
36
+ loop do
37
+ char = STDIN.raw(&:getc)
38
+ STDOUT.print char
39
+ break if ["\r", "\n", "\r\n"].include?(char)
40
+ str += char
41
+ end
42
+ str.chomp
43
+ end
44
+
45
+ end
46
+ end
@@ -27,5 +27,15 @@ module BBLib
27
27
  super
28
28
  end
29
29
  end
30
+
31
+ def respond_to_missing?(method, include_private = false)
32
+ include?(method) || method.to_s =~ /\?|\=/ && include?(method.to_s[0..-2].to_sym) || super
33
+ end
34
+ end
35
+ end
36
+
37
+ class Hash
38
+ def to_hash_struct
39
+ BBLib::HashStruct.new.merge(self)
30
40
  end
31
41
  end
@@ -44,7 +44,7 @@ end
44
44
 
45
45
  module BBLib
46
46
  def self.hash_path(hash, *paths, multi_path: false, multi_join: false, multi_join_hash: false)
47
- tree = TreeHash.new(hash)
47
+ tree = TreeHash.new(hash.to_h)
48
48
  if multi_path
49
49
  tree.find_multi(*paths).map { |r| r.map { |sr| sr.value } }
50
50
  elsif multi_join
@@ -69,6 +69,7 @@ module BBLib
69
69
 
70
70
  def _ancestor_delegate_fast
71
71
  ancestors.reverse.find do |anc|
72
+ next if anc == self
72
73
  next unless anc.respond_to?(:delegate_fast)
73
74
  return anc.delegate_fast
74
75
  end
@@ -8,7 +8,18 @@ module BBLib
8
8
  base.extend(ClassMethods)
9
9
  base.send(:bridge_method, :type)
10
10
  base.send(:serialize_method, :type, always: true)
11
- base.send(:setup_init_foundation, :type) { |a, b| a && b && a.to_s.to_sym == b.to_s.to_sym }
11
+ base.send(:setup_init_foundation, :type) do |a, b|
12
+ if a && b
13
+ case
14
+ when a.is_a?(Array)
15
+ a.include?(b)
16
+ else
17
+ a.to_s.to_sym == b.to_s.to_sym
18
+ end
19
+ else
20
+ false
21
+ end
22
+ end
12
23
  end
13
24
 
14
25
  module ClassMethods
@@ -1,3 +1,3 @@
1
1
  module BBLib
2
- VERSION = '2.0.1'.freeze
2
+ VERSION = '2.0.3'.freeze
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: bblib
3
3
  version: !ruby/object:Gem::Version
4
- version: 2.0.1
4
+ version: 2.0.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - Brandon Black
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2018-09-12 00:00:00.000000000 Z
11
+ date: 2018-09-21 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -109,6 +109,7 @@ files:
109
109
  - lib/bblib/cli/options/toggle.rb
110
110
  - lib/bblib/cli/options/untoggle.rb
111
111
  - lib/bblib/cli/opts_parser.rb
112
+ - lib/bblib/cli/util.rb
112
113
  - lib/bblib/core.rb
113
114
  - lib/bblib/core/classes/hash_struct.rb
114
115
  - lib/bblib/core/classes/splitter.rb