hammer_cli 0.19.0 → 2.1.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (213) hide show
  1. checksums.yaml +4 -4
  2. data/bin/hammer-complete +28 -0
  3. data/config/cli_config.template.yml +2 -0
  4. data/config/hammer.completion +5 -0
  5. data/doc/commands_extension.md +12 -0
  6. data/doc/creating_commands.md +100 -0
  7. data/doc/installation.md +47 -4
  8. data/doc/release_notes.md +32 -5
  9. data/lib/hammer_cli.rb +1 -0
  10. data/lib/hammer_cli/abstract.rb +61 -4
  11. data/lib/hammer_cli/apipie/api_connection.rb +5 -1
  12. data/lib/hammer_cli/apipie/command.rb +3 -2
  13. data/lib/hammer_cli/apipie/option_builder.rb +15 -13
  14. data/lib/hammer_cli/apipie/option_definition.rb +9 -7
  15. data/lib/hammer_cli/bash.rb +2 -0
  16. data/lib/hammer_cli/bash/completion.rb +159 -0
  17. data/lib/hammer_cli/bash/prebuild_command.rb +21 -0
  18. data/lib/hammer_cli/command_extensions.rb +21 -1
  19. data/lib/hammer_cli/connection.rb +4 -0
  20. data/lib/hammer_cli/exception_handler.rb +11 -2
  21. data/lib/hammer_cli/full_help.rb +8 -1
  22. data/lib/hammer_cli/help/builder.rb +29 -3
  23. data/lib/hammer_cli/logger_watch.rb +1 -1
  24. data/lib/hammer_cli/main.rb +5 -3
  25. data/lib/hammer_cli/options/normalizers.rb +7 -3
  26. data/lib/hammer_cli/options/option_definition.rb +26 -6
  27. data/lib/hammer_cli/options/option_family.rb +114 -0
  28. data/lib/hammer_cli/options/predefined.rb +1 -1
  29. data/lib/hammer_cli/output.rb +1 -1
  30. data/lib/hammer_cli/output/adapter/abstract.rb +1 -5
  31. data/lib/hammer_cli/output/adapter/base.rb +1 -1
  32. data/lib/hammer_cli/output/adapter/csv.rb +3 -2
  33. data/lib/hammer_cli/output/adapter/json.rb +14 -3
  34. data/lib/hammer_cli/output/adapter/silent.rb +1 -1
  35. data/lib/hammer_cli/output/adapter/table.rb +30 -80
  36. data/lib/hammer_cli/output/adapter/yaml.rb +6 -3
  37. data/lib/hammer_cli/output/definition.rb +11 -56
  38. data/lib/hammer_cli/output/generators.rb +1 -0
  39. data/lib/hammer_cli/output/generators/table.rb +121 -0
  40. data/lib/hammer_cli/output/output.rb +2 -4
  41. data/lib/hammer_cli/settings.rb +2 -1
  42. data/lib/hammer_cli/subcommand.rb +25 -1
  43. data/lib/hammer_cli/testing/command_assertions.rb +2 -2
  44. data/lib/hammer_cli/utils.rb +22 -0
  45. data/lib/hammer_cli/version.rb +1 -1
  46. data/locale/ca/LC_MESSAGES/hammer-cli.mo +0 -0
  47. data/locale/de/LC_MESSAGES/hammer-cli.mo +0 -0
  48. data/locale/en/LC_MESSAGES/hammer-cli.mo +0 -0
  49. data/locale/en_GB/LC_MESSAGES/hammer-cli.mo +0 -0
  50. data/locale/es/LC_MESSAGES/hammer-cli.mo +0 -0
  51. data/locale/fr/LC_MESSAGES/hammer-cli.mo +0 -0
  52. data/locale/it/LC_MESSAGES/hammer-cli.mo +0 -0
  53. data/locale/ja/LC_MESSAGES/hammer-cli.mo +0 -0
  54. data/locale/ko/LC_MESSAGES/hammer-cli.mo +0 -0
  55. data/locale/pt_BR/LC_MESSAGES/hammer-cli.mo +0 -0
  56. data/locale/ru/LC_MESSAGES/hammer-cli.mo +0 -0
  57. data/locale/zh_CN/LC_MESSAGES/hammer-cli.mo +0 -0
  58. data/locale/zh_TW/LC_MESSAGES/hammer-cli.mo +0 -0
  59. data/man/hammer.1.gz +0 -0
  60. data/test/unit/abstract_test.rb +23 -2
  61. data/test/unit/apipie/api_connection_test.rb +1 -0
  62. data/test/unit/apipie/option_builder_test.rb +8 -0
  63. data/test/unit/bash_test.rb +138 -0
  64. data/test/unit/command_extensions_test.rb +67 -49
  65. data/test/unit/exception_handler_test.rb +44 -0
  66. data/test/unit/help/builder_test.rb +22 -0
  67. data/test/unit/options/option_family_test.rb +48 -0
  68. data/test/unit/output/adapter/base_test.rb +58 -0
  69. data/test/unit/output/adapter/csv_test.rb +63 -1
  70. data/test/unit/output/adapter/json_test.rb +61 -0
  71. data/test/unit/output/adapter/table_test.rb +70 -1
  72. data/test/unit/output/adapter/yaml_test.rb +59 -0
  73. data/test/unit/output/definition_test.rb +7 -7
  74. data/test/unit/output/output_test.rb +3 -3
  75. metadata +83 -345
  76. data/hammer_cli_complete +0 -13
  77. data/test/reports/TEST-Fields-ContainerField-display-.xml +0 -7
  78. data/test/reports/TEST-Fields-ContainerField-display-blank-is-allowed.xml +0 -15
  79. data/test/reports/TEST-Fields-ContainerField-display-blank-is-not-allowed.xml +0 -15
  80. data/test/reports/TEST-Fields-ContainerField.xml +0 -7
  81. data/test/reports/TEST-Fields-Field-display-.xml +0 -7
  82. data/test/reports/TEST-Fields-Field-display-blank-is-allowed.xml +0 -11
  83. data/test/reports/TEST-Fields-Field-display-blank-is-not-allowed.xml +0 -11
  84. data/test/reports/TEST-Fields-Field-hide-blank-.xml +0 -11
  85. data/test/reports/TEST-Fields-Field-parameters.xml +0 -9
  86. data/test/reports/TEST-Fields-Field.xml +0 -13
  87. data/test/reports/TEST-HammerCLI-AbstractCommand-build-options.xml +0 -15
  88. data/test/reports/TEST-HammerCLI-AbstractCommand-exception-handler.xml +0 -13
  89. data/test/reports/TEST-HammerCLI-AbstractCommand-logging.xml +0 -21
  90. data/test/reports/TEST-HammerCLI-AbstractCommand-option-builder.xml +0 -11
  91. data/test/reports/TEST-HammerCLI-AbstractCommand-options.xml +0 -11
  92. data/test/reports/TEST-HammerCLI-AbstractCommand-output.xml +0 -19
  93. data/test/reports/TEST-HammerCLI-AbstractCommand-subcommand-behavior-remove-subcommand.xml +0 -11
  94. data/test/reports/TEST-HammerCLI-AbstractCommand-subcommand-behavior-subcommand-.xml +0 -13
  95. data/test/reports/TEST-HammerCLI-AbstractCommand-subcommand-behavior-subcommand.xml +0 -11
  96. data/test/reports/TEST-HammerCLI-AbstractCommand-subcommand-behavior.xml +0 -7
  97. data/test/reports/TEST-HammerCLI-AbstractCommand.xml +0 -11
  98. data/test/reports/TEST-HammerCLI-Apipie-Command-options.xml +0 -11
  99. data/test/reports/TEST-HammerCLI-Apipie-Command-resource-defined.xml +0 -9
  100. data/test/reports/TEST-HammerCLI-Apipie-Command-setting-resources.xml +0 -19
  101. data/test/reports/TEST-HammerCLI-Apipie-Command.xml +0 -9
  102. data/test/reports/TEST-HammerCLI-Apipie-OptionBuilder-aliasing-resources.xml +0 -13
  103. data/test/reports/TEST-HammerCLI-Apipie-OptionBuilder-filtering-options.xml +0 -15
  104. data/test/reports/TEST-HammerCLI-Apipie-OptionBuilder-required-options.xml +0 -11
  105. data/test/reports/TEST-HammerCLI-Apipie-OptionBuilder-setting-correct-normalizers.xml +0 -9
  106. data/test/reports/TEST-HammerCLI-Apipie-OptionBuilder-with-hash-params.xml +0 -11
  107. data/test/reports/TEST-HammerCLI-Apipie-OptionBuilder-with-one-simple-param.xml +0 -15
  108. data/test/reports/TEST-HammerCLI-Apipie-OptionBuilder.xml +0 -7
  109. data/test/reports/TEST-HammerCLI-Completer-command-completion.xml +0 -29
  110. data/test/reports/TEST-HammerCLI-Completer-option-value-completion.xml +0 -17
  111. data/test/reports/TEST-HammerCLI-Completer-subcommand-completion.xml +0 -19
  112. data/test/reports/TEST-HammerCLI-Completer.xml +0 -7
  113. data/test/reports/TEST-HammerCLI-CompleterLine-line-complete.xml +0 -25
  114. data/test/reports/TEST-HammerCLI-CompleterLine-splitting-words.xml +0 -29
  115. data/test/reports/TEST-HammerCLI-CompleterLine.xml +0 -7
  116. data/test/reports/TEST-HammerCLI-CompleterWord-complete-.xml +0 -23
  117. data/test/reports/TEST-HammerCLI-CompleterWord-quote.xml +0 -15
  118. data/test/reports/TEST-HammerCLI-CompleterWord-quoted-.xml +0 -13
  119. data/test/reports/TEST-HammerCLI-CompleterWord.xml +0 -7
  120. data/test/reports/TEST-HammerCLI-Connection.xml +0 -21
  121. data/test/reports/TEST-HammerCLI-ExceptionHandler.xml +0 -21
  122. data/test/reports/TEST-HammerCLI-I18n.xml +0 -11
  123. data/test/reports/TEST-HammerCLI-MainCommand-loading-context-password.xml +0 -11
  124. data/test/reports/TEST-HammerCLI-MainCommand-loading-context-username.xml +0 -11
  125. data/test/reports/TEST-HammerCLI-MainCommand-loading-context-verbose.xml +0 -9
  126. data/test/reports/TEST-HammerCLI-MainCommand-loading-context.xml +0 -7
  127. data/test/reports/TEST-HammerCLI-MainCommand.xml +0 -7
  128. data/test/reports/TEST-HammerCLI-Modules-find-by-name.xml +0 -13
  129. data/test/reports/TEST-HammerCLI-Modules-load-a-module-module-not-found.xml +0 -13
  130. data/test/reports/TEST-HammerCLI-Modules-load-a-module-module-runtime-exception.xml +0 -13
  131. data/test/reports/TEST-HammerCLI-Modules-load-a-module-success.xml +0 -15
  132. data/test/reports/TEST-HammerCLI-Modules-load-a-module.xml +0 -7
  133. data/test/reports/TEST-HammerCLI-Modules-load-all-modules.xml +0 -9
  134. data/test/reports/TEST-HammerCLI-Modules-names.xml +0 -13
  135. data/test/reports/TEST-HammerCLI-Modules.xml +0 -7
  136. data/test/reports/TEST-HammerCLI-OptionBuilderContainer.0.xml +0 -7
  137. data/test/reports/TEST-HammerCLI-OptionBuilderContainer.xml +0 -11
  138. data/test/reports/TEST-HammerCLI-Options-Normalizers-abstract.xml +0 -9
  139. data/test/reports/TEST-HammerCLI-Options-Normalizers-bool.xml +0 -31
  140. data/test/reports/TEST-HammerCLI-Options-Normalizers-datetime.xml +0 -17
  141. data/test/reports/TEST-HammerCLI-Options-Normalizers-enum.xml +0 -15
  142. data/test/reports/TEST-HammerCLI-Options-Normalizers-enumlist.xml +0 -21
  143. data/test/reports/TEST-HammerCLI-Options-Normalizers-json-input.xml +0 -15
  144. data/test/reports/TEST-HammerCLI-Options-Normalizers-key-value-list.xml +0 -17
  145. data/test/reports/TEST-HammerCLI-Options-Normalizers-list.xml +0 -15
  146. data/test/reports/TEST-HammerCLI-Options-Normalizers.xml +0 -7
  147. data/test/reports/TEST-HammerCLI-Options-OptionDefinition-context.xml +0 -9
  148. data/test/reports/TEST-HammerCLI-Options-OptionDefinition-formatters.xml +0 -11
  149. data/test/reports/TEST-HammerCLI-Options-OptionDefinition.xml +0 -7
  150. data/test/reports/TEST-HammerCLI-Output-Adapter-Abstract-error-messages.xml +0 -15
  151. data/test/reports/TEST-HammerCLI-Output-Adapter-Abstract-messages.xml +0 -11
  152. data/test/reports/TEST-HammerCLI-Output-Adapter-Abstract-test-data-for-field.xml +0 -15
  153. data/test/reports/TEST-HammerCLI-Output-Adapter-Abstract.xml +0 -17
  154. data/test/reports/TEST-HammerCLI-Output-Adapter-Base-print-collection-show-ids.xml +0 -9
  155. data/test/reports/TEST-HammerCLI-Output-Adapter-Base-print-collection.xml +0 -27
  156. data/test/reports/TEST-HammerCLI-Output-Adapter-Base.xml +0 -7
  157. data/test/reports/TEST-HammerCLI-Output-Adapter-CSValues-print-collection-formatters.xml +0 -11
  158. data/test/reports/TEST-HammerCLI-Output-Adapter-CSValues-print-collection-handle-fields-with-collections.xml +0 -13
  159. data/test/reports/TEST-HammerCLI-Output-Adapter-CSValues-print-collection-handle-fields-with-containers.xml +0 -11
  160. data/test/reports/TEST-HammerCLI-Output-Adapter-CSValues-print-collection-handle-ids.xml +0 -11
  161. data/test/reports/TEST-HammerCLI-Output-Adapter-CSValues-print-collection.xml +0 -11
  162. data/test/reports/TEST-HammerCLI-Output-Adapter-CSValues-print-message.xml +0 -11
  163. data/test/reports/TEST-HammerCLI-Output-Adapter-CSValues.xml +0 -7
  164. data/test/reports/TEST-HammerCLI-Output-Adapter-Table-print-collection-column-width.xml +0 -15
  165. data/test/reports/TEST-HammerCLI-Output-Adapter-Table-print-collection-formatters.xml +0 -11
  166. data/test/reports/TEST-HammerCLI-Output-Adapter-Table-print-collection-handle-ids.xml +0 -11
  167. data/test/reports/TEST-HammerCLI-Output-Adapter-Table-print-collection-sort-columns.xml +0 -9
  168. data/test/reports/TEST-HammerCLI-Output-Adapter-Table-print-collection.xml +0 -11
  169. data/test/reports/TEST-HammerCLI-Output-Adapter-Table.xml +0 -7
  170. data/test/reports/TEST-HammerCLI-Output-Definition-empty-.xml +0 -11
  171. data/test/reports/TEST-HammerCLI-Output-Definition.xml +0 -11
  172. data/test/reports/TEST-HammerCLI-Output-Dsl-collection.xml +0 -13
  173. data/test/reports/TEST-HammerCLI-Output-Dsl-custom-fields.xml +0 -11
  174. data/test/reports/TEST-HammerCLI-Output-Dsl-fields.xml +0 -15
  175. data/test/reports/TEST-HammerCLI-Output-Dsl-label.xml +0 -13
  176. data/test/reports/TEST-HammerCLI-Output-Dsl-path-definition.xml +0 -13
  177. data/test/reports/TEST-HammerCLI-Output-Dsl.xml +0 -9
  178. data/test/reports/TEST-HammerCLI-Output-FieldFilter.xml +0 -13
  179. data/test/reports/TEST-HammerCLI-Output-Formatters-BooleanFormatter.xml +0 -11
  180. data/test/reports/TEST-HammerCLI-Output-Formatters-ColorFormatter.xml +0 -9
  181. data/test/reports/TEST-HammerCLI-Output-Formatters-DateFormatter.xml +0 -11
  182. data/test/reports/TEST-HammerCLI-Output-Formatters-FieldFormatter.xml +0 -13
  183. data/test/reports/TEST-HammerCLI-Output-Formatters-FormatterContainer.xml +0 -13
  184. data/test/reports/TEST-HammerCLI-Output-Formatters-FormatterLibrary.xml +0 -11
  185. data/test/reports/TEST-HammerCLI-Output-Formatters-KeyValueFormatter.xml +0 -13
  186. data/test/reports/TEST-HammerCLI-Output-Formatters-ListFormatter.xml +0 -13
  187. data/test/reports/TEST-HammerCLI-Output-Formatters-LongTextFormatter.xml +0 -13
  188. data/test/reports/TEST-HammerCLI-Output-Output-adapters.xml +0 -17
  189. data/test/reports/TEST-HammerCLI-Output-Output-data.xml +0 -15
  190. data/test/reports/TEST-HammerCLI-Output-Output-formatters.xml +0 -9
  191. data/test/reports/TEST-HammerCLI-Output-Output-messages.xml +0 -19
  192. data/test/reports/TEST-HammerCLI-Output-Output.xml +0 -7
  193. data/test/reports/TEST-HammerCLI-Output-RecordCollection.xml +0 -13
  194. data/test/reports/TEST-HammerCLI-Settings-load-from-paths.xml +0 -15
  195. data/test/reports/TEST-HammerCLI-Settings.xml +0 -25
  196. data/test/reports/TEST-HammerCLI-ShellHistory-loading-old-history.xml +0 -11
  197. data/test/reports/TEST-HammerCLI-ShellHistory-saving-history.xml +0 -15
  198. data/test/reports/TEST-HammerCLI-ShellHistory.xml +0 -7
  199. data/test/reports/TEST-MiniTest-Spec.xml +0 -7
  200. data/test/reports/TEST-String-camelize.xml +0 -11
  201. data/test/reports/TEST-String-formatting.xml +0 -17
  202. data/test/reports/TEST-String-indent.xml +0 -11
  203. data/test/reports/TEST-String-interactive-.xml +0 -13
  204. data/test/reports/TEST-String.xml +0 -7
  205. data/test/reports/TEST-constraints-HammerCLI-Validator-AllConstraint-exist-.xml +0 -13
  206. data/test/reports/TEST-constraints-HammerCLI-Validator-AllConstraint.xml +0 -7
  207. data/test/reports/TEST-constraints-HammerCLI-Validator-AnyConstraint-exist-.xml +0 -13
  208. data/test/reports/TEST-constraints-HammerCLI-Validator-AnyConstraint.xml +0 -7
  209. data/test/reports/TEST-constraints-HammerCLI-Validator-BaseConstraint-exist-.xml +0 -9
  210. data/test/reports/TEST-constraints-HammerCLI-Validator-BaseConstraint-rejected.xml +0 -13
  211. data/test/reports/TEST-constraints-HammerCLI-Validator-BaseConstraint-required.xml +0 -13
  212. data/test/reports/TEST-constraints-HammerCLI-Validator-BaseConstraint.xml +0 -7
  213. data/test/reports/TEST-constraints.xml +0 -7
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 257e54094a8995f71d00dbe29299e84801b68404271de7baaad58b6a9207fe93
4
- data.tar.gz: f14b2acba71f1a4ad5a237f7dc6da746a23245ba30c504e689bf5a319e5e86b2
3
+ metadata.gz: 8e7d0cc6fea99d3276fb653e462448bddc6b8977e0e5ed9df2a1f257365bee84
4
+ data.tar.gz: 72123cef1ffaafb8979ff5e6bac55ca0fa9776e88800327ad41b48404a3a6284
5
5
  SHA512:
