hammer_cli 0.15.1 → 0.16.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (69) hide show
  1. checksums.yaml +4 -4
  2. data/doc/commands_modification.md +82 -0
  3. data/doc/creating_commands.md +78 -38
  4. data/doc/developer_docs.md +2 -0
  5. data/doc/help_modification.md +119 -0
  6. data/doc/i18n.md +2 -2
  7. data/doc/installation_rpm.md +3 -3
  8. data/doc/release_notes.md +9 -1
  9. data/lib/hammer_cli.rb +0 -1
  10. data/lib/hammer_cli/abstract.rb +44 -13
  11. data/lib/hammer_cli/exception_handler.rb +1 -1
  12. data/lib/hammer_cli/help/definition.rb +55 -0
  13. data/lib/hammer_cli/help/definition/abstract_item.rb +38 -0
  14. data/lib/hammer_cli/help/definition/list.rb +53 -0
  15. data/lib/hammer_cli/help/definition/section.rb +36 -0
  16. data/lib/hammer_cli/help/definition/text.rb +21 -0
  17. data/lib/hammer_cli/help/text_builder.rb +26 -49
  18. data/lib/hammer_cli/i18n.rb +0 -1
  19. data/lib/hammer_cli/options/option_collector.rb +12 -9
  20. data/lib/hammer_cli/options/option_processor.rb +17 -0
  21. data/lib/hammer_cli/options/processor_list.rb +37 -0
  22. data/lib/hammer_cli/options/sources/base.rb +17 -0
  23. data/lib/hammer_cli/options/sources/command_line.rb +3 -1
  24. data/lib/hammer_cli/options/sources/saved_defaults.rb +3 -1
  25. data/lib/hammer_cli/options/validators/base.rb +20 -0
  26. data/lib/hammer_cli/options/validators/dsl.rb +160 -0
  27. data/lib/hammer_cli/options/validators/dsl_block_validator.rb +19 -0
  28. data/lib/hammer_cli/output/adapter/csv.rb +11 -11
  29. data/lib/hammer_cli/output/adapter/tree_structure.rb +5 -3
  30. data/lib/hammer_cli/output/definition.rb +42 -4
  31. data/lib/hammer_cli/output/dsl.rb +4 -2
  32. data/lib/hammer_cli/output/fields.rb +9 -2
  33. data/lib/hammer_cli/testing/messages.rb +6 -6
  34. data/lib/hammer_cli/utils.rb +15 -0
  35. data/lib/hammer_cli/version.rb +1 -1
  36. data/locale/ca/LC_MESSAGES/hammer-cli.mo +0 -0
  37. data/locale/de/LC_MESSAGES/hammer-cli.mo +0 -0
  38. data/locale/en/LC_MESSAGES/hammer-cli.mo +0 -0
  39. data/locale/en_GB/LC_MESSAGES/hammer-cli.mo +0 -0
  40. data/locale/es/LC_MESSAGES/hammer-cli.mo +0 -0
  41. data/locale/fr/LC_MESSAGES/hammer-cli.mo +0 -0
  42. data/locale/it/LC_MESSAGES/hammer-cli.mo +0 -0
  43. data/locale/ja/LC_MESSAGES/hammer-cli.mo +0 -0
  44. data/locale/ko/LC_MESSAGES/hammer-cli.mo +0 -0
  45. data/locale/pt_BR/LC_MESSAGES/hammer-cli.mo +0 -0
  46. data/locale/ru/LC_MESSAGES/hammer-cli.mo +0 -0
  47. data/locale/zh_CN/LC_MESSAGES/hammer-cli.mo +0 -0
  48. data/locale/zh_TW/LC_MESSAGES/hammer-cli.mo +0 -0
  49. data/test/unit/abstract_test.rb +70 -5
  50. data/test/unit/exception_handler_test.rb +1 -1
  51. data/test/unit/help/definition/abstract_item_test.rb +33 -0
  52. data/test/unit/help/definition/list_test.rb +17 -0
  53. data/test/unit/help/definition/section_test.rb +25 -0
  54. data/test/unit/help/definition/text_test.rb +11 -0
  55. data/test/unit/help/definition_test.rb +236 -0
  56. data/test/unit/help/text_builder_test.rb +170 -1
  57. data/test/unit/options/option_collector_test.rb +18 -9
  58. data/test/unit/options/processor_list_test.rb +70 -0
  59. data/test/unit/{validator_test.rb → options/validators/dsl_test.rb} +57 -93
  60. data/test/unit/output/adapter/abstract_test.rb +3 -0
  61. data/test/unit/output/adapter/base_test.rb +5 -0
  62. data/test/unit/output/adapter/csv_test.rb +3 -0
  63. data/test/unit/output/adapter/json_test.rb +12 -0
  64. data/test/unit/output/adapter/table_test.rb +5 -0
  65. data/test/unit/output/adapter/yaml_test.rb +11 -0
  66. data/test/unit/output/definition_test.rb +221 -1
  67. data/test/unit/utils_test.rb +23 -0
  68. metadata +31 -5
  69. data/lib/hammer_cli/validator.rb +0 -172
