eac_cli 0.10.0 → 0.11.0

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: 607e900ac4f5c7b6e7531c4ce9f9f6cfc8fd8ffce30f7b4d2a22ff91cdfd99bb
4
- data.tar.gz: 0f49af96a89a527390ba34d2e6ecc17d2942aea7200bebbab1ba1f540398f413
3
+ metadata.gz: 6bfbec2f4b3875640397fa352c5edde397fc335614d7e95a9200472ae8688819
4
+ data.tar.gz: fcf67763d91ea9674b8355b7e5dd587d6d6b3a9e05ae387eea045d364658481c
5
5
  SHA512:
6
- metadata.gz: 1e2a23e42fb5df31819b499f8dc1448f6ecc9d095fc22c9a0ceabe1acadf9ba6398a62d04a09943b233feeeb5e07325f62069ee883fb388b29424e7656ebe500
7
- data.tar.gz: 1fefcf204e43d2ccfcf45c8507be13be885d46da72f0d6eda2ea3c3e6b7b845592e3ed989fc29b8c687a3727b795f8ea3a2d2a6526c3cda61bbf6b6f7da8df86
6
+ metadata.gz: ff15edf91a76df0af27aff006f85071f8ff1b4ae5c70930ad5daf8ae129d372e976742d47a817f142b8fa747d8d5e285806619002eab8602d760e40e83ff89d6
7
+ data.tar.gz: 6ff1ae990f460832abf8cfc50136a28cf09d317c85835e384562640b0b48cff4f63fcb794efcad45784bd168b2872f46403dc658cd4bafeab8cceed37bddf2a8
@@ -45,6 +45,10 @@ module EacCli
45
45
  self.description = description
46
46
  end
47
47
 
48
+ def help_formatter
49
+ @help_formatter ||= ::EacCli::Definition::HelpFormatter.new(self)
50
+ end
51
+
48
52
  def options_arg(options_argument)
49
53
  self.options_argument = options_argument
50
54
  end
@@ -54,17 +58,23 @@ module EacCli
54
58
  end
55
59
 
56
60
  def pos_arg(name, arg_options = {})
57
- raise 'Positional arguments are blocked' if positional_arguments_blocked?
61
+ new_pos_arg = ::EacCli::Definition::PositionalArgument.new(name, arg_options)
62
+ raise 'Positional arguments are blocked' if positional_arguments_blocked?(new_pos_arg)
58
63
 
59
- pos_set << ::EacCli::Definition::PositionalArgument.new(name, arg_options)
64
+ pos_set << new_pos_arg
60
65
  end
61
66
 
62
67
  def positional
63
68
  pos_set.to_a
64
69
  end
65
70
 
66
- def positional_arguments_blocked?
67
- pos_set.any? { |e| e.optional? || e.repeat? }
71
+ def positional_arguments_blocked?(new_pos_arg)
72
+ last = pos_set.last
73
+ return false unless last
74
+ return true if last.repeat?
75
+ return true if last.optional? && new_pos_arg.required?
76
+
77
+ false
68
78
  end
69
79
 
70
80
  def subcommands
@@ -0,0 +1,82 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'eac_ruby_utils/core_ext'
4
+
5
+ module EacCli
6
+ class Definition
7
+ class HelpFormatter
8
+ SEP = ' '
9
+ IDENT = SEP * 2
10
+ OPTION_DESC_SEP = IDENT * 2
11
+
12
+ class << self
13
+ def option_long(option)
14
+ b = option.long
15
+ b += '=VALUE' if option.argument?
16
+ b
17
+ end
18
+
19
+ def option_short(option)
20
+ b = option.short
21
+ b += 'VALUE' if option.argument?
22
+ b
23
+ end
24
+ end
25
+
26
+ common_constructor :definition
27
+
28
+ def option_argument(option)
29
+ b = option.long
30
+ b += '=VALUE' if option.argument?
31
+ b
32
+ end
33
+
34
+ def positional_argument(positional)
35
+ if positional.subcommand?
36
+ ::EacRubyUtils::Console::DocoptRunner::SUBCOMMANDS_MACRO
37
+ else
38
+ r = "<#{positional.name}>"
39
+ r += '...' if positional.repeat?
40
+ r = "[#{r}]" if positional.optional?
41
+ r
42
+ end
43
+ end
44
+
45
+ def section(header, include_header = true)
46
+ b = include_header ? "#{header.humanize}:\n" : ''
47
+ b += send("self_#{header}") + "\n"
48
+ # TO-DO: implement alternatives
49
+ b
50
+ end
51
+
52
+ def self_options
53
+ definition.options.map { |option| IDENT + option_definition(option) }.join("\n")
54
+ end
55
+
56
+ def self_usage
57
+ IDENT + self_usage_arguments.join(SEP)
58
+ end
59
+
60
+ def self_usage_arguments
61
+ [::EacRubyUtils::Console::DocoptRunner::PROGRAM_MACRO] +
62
+ definition.options_argument.if_present([]) { |_v| ['[options]'] } +
63
+ self_usage_arguments_options +
64
+ self_usage_arguments_positional
65
+ end
66
+
67
+ def self_usage_arguments_options
68
+ definition.options.select(&:show_on_usage?).map do |option|
69
+ self.class.option_argument(option)
70
+ end
71
+ end
72
+
73
+ def self_usage_arguments_positional
74
+ definition.positional.map { |p| positional_argument(p) }
75
+ end
76
+
77
+ def to_banner
78
+ "#{definition.description}\n\n#{section('usage')}"
79
+ end
80
+ end
81
+ end
82
+ end
@@ -5,8 +5,10 @@ require 'eac_ruby_utils/core_ext'
5
5
  module EacCli