6
- metadata.gz: 559d6704483f5dd8bb2f27ba7c0f8d3c4212033297168fad4cdea0696ee4cd31f8421f1e79cd758b9f4228ff45d2c41751f7509091de0f92ab86a1dab4d97649
7
- data.tar.gz: 2396627dc3f2b9d97dca8b0501dc2260b6c02d4a59fbf64e2fbfcdf625d913703dd02d47557193d575156f5a2b1270c81c57ff8caae08345675567d063ecc892
6
+ metadata.gz: 1514dfe8e1059a70da7af58887e9763d604eaec59ec438af136b2a430338ba0c4c9222e4e86f275fb12135165dabb1f51281daaba5ba48924e32d206307bd94e
7
+ data.tar.gz: a17a67150e2bf0fb293f167d72cc5425e0fe0ebc9eb57c30144e5be952dbb45d8e370257408893727cfadcf3ce6d2607e098f90a1eb40955ff0dc98dc1ac39f1
@@ -0,0 +1,28 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require 'English'
4
+ @project_root = File.expand_path('../..', __FILE__)
5
+ $LOAD_PATH.unshift(File.join(@project_root, 'lib'))
6
+ HAMMER = ENV['HAMMER'] || File.join(@project_root, 'bin/hammer')
7
+
8
+ require 'yaml'
9
+
10
+ completion_cache_file = ENV['HAMMER_COMPLETION_CACHE'] || '~/.cache/hammer_completion.yml'
11
+ completion_cache_file = File.expand_path(completion_cache_file)
12
+
13
+ # build the cache if it does not exist
14
+ unless File.exist?(completion_cache_file)
15
+ require 'hammer_cli'
16
+ `#{HAMMER} prebuild-bash-completion`
17
+ end
18
+
19
+ require 'hammer_cli/bash/completion'
20
+
21
+ dict = HammerCLI::Bash::Completion.load_description(completion_cache_file)
22
+
23
+ comp_line = ENV['COMP_LINE'] || ''
24
+ comp_args = comp_line.split(' ', 2).last || ''
25
+
26
+ result = HammerCLI::Bash::Completion.new(dict).complete(comp_args)
27
+
28
+ puts result.join("\n")
@@ -44,6 +44,8 @@
44
44
  # Log record pattern (logging gem syntax)
