eac_cli 0.12.3 → 0.14.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: fdd062e7481cd022e3706681b1fccae579efc2ecab9378cca76871008d75d89f
4
- data.tar.gz: b33f0432da0acd199ff02df511708a9eb56423d5b3fbc2a784b340a8eace9748
3
+ metadata.gz: 8ca942fefac703528e400de1a095cad1dd85d648895eabf7cfdf51ac430b34fe
4
+ data.tar.gz: 73f8e8c595e7669756b134d35576a1a3f873b67dfda1def674ea02d4a60173a7
5
5
  SHA512:
6
- metadata.gz: 60b05d12fb2ea5c787193575a10fac4bead0f9177a75cf39e9dcbefe230d033412b58e2f164112eea1b2add9a95b55f8caf8f7d74831f79e0aa09f6c6889dcb8
7
- data.tar.gz: c4b0d9cf8829b9c18a2d67e7986f187e1797fd7bc6448435534218b9bfa9be3e574adc6a1b3d278a016dc6194b8cbaf569a35f6025f2c0a036cd69ec4b54a420
6
+ metadata.gz: 95cbcedfb77ca5d9fd3a3ffe2f818d9a8866d6774cc25bf3964f0a0fa1c81a3014afb6f3a26cb32393fd84fe76e56e159a5be22f0780e5d916b1d66f90a08961
7
+ data.tar.gz: 4a9cf2040a0cdae39543379f3da011931d11bbc22c15aca1a0c88838a592215d80c6fd413b034f66c991487d7f6a5b32eecc9b9d29530c787aba09c3f48c4daa
@@ -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)
@@ -0,0 +1,15 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'eac_ruby_utils/core_ext'
4
+
5
+ module EacCli
6
+ module Docopt
7
+ class RunnerContextReplacement
8
+ common_constructor :runner
9
+
10
+ def argv
11
+ runner.settings[:argv] || ARGV
12
+ end
13
+ end
14
+ end
15
+ end
@@ -1,6 +1,7 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  require 'eac_cli/docopt/doc_builder'
4
+ require 'eac_cli/docopt/runner_context_replacement'
4
5
  require 'eac_cli/runner'
5
6
  require 'eac_ruby_utils/console/docopt_runner'
6
7
 
@@ -32,6 +33,10 @@ module EacCli
32
33
  def docopt_options
33
34
  super.merge(options_first: self.class.runner_definition.options_first?)
34
35
  end
36
+
37
+ def runner_context
38
+ @runner_context ||= ::EacCli::Docopt::RunnerContextReplacement.new(self)
39
+ end
35
40
  end
36
41
 
37
42
  def extra_available_subcommands
@@ -35,11 +35,11 @@ module EacCli
35
35
  attr_accessor :phase
36
36
 
37
37
  def any_collect_argv_value
38
- if argv_current_option?
39
- option_collect_argv_value
40
- else
41
- positional_collect_argv_value
38
+ %w[double_dash long_option short_option].each do |arg_type|
39
+ return send("#{arg_type}_collect_argv_value") if send("argv_current_#{arg_type}?")
42
40
  end
41
+
42
+ positional_collect_argv_value
43
43
  end
44
44
 
45
45
  def collector
