eac_cli 0.12.6 → 0.16.0

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
  SHA256:
3
- metadata.gz: e9465f0f0d648395bb6616a208f830034405eaed91e6d186f8235cd2b0ea28a6
4
- data.tar.gz: efd2ab2ad3737f7f12577908cc3c0bda2fbead8ddaa99ae671c72162a3f63bb3
3
+ metadata.gz: 901db493185f12775a163c9d1b017b32e520a62d48952eead7d6fef94a0dde64
4
+ data.tar.gz: 474e78827b8ddd3b91f94a65fc250a9aaa36caece0377de75206c344cac06318
5
5
  SHA512:
6
- metadata.gz: 88e58527420ebd47d4de3684bdd6d2c18eb6b5b1be87c33043b0d3951d924265d44b9108feed7f3c0cd236ef11546ecf188c2a1c0de0ab73e0b4740c2df63287
7
- data.tar.gz: 45fb719847efcade7e5c56b015ebd8aefce718d109f11aebb97f7645a681ec5ed53358a3a7beebc63019b77e3847b2be69421e1b9b8bcaacbf7682ecaafdec20
6
+ metadata.gz: f5594e5963d7e093ee29ceb4214e5d42deac0b59ca449d64dcbc06c2998806bf1011fdf592dfe395ad8269caca8b0b3d79656ab2d8f47a93594833b211406a01
7
+ data.tar.gz: 89345d413dbd1c7cd52f1fa4ae08c20a2276f307e7c557e94ddf66dbbbbe14f967f0ec7570ac14e40921679702f1b5a7a97c31e69dc1b718e81727f2c4eab97c
@@ -2,3 +2,5 @@
2
2
 
3
3
  require 'eac_ruby_utils/core_ext'
4
4
  require 'eac_cli/patches'
5
+
6
+ ::EacCli::RunnerWithSet.default.add_namespace(::EacCli::RunnerWith)
@@ -10,15 +10,12 @@ module EacCli
10
10
  SUBCOMMAND_NAME_ARG = :subcommand
11
11
  SUBCOMMAND_ARGS_ARG = :subcommand_args
12
12
 
13
- def arg_opt(short, long, description, option_options = {})
14
- options_set << ::EacCli::Definition::ArgumentOption.new(
15
- short, long, description, option_options
16
- )
13
+ def arg_opt(*args)
14
+ options_set << ::EacCli::Definition::ArgumentOption.from_args(args)
17
15
  end
18
16
 
19
- def bool_opt(short, long, description, option_options = {})
20
- options_set << ::EacCli::Definition::BooleanOption.new(short, long, description,
21
- option_options)
17
+ def bool_opt(*args)
18
+ options_set << ::EacCli::Definition::BooleanOption.from_args(args)
22
19
  end
23
20
 
24
21
  def options
@@ -8,6 +8,14 @@ module EacCli
8
8
  def argument?
9
9
  true
10
10
  end
11
+
12
+ def build_value(new_value, previous_value)
13
+ repeat? ? previous_value + [new_value] : new_value
14
+ end
15
+
16
+ def default_value
17
+ repeat? ? [] : nil
18
+ end
11
19
  end
12
20
  end
13
21
  end
@@ -5,24 +5,36 @@ require 'eac_ruby_utils/core_ext'
5
5
  module EacCli
6
6
  class Definition
7
7
  class BaseOption
8
+ require_sub __FILE__
9
+
10
+ class << self
11
+ def from_args(args)
12
+ p = ::EacCli::Definition::BaseOption::InitializeArgsParser.new(args)
13
+ new(p.short, p.long, p.description, p.options)
14
+ end
15
+ end
16
+
8
17
  DEFAULT_REQUIRED = false
9
18
 
10
19
  enable_listable
11
- lists.add_symbol :option, :optional, :usage, :required
12
- attr_reader :short, :long, :description, :options
13
-
14
- def initialize(short, long, description, options = {})
15
- @short = short
16
- @long = long
17
- @description = description
18
- @options = options.symbolize_keys
19
- @options.assert_valid_keys(::EacCli::Definition::BaseOption.lists.option.values)
20
+ enable_abstract_methods :build_value, :default_value
21
+ lists.add_symbol :option, :optional, :usage, :repeat, :required
22
+ common_constructor :short, :long, :description, :options, default: [{}] do
23
+ raise 'Nor short neither long selector was set' if short.blank? && long.blank?
24
+
25
+ self.options = ::EacCli::Definition::BaseOption.lists.option.hash_keys_validate!(
26
+ options.symbolize_keys
27
+ )
20
28
  end
21
29
 
22
30
  def identifier
23
31
  long.to_s.variableize.to_sym
24
32
  end
25
33
 
34
+ def repeat?
35
+ options[OPTION_REPEAT]
36
+ end
37
+
26
38
  def required?