45
45
  #:log_pattern: '[%5l %d %c] %m'
46
46
 
47
+ # Location of cache for bash completion
48
+ :completion_cache_file: '~/.cache/hammer_completion.yml'
47
49
 
48
50
  # SSL auth options
49
51
  #:ssl:
@@ -0,0 +1,5 @@
1
+ #
2
+ # Hammer CLI bash completion script
3
+ #
4
+
5
+ complete -C hammer-complete -o nospace hammer
@@ -10,6 +10,11 @@ Each command can be easily extended with one ore more `HammerCLI::CommandExtensi
10
10
  inheritable true
11
11
  # Simply add a new option to a command is being extended
12
12
  option(option_params)
13
+ # Add option family to a command
14
+ option_family(common_options = {}) do
15
+ parent option_params
16
+ child option_params
17
+ end
13
18
  # Extend hash with data returned from server before it is printed
14
19
  before_print do |data|
15
20
  # data modifications
@@ -65,6 +70,13 @@ class MyCommandExtensions < HammerCLI::CommandExtensions
65
70
 
66
71
  option ['--new-option'], 'TYPE', _('Option description')
67
72
 
73
+ option_family(
74
+ description: _('Common description')
75
+ ) do
76
+ parent ['--new-option'], 'TYPE', _('Option description')
77
+ child ['--new-option-ver2'], 'TYPE', _('Option description')
78
+ end
79
+
68
80
  before_print do |data|
69
81
  data['results'].each do |result|
70
82
  result['status'] = process_errors(result['errors'])
@@ -173,6 +173,54 @@ Here is the list of predefined options:
173
173
  * `:fields` Expects a list with fields to show in output, see [example](creating_commands.md#printing-hash-records).
174
174
 
175
175
 
176
+ ### Option family
177
+ Option family is the way to unify options which have the same meaning or purpose,
178
+ but contain some differences in their definitions (e.g. the name/switch of an option).
179
+ Mainly serves as a container for options, which purpose is to show less repetitive
180
+ output in commands' help. Option builders use it by default.
181
+
182
+ To define an option family, use the following DSL:
183
+ ```ruby
184
+ # options is a Hash with options for family/each defined option within it
185
+ option_family(options = {}) do
186
+ # parent is the main option. Must be single, option family can have only one parent.
187
+ parent switches, type, description, options
188
+ # child is an additional option. Could be none or more than one. Aren't shown in the help output.
189
+ child switches, type, description, options
190
+ end
191
+ ```
192
+
193
+ ##### Example
194
+
195
+ ```ruby
196
+ option_family(
197
+ aliased_resource: 'environment',
198
+ description: _('Puppet environment'),
199
+ deprecation: _("Use %s instead") % '--puppet-environment[-id]'
200
+ deprecated: { '--environment' => _("Use %s instead") % '--puppet-environment[-id]',
201
+ '--environment-id' => _("Use %s instead") % '--puppet-environment[-id]'}
202
+ ) do
203
+ parent '--environment-id', 'ENVIRONMENT_ID', _(''),
204
+ format: HammerCLI::Options::Normalizers::Number.new,
205
+ attribute_name: :option_environment_id
206
+ child '--environment', 'ENVIRONMENT_NAME', _('Environment name'),
207
+ attribute_name: :option_environment_name
208
+ end
209
+
210
+ # $ hammer command --help:
211
+ # ...
212
+ # Options:
213
+ # --environment[-id] Puppet environment (Deprecated: Use --puppet-environment[-id] instead)
214
+ # ...
215
+
216
+ # $ hammer full-help:
217
+ # ...
218
+ # Options:
219
+ # --environment ENVIRONMENT_NAME Environment name (--environment is deprecated: Use --puppet-environment[-id] instead)
220
+ # --environment-id ENVIRONMENT_ID (--environment-id is deprecated: Use --puppet-environment[-id] instead)
221
+ # ...
222
+ ```
223
+
176
224
  ### Option builders
177
225
  Hammer commands offer option builders that can be used for automatic option generation.
178
226
  See [documentation page](option_builders.md#option-builders) dedicated to this topic for more details.
@@ -406,6 +454,31 @@ Options:
406
454
  -h, --help print help
407
455
  ```