@@ -0,0 +1,37 @@
1
+ # frozen_string_literal: true
2
+
3
+ module EacCli
4
+ class Parser
5
+ class Alternative
6
+ module LongOptions
7
+ LONG_OPTION_PREFIX = '--'
8
+ OPTION_WITH_ARGUMENT_PATTERN = /\A([^=]+)(?:=(.*))\z/.freeze
9
+
10
+ private
11
+
12
+ def argv_current_long_option?
13
+ phase == PHASE_ANY && argv_enum.peek.start_with?(LONG_OPTION_PREFIX) &&
14
+ !argv_current_double_dash?
15
+ end
16
+
17
+ def long_option_collect_argv_value
18
+ option_long, value = parse_option_current_argv
19
+ alternative.options.any? do |option|
20
+ next false unless option.long == option_long
21
+
22
+ if value.nil?
23
+ option_collect_option(option)
24
+ else
25
+ option_argument_collect(option, value)
26
+ end
27
+ end || raise_argv_current_invalid_option
28
+ end
29
+
30
+ def parse_option_current_argv
31
+ m = OPTION_WITH_ARGUMENT_PATTERN.match(argv_enum.peek)
32
+ m ? [m[1], m[2].if_present('')] : [argv_enum.peek, nil]
33
+ end
34
+ end
35
+ end
36
+ end
37
+ end
@@ -0,0 +1,29 @@
1
+ # frozen_string_literal: true
2
+
3
+ module EacCli
4
+ class Parser
5
+ class Alternative
6
+ module OptionArgument
7
+ private
8
+
9
+ attr_accessor :argument_option
10
+
11
+ def argument_option_collect_argv(option)
12
+ self.argument_option = option
13
+ self.phase = PHASE_OPTION_ARGUMENT
14
+ end
15
+
16
+ def option_argument_collect(option, value)
17
+ collector.collect(option, value)
18
+ self.argument_option = nil
19
+ self.phase = PHASE_ANY
20
+ option
21
+ end
22
+
23
+ def option_argument_collect_argv_value
24
+ option_argument_collect(argument_option, argv_enum.peek)
25
+ end
26
+ end
27
+ end
28
+ end
29
+ end
@@ -4,49 +4,20 @@ module EacCli
4
4
  class Parser
5
5
  class Alternative
6
6
  module Options
7
- DOUBLE_DASH = '--'
8
-
9
7
  private
10
8
 
11
- attr_accessor :argument_option, :double_dash
12
-
13
- def argument_option_collect_argv(option)
14
- self.argument_option = option
15
- self.phase = PHASE_OPTION_ARGUMENT
16
- end
17
-
18
- def argv_current_option?
19
- phase == PHASE_ANY && argv_enum.peek.start_with?('-')
20
- end
21
-
22
- def argv_current_double_dash?
23
- argv_enum.peek == DOUBLE_DASH && !double_dash
24
- end
25
-
26
9
  def boolean_option_collect_argv(option)
27
10
  collector.collect(option, true)
28
11
  end
29
12
 
30
- def option_argument_collect_argv_value
31
- collector.collect(argument_option, argv_enum.peek)
32
- self.argument_option = nil
33
- self.phase = PHASE_ANY
34
- end
35
-
36
- def option_collect_argv_value
37
- return double_dash_collect_argv_value if argv_current_double_dash?
38
-
39
- alternative.options.any? do |option|
40
- next false unless [option.short, option.long].include?(argv_enum.peek)
41
-
42
- if option.argument?
43
- argument_option_collect_argv(option)
44
- else
45
- boolean_option_collect_argv(option)
46
- end
13
+ def option_collect_option(option)
14
+ if option.argument?
15
+ argument_option_collect_argv(option)
16
+ else
17
+ boolean_option_collect_argv(option)
18
+ end
47
19
 
48
- true
49
- end || raise_argv_current_invalid_option
20
+ option
50
21
  end
51
22
 
52
23
  def raise_argv_current_invalid_option