@@ -1,172 +0,0 @@
1
-
2
- module HammerCLI
3
-
4
- class Validator
5
-
6
- class ValidationError < StandardError
7
- end
8
-
9
- class BaseConstraint
10
-
11
- attr_reader :rejected_msg, :required_msg
12
-
13
- def initialize(options, to_check)
14
- @to_check = to_check
15
- @rejected_msg = ""
16
- @required_msg = ""
17
-
18
- @options = options.inject({}) do |hash, opt|
19
- hash.update({opt.attribute.attribute_name => opt})
20
- end
21
- end
22
-
23
- def rejected(args={})
24
- msg = args[:msg] || rejected_msg % option_switches.join(", ")
25
- raise ValidationError.new(msg) if exist?
26
- end
27
-
28
- def required(args={})
29
- msg = args[:msg] || required_msg % option_switches.join(", ")
30
- raise ValidationError.new(msg) unless exist?
31
- end
32
-
33
- def exist?
34
- raise NotImplementedError
35
- end
36
-
37
- protected
38
-
39
- def get_option(name)
40
- name = name.to_s
41
- raise _("Unknown option name '%s'.") % name unless @options.has_key? name
42
- @options[name]
43
- end
44
-
45
- def get_option_value(name)
46
- opt = get_option(name)
47
- value = opt.get
48
- if value.nil?
49
- defaults = opt.command.send(:context)[:defaults]
50
- value = defaults.get_defaults(name.to_s) if defaults
51
- end
52
- value
53
- end
54
-
55
- def option_passed?(option_name)
56
- !get_option_value(option_name).nil?
57
- end
58
-
59
- def option_switches(opts=nil)
60
- opts ||= @to_check
61
- opts.collect do |option_name|
62
- get_option(option_name).attribute.long_switch || get_option(option_name).attribute.switches.first
63
- end
64
- end
65
-
66
- end
67
-
68
- class AllConstraint < BaseConstraint
69
-
70
- def initialize(options, to_check)
71
- super(options, to_check)
72
- @rejected_msg = _("You can't set all options %s at one time.")
73
- @required_msg = _("Options %s are required.")
74
- end
75
-
76
- def exist?
77
- @to_check.each do |opt|
78
- return false unless option_passed?(opt)
79
- end
80
- return true
81
- end
82
- end
83
-
84
- class OneOptionConstraint < AllConstraint
85
- def initialize(options, to_check)
86
- super(options, [to_check])
87
- @rejected_msg = _("You can't set option %s.")
88
- @required_msg = _("Option %s is required.")
89
- end
90
-
91
- def value
92
- get_option_value(@to_check[0])
93
- end
94
- end
95
-
96
- class AnyConstraint < BaseConstraint
97
-
98
- def initialize(options, to_check)
99
- super(options, to_check)
100
- @rejected_msg = _("You can't set any of options %s.")
101
- @required_msg = _("At least one of options %s is required.")
102
- end
103
-
104
- def exist?
105
- @to_check.each do |opt|
106
- return true if option_passed?(opt)
107
- end
108
- return @to_check.empty?
109
- end
110
- end
111
-
112
-
113
- class OneOfConstraint < BaseConstraint
114
- def initialize(options, to_check)
115
- raise 'Set at least one expected option' if to_check.empty?
116
- super(options, to_check)
117
- end
118
-
119
- def rejected
120
- raise NotImplementedError, '#rejected is unsupported for OneOfConstraint'
121
- end
122
-
123
- def required_msg
124
- case count_present_options
125
- when 0
126
- _("One of options %s is required.")
127
- when 1
128
- ''
129
- else
130
- _("Only one of options %s can be set.")
131
- end
132
- end
133
-
134
- def exist?
135
- return count_present_options == 1
136
- end
137
-
138
- protected
139
- def count_present_options
140
- @to_check.count do |opt|
141
- option_passed?(opt)
142
- end
143
- end
144
- end
145
-
146
- def initialize(options)
147
- @options = options
148
- end
149
-
150
- def all(*to_check)
151
- AllConstraint.new(@options, to_check.flatten(1))
152
- end
153
-
154
- def option(to_check)
155
- OneOptionConstraint.new(@options, to_check)
156
- end
157
-
158
- def any(*to_check)
159
- AnyConstraint.new(@options, to_check.flatten(1))
160
- end
161
-
162
- def one_of(*to_check)
163
- OneOfConstraint.new(@options, to_check.flatten(1))
164
- end
165
-
166
- def run(&block)
167
- self.instance_eval &block
168
- end
169
-
170
- end
171
-
172
- end