408
456
 
457
+ #### Aliasing subcommands
458
+
459
+ Commands can have two or more names, e.g. aliases. To support such functionality
460
+ simple name addition could be used via `command_name` or `command_names` method:
461
+ ```ruby
462
+ module HammerCLIHello
463
+
464
+ class SayCommand < HammerCLI::AbstractCommand
465
+
466
+ class GreetingsCommand < HammerCLI::AbstractCommand
467
+ command_name 'hello'
468
+ command_name 'hi'
469
+ # or use can use other method:
470
+ command_names 'hello', 'hi'
471
+
472
+ desc 'Say Hello World!'
473
+ # ...
474
+ end
475
+
476
+ autoload_subcommands
477
+ end
478
+
479
+ HammerCLI::MainCommand.subcommand 'say', "Say something", HammerCLIHello::SayCommand
480
+ end
481
+ ```
409
482
 
410
483
  ### Conflicting subcommands
411
484
  It can happen that two different plugins define subcommands with the same name by accident.
@@ -494,6 +567,33 @@ You first create an _output definition_ that you apply to your data. The result
494
567
  is a collection of fields, each having its type. The collection is then passed to an
495
568
  _output adapter_ which handles the actual formatting and printing.
496
569
 
570
+ Adapters support printing by chunks, e.g. if you want to print a large set of
571
+ data (1000+ records), but you make several calls to the server instead of one,
572
+ you may want to print received data right away instead of waiting for the rest.
573
+ This can be achieved via `:current_chunk` option for
574
+ `print_collection` and `print_data` methods. Allowed values for `:current_chunk`
575
+ are `:first`, `:another`, `:last`. By default adapters use `:single` value that
576
+ means only one record will be printed.
577
+
578
+ ##### Printing by chunks
579
+ ```ruby
580
+ # ...
581
+ def execute
582
+ loop do
583
+ # ...
584
+ data = send_request
585
+ print_data(data, current_chunk: :first)
586
+ # ...
587
+ data = send_request
588
+ print_data(data, current_chunk: :another)
589
+ # ...
590
+ data = send_request
591
+ print_data(data, current_chunk: :last)
592
+ end
593
+ end
594
+ # ...
595
+ ```
596
+
497
597
  Hammer provides a DSL for defining the output. Next rather complex example will