@@ -0,0 +1,45 @@
1
+ # frozen_string_literal: true
2
+
3
+ module EacCli
4
+ class Parser
5
+ class Alternative
6
+ module ShortOptions
7
+ SHORT_OPTION_PREFIX = '-'
8
+
9
+ private
10
+
11
+ def argv_current_short_option?
12
+ phase == PHASE_ANY && argv_enum.peek.start_with?(SHORT_OPTION_PREFIX) &&
13
+ !argv_current_long_option?
14
+ end
15
+
16
+ def find_short_option(char)
17
+ alternative.options.find { |option| short_without_prefix(option.short) == char }
18
+ end
19
+
20
+ def short_option_collect_argv_value
21
+ last_option = nil
22
+ short_without_prefix(argv_enum.peek).each_char do |char|
23
+ raise_error "Option \"#{last_option}\" requires a argument not provided" if
24
+ last_option.present?
25
+
26
+ collected_option = short_option_collect_char(char)
27
+ last_option = collected_option if collected_option.argument?
28
+ end
29
+ end
30
+
31
+ # @return [EacCli::Definition::BaseOption] The option collected.
32
+ def short_option_collect_char(char)
33
+ option = find_short_option(char)
34
+ raise_error "Invalid short option \"#{char}\"" unless option
35
+
36
+ option_collect_option(option)
37
+ end
38
+
39
+ def short_without_prefix(short)
40
+ short.gsub(/\A#{::Regexp.quote(SHORT_OPTION_PREFIX)}/, '')
41
+ end
42
+ end
43
+ end
44
+ end
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
@@ -47,53 +47,5 @@ module EacCli
47
47
  include ActiveSupport::Callbacks
48
48
  define_callbacks :run
49
49
  end
50
-
51
- module AfterClassMethods
52
- def create(*runner_context_args)
53
- r = new
54
- r.runner_context = ::EacCli::Runner::Context.new(r, *runner_context_args)
55
- r
56
- end
57
-
58
- def run(*runner_context_args)
59
- r = create(*runner_context_args)
60
- r.run_run
61
- r
62
- end
63
-
64
- def runner_definition(&block)
65
- @runner_definition ||= super_runner_definition
66
- @runner_definition.instance_eval(&block) if block
67
- @runner_definition
68
- end
69
-
70
- def super_runner_definition
71
- superclass.try(:runner_definition).if_present(&:dup) || ::EacCli::Definition.new
72
- end
73
- end
74
-
75
- module InstanceMethods
76
- def run_run
77
- parsed
78
- run_callbacks(:run) { run }
79
- rescue ::EacCli::Runner::Exit # rubocop:disable Lint/SuppressedException
80
- # Do nothing
81
- end
82
-
83
- def runner_context
84
- return @runner_context if @runner_context
85
-
86
- raise 'Context was required, but was not set yet'
87
- end
88
-
89
- def runner_context=(new_runner_context)
90
- @runner_context = new_runner_context
91
- @parsed = nil
92
- end
93
-
94
- def parsed
95
- @parsed ||= ::EacCli::Parser.new(self.class.runner_definition, runner_context.argv).parsed
96
- end
97
- end
98
50
  end
99
51
  end
@@ -0,0 +1,29 @@
1
+ # frozen_string_literal: true
2
+
3
+ module EacCli
4
+ module Runner
5
+ module AfterClassMethods
6
+ def create(*runner_context_args)
7
+ r = new
8
+ r.runner_context = ::EacCli::Runner::Context.new(r, *runner_context_args)
9
+ r
10
+ end
11
+
12
+ def run(*runner_context_args)
13
+ r = create(*runner_context_args)
14
+ r.run_run
15
+ r
16
+ end
17
+
18
+ def runner_definition(&block)
19
+ @runner_definition ||= super_runner_definition
20
+ @runner_definition.instance_eval(&block) if block
21
+ @runner_definition
22
+ end
23
+
24
+ def super_runner_definition
25
+ superclass.try(:runner_definition).if_present(&:dup) || ::EacCli::Definition.new
26
+ end
27
+ end
28
+ end
29
+ end
@@ -0,0 +1,31 @@
1
+ # frozen_string_literal: true
2
+
3
+ module EacCli
4
+ module Runner
5
+ module InstanceMethods
6
+ def run_run
7
+ parsed
8
+ run_callbacks(:run) { run }
9
+ rescue ::EacCli::Parser::Error => e
10
+ $stderr.write("#{e}\n")
11
+ rescue ::EacCli::Runner::Exit # rubocop:disable Lint/SuppressedException
12
+ # Do nothing
13
+ end
14
+
15
+ def runner_context
16
+ return @runner_context if @runner_context
17
+
18
+ raise 'Context was required, but was not set yet'
19
+ end
20
+
21
+ def runner_context=(new_runner_context)
22
+ @runner_context = new_runner_context
23
+ @parsed = nil
24
+ end
25
+
26
+ def parsed
27
+ @parsed ||= ::EacCli::Parser.new(self.class.runner_definition, runner_context.argv).parsed
28
+ end
29
+ end
30
+ end
31
+ end
@@ -27,7 +27,9 @@ module EacCli
27
27
  end
28
28
 
29
29
  def help_text
30
- ::EacCli::Docopt::DocBuilder.new(self.class.runner_definition).to_s
30
+ r = ::EacCli::Docopt::DocBuilder.new(self.class.runner_definition).to_s
31
+ r += help_extra_text if respond_to?(:help_extra_text)
32
+ r
31
33
  end
32
34
  end
33
35
  end
@@ -6,6 +6,14 @@ require 'eac_ruby_utils/core_ext'
6
6
  module EacCli
7
7
  module RunnerWith
8
8
  module Subcommands
9
+ class << self
10
+ def runner?(object)
11
+ ::EacCli::Runner.runner?(object) || (
12
+ object.is_a?(::Class) && object < ::EacRubyUtils::Console::DocoptRunner
13
+ )
14
+ end
15
+ end
16
+
9
17
  common_concern do
10
18
  include ::EacCli::Runner
11
19
  end
@@ -19,7 +27,7 @@ module EacCli
19
27
  def available_subcommands_auto
20
28
  self.class.constants
21
29
  .map { |name| [name.to_s.underscore.gsub('_', '-'), self.class.const_get(name)] }
22
- .select { |c| ::EacCli::Runner.runner?(c[1]) }
30
+ .select { |c| ::EacCli::RunnerWith::Subcommands.runner?(c[1]) }
23
31
  .to_h.with_indifferent_access
24
32
  end
25
33
 
@@ -31,6 +39,11 @@ module EacCli
31
39
  end
32
40
  end
33
41
 
42
+ def help_extra_text
43
+ (['Subcommands:'] + available_subcommands.keys.sort.map { |s| " #{s}" })
44
+ .map { |v| "#{v}\n" }.join
45
+ end
46
+
34
47
  def method_missing(method_name, *arguments, &block)
35
48
  return run_with_subcommand(*arguments, &block) if
36
49
  run_with_subcommand_alias_run?(method_name)
@@ -44,7 +57,11 @@ module EacCli
44
57
 
45
58
  def run_with_subcommand
46
59
  if subcommand_name
47
- subcommand_runner.run_run
60
+ if subcommand_runner.respond_to?(:run_run)
61
+ subcommand_runner.run_run
62
+ else
63
+ subcommand_runner.run
64
+ end
48
65
  else
49
66
  run_without_subcommand
50
67
  end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module EacCli
4
- VERSION = '0.12.3'
4
+ VERSION = '0.14.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.3
4
+ version: 0.14.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: 2020-12-16 00:00:00.000000000 Z
11
+ date: 2021-02-14 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: eac_ruby_utils
@@ -58,26 +58,33 @@ 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
64
65
  - lib/eac_cli/docopt/doc_builder.rb
65
66
  - lib/eac_cli/docopt/doc_builder/alternative.rb
67
+ - lib/eac_cli/docopt/runner_context_replacement.rb
66
68
  - lib/eac_cli/docopt/runner_extension.rb
67
69
  - lib/eac_cli/parser.rb
68
70
  - lib/eac_cli/parser/alternative.rb
69
71
  - lib/eac_cli/parser/alternative/argv.rb
70
72
  - lib/eac_cli/parser/alternative/double_dash.rb
73
+ - lib/eac_cli/parser/alternative/long_options.rb
74
+ - lib/eac_cli/parser/alternative/option_argument.rb
71
75
  - lib/eac_cli/parser/alternative/options.rb
72
76
  - lib/eac_cli/parser/alternative/positionals.rb
77
+ - lib/eac_cli/parser/alternative/short_options.rb
73
78
  - lib/eac_cli/parser/collector.rb
74
79
  - lib/eac_cli/parser/error.rb
75
80
  - lib/eac_cli/patches.rb
76
81
  - lib/eac_cli/patches/object.rb
77
82
  - lib/eac_cli/patches/object/runner_with.rb
78
83
  - lib/eac_cli/runner.rb
84
+ - lib/eac_cli/runner/after_class_methods.rb
79
85
  - lib/eac_cli/runner/context.rb
80
86
  - lib/eac_cli/runner/exit.rb
87
+ - lib/eac_cli/runner/instance_methods.rb
81
88
  - lib/eac_cli/runner_with.rb
82
89
  - lib/eac_cli/runner_with/help.rb
83
90
  - lib/eac_cli/runner_with/output_file.rb