eac_cli 0.13.0 → 0.16.1
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/eac_cli/core_ext.rb +2 -0
- data/lib/eac_cli/definition/alternative.rb +4 -7
- data/lib/eac_cli/definition/base_option.rb +20 -9
- data/lib/eac_cli/definition/base_option/initialize_args_parser.rb +47 -0
- data/lib/eac_cli/docopt/doc_builder.rb +15 -1
- data/lib/eac_cli/parser/alternative/short_options.rb +4 -2
- data/lib/eac_cli/patches/object/runner_with.rb +2 -1
- data/lib/eac_cli/runner/context.rb +2 -1
- data/lib/eac_cli/runner/instance_methods.rb +8 -1
- data/lib/eac_cli/runner_with_set.rb +50 -0
- data/lib/eac_cli/version.rb +1 -1
- metadata +5 -4
- data/lib/eac_cli/runner_with/output_file.rb +0 -30
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 23a97e547ebd10f3efd4f64da1d46c47942b46baacf7407e0dc9079c76c20f4a
|
4
|
+
data.tar.gz: ddcabc7f42f2f616c86435098d7e2d9b840342965abf6e65d72f7311a0cdf06d
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: a56fa25e3023ab586c482573f170584927fd451dd9ab8fd0f53ae9707dfc037af24293ab856254512916c31bf218890b89e647a432e4ea1064fc3555ddd61e47
|
7
|
+
data.tar.gz: 0e303addfc58031901fb5a559184cd07fed828ef5ab80ffd7f6f16ad6f84b02136c181e90bb825799ed6af775747d0668ff6cda2179338f5366e4e7bcb2ec50f
|
data/lib/eac_cli/core_ext.rb
CHANGED
@@ -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(
|
14
|
-
options_set << ::EacCli::Definition::ArgumentOption.
|
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(
|
20
|
-
options_set << ::EacCli::Definition::BooleanOption.
|
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
|
@@ -5,23 +5,34 @@ 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
20
|
enable_abstract_methods :build_value, :default_value
|
12
21
|
lists.add_symbol :option, :optional, :usage, :repeat, :required
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
@options = options.symbolize_keys
|
20
|
-
@options.assert_valid_keys(::EacCli::Definition::BaseOption.lists.option.values)
|
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
|
+
)
|
21
28
|
end
|
22
29
|
|
23
30
|
def identifier
|
24
|
-
long.
|
31
|
+
[long, short].each do |v|
|
32
|
+
v.to_s.if_present { |vv| return vv.variableize.to_sym }
|
33
|
+
end
|
34
|
+
|
35
|
+
raise 'No short or long option to build identifier'
|
25
36
|
end
|
26
37
|
|
27
38
|
def repeat?
|
@@ -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
|
@@ -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
|
-
|
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
|
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
|
@@ -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
|
14
|
+
include ::EacCli::RunnerWithSet.default.item_to_module(runner)
|
14
15
|
end
|
15
16
|
runner_definition(&block) if block
|
16
17
|
end
|
@@ -27,8 +27,9 @@ module EacCli
|
|
27
27
|
|
28
28
|
def parent_call(method_name, *args)
|
29
29
|
return parent.context(method_name, *args) if parent.respond_to?(:context)
|
30
|
+
return parent.runner_context.call(method_name, *args) if parent.respond_to?(:runner_context)
|
30
31
|
|
31
|
-
parent.runner_context
|
32
|
+
raise "Parent #{parent} do not respond to .context or .runner_context (Runner: #{runner})"
|
32
33
|
end
|
33
34
|
end
|
34
35
|
end
|
@@ -3,15 +3,22 @@
|
|
3
3
|
module EacCli
|
4
4
|
module Runner
|
5
5
|
module InstanceMethods
|
6
|
+
PARSER_ERROR_EXIT_CODE = 1
|
7
|
+
|
6
8
|
def run_run
|
7
9
|
parsed
|
8
10
|
run_callbacks(:run) { run }
|
9
11
|
rescue ::EacCli::Parser::Error => e
|
10
|
-
|
12
|
+
run_parser_error(e)
|
11
13
|
rescue ::EacCli::Runner::Exit # rubocop:disable Lint/SuppressedException
|
12
14
|
# Do nothing
|
13
15
|
end
|
14
16
|
|
17
|
+
def run_parser_error(error)
|
18
|
+
$stderr.write("#{error}\n")
|
19
|
+
::Kernel.exit(PARSER_ERROR_EXIT_CODE)
|
20
|
+
end
|
21
|
+
|
15
22
|
def runner_context
|
16
23
|
return @runner_context if @runner_context
|
17
24
|
|
@@ -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
|
data/lib/eac_cli/version.rb
CHANGED
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.
|
4
|
+
version: 0.16.1
|
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-
|
11
|
+
date: 2021-04-01 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: []
|
@@ -107,7 +108,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
107
108
|
- !ruby/object:Gem::Version
|
108
109
|
version: '0'
|
109
110
|
requirements: []
|
110
|
-
rubygems_version: 3.0.
|
111
|
+
rubygems_version: 3.0.9
|
111
112
|
signing_key:
|
112
113
|
specification_version: 4
|
113
114
|
summary: Utilities to build CLI applications with Ruby.
|
@@ -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
|