498
598
  explain how to use it in action.
499
599
 
@@ -132,8 +132,51 @@ And you are done. Your hammer client is configured and ready to use.
132
132
  Autocompletion
133
133
  --------------
134
134
 
135
- It is necessary to copy the hammer_cli_complete script to the bash_completion.d directory.
136
-
137
- $ sudo cp hammer-cli/hammer_cli_complete /etc/bash_completion.d/
135
+ The completion offers suggestion of possible command-line subcommands and their
136
+ options as usual. It can also suggest values for options and params where file
137
+ or directory path is expected.
138
+
139
+ Bash completion is automatically installed by RPM. To use it for development
140
+ setup `cp ./config/hammer.completion /etc/bash_completion.d/hammer` and load it
141
+ to the current shell `source /etc/bash_completion.d/hammer`. Make sure
142
+ the `$PWD/bin` is in `PATH` or there is full path to `hammer-complete`
143
+ executable specified in `/etc/bash_completion.d/hammer`.
144
+
145
+ Bash completion for hammer needs pre-built cache that holds description of
146
+ all subcommands and its parameters. The cache is located by default in
147
+ `~/.cache/hammer_completion.yml`. The location can be changed in hammer's
148
+ config file. The cache can be built manually with
149
+ `hammer prebuild-bash-completion` or is built automatically when completion is
150
+ used and the cache is missing (this may cause slight delay). The cache expires
151
+ if your API cache was changed (it indicates that the features on the instance
152
+ may have changed which has impact on hammer CLI options and subcommands).
153
+
154
+ #### Available value types
155
+
156
+ Completion of values is dependent on CLI option and prameter settings, e.g.:
157
+
158
+ ```ruby
159
+ option '--value', 'VALUE', 'One of a, b, c', completion: { type: :enum, values: %w[a b c] }
160
+ ```
138
161
 