27
39
  return true if options.key?(:required) && options.fetch(:required)
28
40
  return false if options.key?(:optional) && options.fetch(:optional)
@@ -0,0 +1,47 @@
1
+ # frozen_string_literal: true
2
+
3
+ module EacCli
4
+ class Definition
5
+ class BaseOption
6
+ class InitializeArgsParser
7
+ PROPERTIES = %i[short long description options].freeze
8
+ attr_reader(*PROPERTIES)
9
+
10
+ def initialize(args)
11
+ self.options = args.extract_options!.freeze
12
+ args.each { |arg| absorb_arg(arg) }
13
+ end
14
+
15
+ private
16
+
17
+ attr_writer(*PROPERTIES)
18
+
19
+ def absorb_arg(arg)
20
+ arg_ext = ArgumentParser.new(arg)
21
+ send("#{arg_ext.type}=", arg_ext.value)
22
+ end
23
+
24
+ class ArgumentParser
25
+ TYPES = %i[short long description].freeze
26
+ common_constructor :value
27
+
28
+ def type
29
+ TYPES.find { |type| send("#{type}?") } || raise("Unknown type for \"#{value}\"")
30
+ end
31
+
32
+ def short?
33
+ value.start_with?('-') && !long?
34
+ end
35
+
36
+ def long?
37
+ value.start_with?('--')
38
+ end
39
+
40
+ def description?
41
+ !short? || !long?
42
+ end
43
+ end
44
+ end
45
+ end
46
+ end
47
+ end
@@ -8,6 +8,14 @@ module EacCli
8
8
  def argument?
9
9
  false
10
10
  end
11
+
12
+ def build_value(_new_value, previous_value)
13
+ repeat? ? previous_value + 1 : true
14
+ end
15
+
16
+ def default_value
17
+ repeat? ? 0 : false
18
+ end
11
19
  end
12
20
  end
13
21
  end
@@ -13,6 +13,18 @@ module EacCli
13
13
  options.assert_valid_keys(self.class.lists.option.values)
14
14
  end
15
15
 
16
+ def build_value(new_value, previous_value)
17
+ if previous_value.is_a?(::Array)
18
+ previous_value + [new_value]
19
+ else
20
+ new_value
21
+ end
22
+ end
23
+
24
+ def default_value
25
+ repeat? ? [] : nil
26
+ end
27
+
16
28
  def identifier
17
29
  name.to_s.variableize.to_sym
18
30
  end
@@ -19,10 +19,24 @@ module EacCli
19
19
  b += '=<value>' if option.argument?
20
20
  b
21
21
  end
22
+
23
+ def option_short(option)
24
+ b = option.short
25
+ b += '=<value>' if option.argument?
26
+ b
27
+ end
28
+
29
+ def option_usage_full(option)
30
+ if option.long.present?
31
+ [option.short, option_long(option)].reject(&:blank?).join(SEP)
32
+ else
33
+ option_short(option)
34
+ end
35
+ end
22
36
  end
23
37
 
24
38
  def option_definition(option)
25
- option.short + SEP + self.class.option_long(option) + OPTION_DESC_SEP + option.description
39
+ self.class.option_usage_full(option) + OPTION_DESC_SEP + option.description
26
40
  end
27
41
 
28
42
  def section(header, include_header = true)
@@ -14,7 +14,9 @@ module EacCli
14
14
  end
15
15
 
16
16
  def find_short_option(char)
17
- alternative.options.find { |option| short_without_prefix(option.short) == char }
17
+ alternative.options.find do |option|
18
+ short_without_prefix(option.short).if_present(false) { |v| v == char }
19
+ end
18
20
  end
19
21
 
20
22
  def short_option_collect_argv_value
@@ -37,7 +39,7 @@ module EacCli
37
39
  end
38
40
 
39
41
  def short_without_prefix(short)
