bblib 2.0.1 → 2.0.3
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 +4 -4
- data/lib/bblib/all.rb +1 -0
- data/lib/bblib/cli.rb +1 -0
- data/lib/bblib/cli/option.rb +3 -3
- data/lib/bblib/cli/options/basic_option.rb +1 -1
- data/lib/bblib/cli/options/command.rb +6 -1
- data/lib/bblib/cli/options/toggle.rb +1 -1
- data/lib/bblib/cli/opts_parser.rb +22 -5
- data/lib/bblib/cli/util.rb +46 -0
- data/lib/bblib/core/classes/hash_struct.rb +10 -0
- data/lib/bblib/core/hash_path/hash_path.rb +1 -1
- data/lib/bblib/core/mixins/delegator.rb +1 -0
- data/lib/bblib/core/mixins/type_init.rb +12 -1
- data/lib/bblib/version.rb +1 -1
- metadata +3 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: debe993c7a7d4324816c660a01fb9befab19a636aaf16ee403924051a12cd838
|
4
|
+
data.tar.gz: 9e8f4aa2c6f6705ae982b086d536c93073e11725ccc8bf137f581c22a355fde5
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: ee2dff5db783cc66d30f0f9254a538014f41fbb58f81d2ec21c721d6900b6a5aa1bc8aee06b45cc3630b6e4d304c635c754a2cb6cc152ed42db61c9298db1f4f
|
7
|
+
data.tar.gz: a24156d985bded501a00d8e4e35764508dcc0a509c638c3fa9f31c528e3befe1d43014b549b4cbeb1ae5a3fbe1de6ad553ce8f489e246dc58f698f143abc7661
|
data/lib/bblib/all.rb
CHANGED
data/lib/bblib/cli.rb
CHANGED
data/lib/bblib/cli/option.rb
CHANGED
@@ -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
|
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.
|
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
|
@@ -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
|
@@ -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
|
-
|
6
|
-
|
7
|
-
|
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
|
@@ -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)
|
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
|
data/lib/bblib/version.rb
CHANGED
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.
|
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-
|
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
|