139
- Then after starting a new shell the completion should work.
162
+ Possible options for the `:completion` attribute are:
163
+ - `{ type: :flag }` option has no value, default for flags.
164
+ - `{ type: :value }` option has value of unknown type, no suggestions for the
165
+ value, default.
166
+ - `{ type: :list }` option has value of list type, no suggestions for the
167
+ value.
168
+ - `{ type: :key_value_list }` option has value of key=value list type, no
169
+ suggestions for the value.
170
+ - `{ type: :directory }` value is directory, suggestions follow directory
171
+ structure.
172
+ - `{ type: :file, filter: '\.txt$' }` value is file, suggestions follow
173
+ directory structure, optional `:filter` is regexp to filter the results.
174
+ - `{ type: :enum, values: ['first', 'second']}` option can have one of the
175
+ listed values, suggestions follow specified `values`.
176
+ - `{ type: :multienum, values: ['first', 'second']}` option can have one or
177
+ more of the listed values, suggestions follow specified `values`.
178
+ - `{ type: :schema, schema: 'a=int\,b=string' }` option should have value
179
+ according to specified schema, suggestion is the specified schema.
180
+
181
+ All those completion attributes are generated automatically, specify you own to
182
+ override.
@@ -1,11 +1,38 @@
1
1
  Release notes