40
- short.gsub(/\A#{::Regexp.quote(SHORT_OPTION_PREFIX)}/, '')
42
+ short.to_s.gsub(/\A#{::Regexp.quote(SHORT_OPTION_PREFIX)}/, '')
41
43
  end
42
44
  end
43
45
  end
@@ -24,11 +24,7 @@ module EacCli
24
24
  end
25
25
 
26
26
  def collect(option, value)
27
- if data[option].is_a?(::Array)
28
- data[option] << value
29
- else
30
- data[option] = value
31
- end
27
+ data[option] = option.build_value(value, data[option])
32
28
  end
33
29
 
34
30
  def supplied?(option)
@@ -42,18 +38,8 @@ module EacCli
42
38
  end
43
39
 
44
40
  def default_values
45
- definition.options.each { |option| data[option] = option_default_value(option) }
46
- definition.positional.each do |positional|
47
- data[positional] = positional_default_value(positional)
48
- end
49
- end
50
-
51
- def option_default_value(option)
52
- option.argument? ? nil : false
53
- end
54
-
55
- def positional_default_value(positional)
56
- positional.repeat? ? [] : nil
41
+ definition.options.each { |option| data[option] = option.default_value }
42
+ definition.positional.each { |positional| data[positional] = positional.default_value }
57
43
  end
58
44
  end
59
45
  end
@@ -3,6 +3,7 @@
3
3
  require 'eac_ruby_utils/core_ext'
4
4
  require 'eac_cli/runner'
5
5
  require 'eac_cli/runner_with'
6
+ require 'eac_cli/runner_with_set'
6
7
 
7
8
  class Object
8
9
  def runner_with(*runners, &block)
@@ -10,7 +11,7 @@ class Object
10
11
  enable_simple_cache
11
12
  enable_console_speaker
12
13
  runners.each do |runner|
13
- include runner_with_to_module(runner)
14
+ include ::EacCli::RunnerWithSet.default.item_to_module(runner)
14
15
  end
15
16
  runner_definition(&block) if block
16
17
  end
@@ -0,0 +1,50 @@
1
+ # frozen_string_literal: true
2
+
3
+ module EacCli
4
+ class RunnerWithSet
5
+ class << self
6
+ def default
7
+ @default ||= new
8
+ end
9
+ end
10
+
11
+ def add_namespace(namespace)
12
+ namespace = sanitize_namespace(namespace)
13
+ raise "\"#{namespace}\" already was included" if namespace_set.include?(namespace)
14
+
15
+ namespace_set << namespace
16
+ self
17
+ end
18
+
19
+ def item_to_module(item)
20
+ item.is_a?(::Module) ? item : key_to_module(item)
21
+ end
22
+
23
+ def namespaces
24
+ namespace_set.dup
25
+ end
26
+
27
+ private
28
+
29
+ def namespace_set
30
+ @namespace_set ||= ::Array.new
31
+ end
32
+
33
+ def key_to_module(key)
34
+ namespace_set.lazy
35
+ .map { |namespace| key_to_module_in_namespace(namespace, key) }
36
+ .find(&:present?) ||
37
+ raise("Not module found with key \"#{key}\" (Namespaces: #{namespace_set})")
38
+ end
39
+
40
+ def key_to_module_in_namespace(namespace, key)
41
+ namespace.const_get(key.to_s.camelize)
42
+ rescue ::NameError
43
+ nil
44
+ end
45
+
46
+ def sanitize_namespace(source)
47
+ source.is_a?(::Module) ? source : source.to_s.constantize
48
+ end
49
+ end
50
+ end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module EacCli
4
- VERSION = '0.12.6'
4
+ VERSION = '0.16.0'
5
5
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: eac_cli
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.12.6
4
+ version: 0.16.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Esquilo Azul Company
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2021-02-06 00:00:00.000000000 Z
11
+ date: 2021-02-27 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: eac_ruby_utils
@@ -58,6 +58,7 @@ files:
58
58
  - lib/eac_cli/definition/alternative.rb
59
59
  - lib/eac_cli/definition/argument_option.rb
60
60
  - lib/eac_cli/definition/base_option.rb
61
+ - lib/eac_cli/definition/base_option/initialize_args_parser.rb
61
62
  - lib/eac_cli/definition/boolean_option.rb
62
63
  - lib/eac_cli/definition/help_formatter.rb
63
64
  - lib/eac_cli/definition/positional_argument.rb
@@ -86,8 +87,8 @@ files:
86
87
  - lib/eac_cli/runner/instance_methods.rb
87
88
  - lib/eac_cli/runner_with.rb
88
89
  - lib/eac_cli/runner_with/help.rb
89
- - lib/eac_cli/runner_with/output_file.rb
90
90
  - lib/eac_cli/runner_with/subcommands.rb
91
+ - lib/eac_cli/runner_with_set.rb
91
92
  - lib/eac_cli/version.rb
92
93
  homepage:
93
94
  licenses: []
@@ -1,30 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- require 'eac_cli/runner'
4
- require 'eac_ruby_utils/core_ext'
5
- require 'eac_ruby_utils/abstract_methods'
6
-
7
- module EacCli
8
- module RunnerWith
9
- module OutputFile
10
- common_concern do
11
- include ::EacCli::Runner
12
- include ::EacRubyUtils::AbstractMethods
13
-
14
- abstract_methods :output_content
15
-
16
- runner_definition do
17
- arg_opt '-o', '--output-file', 'Output to file.'
18
- end
19
- end
20
-
21
- def run_output
22
- if parsed.output_file.present?
23
- ::File.write(parsed.output_file, output_content)
24
- else
25
- $stdout.write(output_content)
26
- end
27
- end
28
- end
29
- end
30
- end