Module: Morpheus::Cli::PromptHelper
- Defined in:
- lib/morpheus/cli/mixins/prompt_helper.rb
Overview
Mixin for Morpheus::Cli command classes Provides common methods for prompting for option type inputs and forms. Prompting is delegated to the OptionTypes module while the provided #prompt simplifies the required parameters The command class must establish the @api_client on its own.
Instance Method Summary collapse
-
#no_prompt(option_types, options = {}) ⇒ Object
Process 1-N inputs (OptionType) in a special ‘edit mode’ that supresses user interaction.
-
#prompt(option_types, options = {}) ⇒ Hash
Prompt for a list of inputs (OptionType) and return a Hash containing all of the provides values.
-
#prompt_form(form, options = {}) ⇒ Object
Process inputs of a form, prompting for its options and field groups.
-
#prompt_value(option_type, options = {}) ⇒ String, ...
Prompt for a single input and return only the value.
Instance Method Details
#no_prompt(option_types, options = {}) ⇒ Object
Process 1-N inputs (OptionType) in a special ‘edit mode’ that supresses user interaction. This is used by update
commands where we want to process option types without prompting the user so that the results only contains values that are passed in explicitely as options and default values are not used.
67 68 69 70 71 |
# File 'lib/morpheus/cli/mixins/prompt_helper.rb', line 67 def no_prompt(option_types, ={}) = .merge({:edit_mode => true, :no_prompt => true}) .delete(:no_prompt) if [:always_prompt] # --prompt to always prompt return prompt(option_types, ) end |
#prompt(option_types, options = {}) ⇒ Hash
Prompt for a list of inputs (OptionType) and return a Hash containing all of the provides values. The user is prompted to provide a value for each input, unless the value is already set in the options.
39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 |
# File 'lib/morpheus/cli/mixins/prompt_helper.rb', line 39 def prompt(option_types, ={}) # option types can be passed as a single input instead of an array option_types = option_types.is_a?(Hash) ? [option_types] : option_types #Array(option_types) # construct options parameter for Morpheus::Cli::OptionTypes.prompt = () # by default the @api_client established by the command is used api_client = .key?(:api_client) ? [:api_client] : @api_client api_params = .key?(:api_params) ? [:api_params] : {} no_prompt = .key?(:no_prompt) ? [:no_prompt] : false paging_enabled = .key?(:paging_enabled) ? [:paging_enabled] : true ignore_empty = .key?(:ignore_empty) ? [:ignore_empty] : false # Defaulting skip_sort to true which is the opposite of OptionTypes.prompt() # The API handles sorting most of the time now, calling function can sort before prompting if needed # maybe switch this later if needed, removing skip_sort would be nice though... skip_sort = .key?(:skip_sort) ? [:skip_sort] : true results = Morpheus::Cli::OptionTypes.prompt(option_types, , api_client, api_params, no_prompt, paging_enabled, ignore_empty, skip_sort) # trying to get rid of the need to do these compact and booleanize calls.. # for now you can use options.merge({compact: true,booleanize: true}) or modify the results yourself results.deep_compact! if [:compact] results.booleanize! if [:booleanize] # 'on' => true return results end |
#prompt_form(form, options = {}) ⇒ Object
Process inputs of a form, prompting for its options and field groups
103 104 105 106 107 108 109 110 111 112 113 114 115 |
# File 'lib/morpheus/cli/mixins/prompt_helper.rb', line 103 def prompt_form(form, ={}) form_results = {} results = prompt(Array(form['options']), .merge(form_results)) form_results.deep_merge!(results) # prompt for each field group, merging results into options context as we go Array(form['fieldGroups']).each do |field_group| # todo: look at isCollapsible, defaultCollapsed and visibleOnCode to see if the group should # if collapsed then just use options.merge({:no_prompt => true}) and maybe need to set :required => false ? results = prompt(field_group['options'], .merge(form_results)) form_results.deep_merge!(results) end return form_results end |
#prompt_value(option_type, options = {}) ⇒ String, ...
Prompt for a single input and return only the value
84 85 86 87 88 89 90 91 92 93 94 95 |
# File 'lib/morpheus/cli/mixins/prompt_helper.rb', line 84 def prompt_value(option_type, ={}) # this does not need with fieldContext, so get rid of it # option_types = [option_type.merge({'fieldContext' => nil})] # results = prompt(Array(option_type), options) # return results[option_type['fieldName']] # this works with fieldContext now, hooray # use get_object_value() to traverse object to get the value option_types = [option_type] # Array(option_type) results = prompt(option_types, ) return get_option_type_value(results, option_types.first) end |