2
2
  =============
3
+ ### 2.1.1 (2020-05-29)
4
+ * Clean gem_release.ipynb up
5
+ * Replace awesome_print with amazing_print ([PR #330](https://github.com/theforeman/hammer-cli/pull/330)), [#29846](http://projects.theforeman.org/issues/29846)
6
+ * Fix typo: s/filed/feild/ ([PR #331](https://github.com/theforeman/hammer-cli/pull/331))
7
+
8
+ ### 2.1.0 (2020-05-14)
9
+ * Hammer full-help returns correct output, [#29697](http://projects.theforeman.org/issues/29697)
10
+ * Add fuzzy subcommand matching, [#29413](http://projects.theforeman.org/issues/29413)
11
+ * Help contains squeezed options, [#28440](http://projects.theforeman.org/issues/28440)
12
+ * Keep referenced resource in option options, [#29015](http://projects.theforeman.org/issues/29015)
13
+ * Bump to 2.1.0-develop
14
+
15
+ ### 2.0.0 (2020-02-12)
16
+ * Bump version to 2.0.0
17
+ * Bump version to 2.0 ([PR #324](https://github.com/theforeman/hammer-cli/pull/324))
18
+ * Better promts for missing arguments, [#28793](http://projects.theforeman.org/issues/28793)
19
+ * Allow column max width more than 80, [#28503](http://projects.theforeman.org/issues/28503)
20
+ * Remove computing sha, [#27728](http://projects.theforeman.org/issues/27728)
21
+ * Fixed userdata false display in image list, [#28134](http://projects.theforeman.org/issues/28134)
22
+ * Add new bash completion, [#27728](http://projects.theforeman.org/issues/27728)
23
+ * Allow adapters print page by page, [#17819](http://projects.theforeman.org/issues/17819)
24
+ * Add release documentation ([PR #317](https://github.com/theforeman/hammer-cli/pull/317)), [#28149](http://projects.theforeman.org/issues/28149)
25
+ * Fix pr links in release notes ([PR #318](https://github.com/theforeman/hammer-cli/pull/318)), [#28202](http://projects.theforeman.org/issues/28202)
26
+ * Extract table generator into reusable component ([PR #314](https://github.com/theforeman/hammer-cli/pull/314)), [#27318](http://projects.theforeman.org/issues/27318)
27
+ * Better prompts for missing arguments ([PR #313](https://github.com/theforeman/hammer-cli/pull/313)), [#27595](http://projects.theforeman.org/issues/27595)
28
+ * Bump to 0.20-develop
29
+
3
30
  ### 0.19.0 (2019-10-26)
4
- * Allow schema building for custom options ([PR #316](https://github.com/Apipie/apipie-bindings/pull/316)), [#27899](http://projects.theforeman.org/issues/27899)
5
- * New lines in text attr dont break output ([PR #300](https://github.com/Apipie/apipie-bindings/pull/300)), [#25878](http://projects.theforeman.org/issues/25878)
6
- * Added error to wrong --output ([PR #315](https://github.com/Apipie/apipie-bindings/pull/315)), [#21590](http://projects.theforeman.org/issues/21590)
7
- * Pr review checklist ([PR #305](https://github.com/Apipie/apipie-bindings/pull/305)), [#26950](http://projects.theforeman.org/issues/26950)
8
- * List items in help with customization ([PR #309](https://github.com/Apipie/apipie-bindings/pull/309)), [#27237](http://projects.theforeman.org/issues/27237)
31
+ * Allow schema building for custom options ([PR #316](https://github.com/theforeman/hammer-cli/pull/316)), [#27899](http://projects.theforeman.org/issues/27899)
32
+ * New lines in text attr dont break output ([PR #300](https://github.com/theforeman/hammer-cli/pull/300)), [#25878](http://projects.theforeman.org/issues/25878)
33
+ * Added error to wrong --output ([PR #315](https://github.com/theforeman/hammer-cli/pull/315)), [#21590](http://projects.theforeman.org/issues/21590)
34
+ * Pr review checklist ([PR #305](https://github.com/theforeman/hammer-cli/pull/305)), [#26950](http://projects.theforeman.org/issues/26950)
35
+ * List items in help with customization ([PR #309](https://github.com/theforeman/hammer-cli/pull/309)), [#27237](http://projects.theforeman.org/issues/27237)
9
36
 
10
37
  ### 0.18.0 (2019-08-01)
11
38
  * Unsure minimal label length ([PR #310](https://github.com/theforeman/hammer-cli/pull/310)) ([#26960](http://projects.theforeman.org/issues/26960))
@@ -23,3 +23,4 @@ require 'hammer_cli/apipie'
23
23
  require 'hammer_cli/shell'
24
24
  require 'hammer_cli/defaults'
25
25
  require 'hammer_cli/full_help'
26
+ require 'hammer_cli/bash'
@@ -13,6 +13,7 @@ require 'hammer_cli/options/predefined'
13
13
  require 'hammer_cli/help/builder'
14
14
  require 'hammer_cli/help/text_builder'
15
15
  require 'hammer_cli/command_extensions'
16
+ require 'hammer_cli/options/option_family'
16
17
  require 'logging'
17
18
 
18
19
  module HammerCLI
@@ -74,6 +75,7 @@ module HammerCLI
74
75
  begin
75
76
  begin
76
77
  exit_code = super
78
+ context.delete(:fields)
77
79
  raise "exit code must be integer" unless exit_code.is_a? Integer
78
80
  rescue => e
79
81
  exit_code = handle_exception(e)
@@ -190,10 +192,21 @@ module HammerCLI
190
192
  # skip switches that are already defined
191
193
  next if option.nil? or option.switches.any? {|s| find_option(s) }
192
194
 
195
+ if option.respond_to?(:referenced_resource)
196
+ # Collect options that don't have family, but related to this parent.
197
+ children = find_options(
198
+ referenced_resource: option.referenced_resource.to_s,
199
+ aliased_resource: option.aliased_resource.to_s
200
+ ).select { |o| o.family.nil? || o.family.head.nil? }
201
+ children.each do |child|
202
+ option.family.adopt(child) if option.family
203
+ end
204
+ end
193
205
  declared_options << option
194
206
  block ||= option.default_conversion_block
195
207
  define_accessors_for(option, &block)
196
208
  extend_options_help(option) if option.value_formatter.is_a?(HammerCLI::Options::Normalizers::ListNested)
209
+ completion_type_for(option)
197
210
  end
198
211
  end
199
212
 
@@ -205,6 +218,7 @@ module HammerCLI
205
218
  extension.delegatee(self)
206
219
  extension.extend_predefined_options(self)
207
220
  extension.extend_options(self)
221
+ extension.extend_option_family(self)
208
222
  extension.extend_output(self)
209
223
  extension.extend_help(self)
210
224
  logger('Extensions').info "Applied #{extension.details} on #{self}."
@@ -220,6 +234,12 @@ module HammerCLI
220
234
 
221
235
  protected
222
236
 
237
+ def self.option_family(options = {}, &block)
238
+ options[:creator] ||= self
239
+ family = HammerCLI::Options::OptionFamily.new(options)
240
+ family.instance_eval(&block)
241
+ end
242
+
223
243
  def self.find_options(switch_filter, other_filters={})
224
244
  filters = other_filters
225
245
  if switch_filter.is_a? Hash
@@ -242,8 +262,8 @@ module HammerCLI
242
262
  output.print_record(definition, record)
243
263
  end
244
264
 
245
- def print_collection(definition, collection)
246
- output.print_collection(definition, collection)
265
+ def print_collection(definition, collection, options = {})
266
+ output.print_collection(definition, collection, options)
247
267
  end
248
268
 
249
269
  def print_message(msg, msg_params = {}, options = {})
@@ -283,8 +303,17 @@ module HammerCLI
283
303
  end
284
304
 
285
305
  def self.command_name(name=nil)
286
- @name = name if name
287
- @name || (superclass.respond_to?(:command_name) ? superclass.command_name : nil)
306
+ if @names && name
307
+ @names << name if !@names.include?(name)
308
+ else
309
+ @names = [name] if name
310
+ end
311
+ @names || (superclass.respond_to?(:command_names) ? superclass.command_names : nil)
312
+ end
313
+
314
+ def self.command_names(*names)
315
+ @names = names unless names.empty?
316
+ @names || (superclass.respond_to?(:command_names) ? superclass.command_names : nil)
288
317
  end
289
318
 
290
319
  def self.warning(message = nil)
@@ -314,6 +343,7 @@ module HammerCLI
314
343
  declared_options << option
315
344
  block ||= option.default_conversion_block
316
345
  define_accessors_for(option, &block)
346
+ completion_type_for(option, opts)
317
347
  end
318
348
  extend_options_help(option) if option.value_formatter.is_a?(HammerCLI::Options::Normalizers::ListNested)
319
349
  option
@@ -352,6 +382,33 @@ module HammerCLI
352
382
  sources
353
383
  end
354
384
 
385
+ def self.completion_map
386
+ completion = {}
387
+ # collect options
388
+ recognised_options.each do |opt|
389
+ opt.switches.each do |switch|
390
+ completion[switch] = completion_types.fetch(switch, {})
391
+ end
392
+ end
393
+ # collect subcommands recursively
394
+ recognised_subcommands.each do |cmd|
395
+ completion[cmd.names.first] = cmd.subcommand_class.completion_map
396
+ end
397
+ # collect params
398
+ completion[:params] = completion_types[:params] unless completion_types[:params].empty?
399
+ completion
400
+ end
401
+
402
+ def self.completion_types
403
+ @completion_types ||= { :params => [] }
404
+ end
405
+
406
+ def self.completion_type_for(option, opts = {})
407
+ completion_type = opts.delete(:completion)
408
+ completion_type ||= option.completion_type(opts[:format])
409
+ [option.switches].flatten(1).each { |s| completion_types[s] = completion_type }
410
+ end
411
+
355
412
  private
356
413
 
357
414
  def self.inherited_output_definition
@@ -10,7 +10,11 @@ module HammerCLI::Apipie
10
10
  @api = ApipieBindings::API.new(params, HammerCLI::SSLOptions.new.get_options(params[:uri]))
11
11
  if options[:reload_cache]
12
12
  @api.clean_cache
13
- @logger.debug 'Apipie cache was cleared' unless @logger.nil?
13
+ HammerCLI.clear_cache
14
+ unless @logger.nil?
15
+ @logger.debug 'Apipie cache was cleared'
16
+ @logger.debug 'Completion cache was cleared'
17
+ end
14
18
  end
15
19
  end
16
20