6
6
  class Definition
7
7
  class PositionalArgument
8
+ DEFAULT_REQUIRED = true
9
+
8
10
  enable_listable
9
- lists.add_symbol :option, :optional, :repeat, :subcommand
11
+ lists.add_symbol :option, :optional, :repeat, :required, :subcommand
10
12
  common_constructor :name, :options, default: [{}] do
11
13
  options.assert_valid_keys(self.class.lists.option.values)
12
14
  end
@@ -16,13 +18,20 @@ module EacCli
16
18
  end
17
19
 
18
20
  def optional?
19
- options[OPTION_OPTIONAL]
21
+ !required?
20
22
  end
21
23
 
22
24
  def repeat?
23
25
  options[OPTION_REPEAT]
24
26
  end
25
27
 
28
+ def required?
29
+ return true if options.key?(OPTION_REQUIRED) && options.fetch(OPTION_REQUIRED)
30
+ return false if options.key?(OPTION_OPTIONAL) && options.fetch(OPTION_OPTIONAL)
31
+
32
+ DEFAULT_REQUIRED
33
+ end
34
+
26
35
  def subcommand?
27
36
  options[OPTION_SUBCOMMAND]
28
37
  end
@@ -6,10 +6,6 @@ require 'optparse'
6
6
  module EacCli
7
7
  class Parser
8
8
  class OptionsCollection
9
- SEP = ' '
10
- IDENT = SEP * 2
11
- OPTION_DESC_SEP = IDENT * 2
12
-
13
9
  enable_simple_cache
14
10
  common_constructor(:definition, :argv, :collector) { collect }
15
11
  attr_reader :arguments
@@ -49,7 +45,7 @@ module EacCli
49
45
  end
50
46
 
51
47
  def build_banner
52
- option_parser.banner = "#{definition.description}\n\n#{section('usage')}"
48
+ option_parser.banner = definition.help_formatter.to_banner
53
49
  end
54
50
 
55
51
  def build_options
@@ -60,68 +56,13 @@ module EacCli
60
56
 
61
57
  def build_option(option)
62
58
  option_parser.on(
63
- *[option_short(option), option_long(option), option.description].reject(&:blank?)
59
+ *[::EacCli::Definition::HelpFormatter.option_short(option),
60
+ ::EacCli::Definition::HelpFormatter.option_long(option),
61
+ option.description].reject(&:blank?)
64
62
  ) do |value|
65
63
  collector.collect(option, value)
66
64
  end
67
65
  end
68
-
69
- def positional_argument(positional)
70
- if positional.subcommand?
71
- ::EacRubyUtils::Console::DocoptRunner::SUBCOMMANDS_MACRO
72
- else
73
- r = "<#{positional.name}>"
74
- r += '...' if positional.repeat?
75
- r = "[#{r}]" if positional.optional?
76
- r
77
- end
78
- end
79
-
80
- def option_argument(option)
81
- option_long(option)
82
- end
83
-
84
- def option_long(option)
85
- b = option.long
86
- b += '=VALUE' if option.argument?
87
- b
88
- end
89
-
90
- def option_short(option)
91
- b = option.short
92
- b += 'VALUE' if option.argument?
93
- b
94
- end
95
-
96
- def section(header, include_header = true)
97
- b = include_header ? "#{header.humanize}:\n" : ''
98
- b += send("self_#{header}") + "\n"
99
- # TO-DO: implement alternatives
100
- b
101
- end
102
-
103
- def self_options
104
- definition.options.map { |option| IDENT + option_definition(option) }.join("\n")
105
- end
106
-
107
- def self_usage
108
- IDENT + self_usage_arguments.join(SEP)
109
- end
110
-
111
- def self_usage_arguments
112
- [::EacRubyUtils::Console::DocoptRunner::PROGRAM_MACRO] +
113
- definition.options_argument.if_present([]) { |_v| ['[options]'] } +
114
- self_usage_arguments_options +
115
- self_usage_arguments_positional
116
- end
117
-
118
- def self_usage_arguments_options
119
- definition.options.select(&:show_on_usage?).map { |option| option_argument(option) }
120
- end
121
-
122
- def self_usage_arguments_positional
123
- definition.positional.map { |p| positional_argument(p) }
124
- end
125
66
  end
126
67
  end
127
68
  end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module EacCli
4
- VERSION = '0.10.0'
4
+ VERSION = '0.11.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.10.0
4
+ version: 0.11.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-11-15 00:00:00.000000000 Z
11
+ date: 2020-11-16 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/argument_option.rb
59
59
  - lib/eac_cli/definition/base_option.rb
60
60
  - lib/eac_cli/definition/boolean_option.rb
61
+ - lib/eac_cli/definition/help_formatter.rb
61
62
  - lib/eac_cli/definition/positional_argument.rb
62
63
  - lib/eac_cli/docopt/doc_builder.rb
63
64
  - lib/eac_cli/docopt/runner_extension.rb