Method: MiGA::Cli::OptHelper#opt_object

Defined in:
lib/miga/cli/opt_helper.rb

#opt_object(opt, what = %i[project dataset])) ⇒ Object

Options to load an object passed to OptionParser opt, as determined by what an Array with any combination of:

  • :project To require a project

  • :dataset To require a dataset

  • :dataset_opt To allow (optionally) a dataset

  • :dataset_type To allow (optionally) a type of dataset

  • :dataset_type_req To require a type of dataset

  • :project_type To allow (optionally) a type of project

  • :project_type_req To require a type of project

  • :result To require a type of project or dataset result

  • :result_opt To allow (optionally) a type of result

  • :result_dataset To require a type of dataset result

  • :result_project To require a type of project result

The options :result, :result_opt, :result_dataset, and :result_project are mutually exclusive



69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
# File 'lib/miga/cli/opt_helper.rb', line 69

def opt_object(opt, what = %i[project dataset])
  what.each do |w|
    case w
    when :project
      opt.on(
        '-P', '--project PATH',
        '(Mandatory) Path to the project'
      ) { |v| self[:project] = v }
    when :dataset, :dataset_opt
      opt.on(
        '-D', '--dataset STRING',
        (w == :dataset ? '(Mandatory) ' : '') + 'Name of the dataset'
      ) { |v| self[:dataset] = v }
    when :dataset_type, :dataset_type_req, :project_type, :project_type_req
      obj = w.to_s.gsub(/_.*/, '')
      klass = Object.const_get("MiGA::#{obj.capitalize}")
      req = w.to_s =~ /_req$/ ? '(Mandatory) ' : ''
      opt.on(
        '-t', '--type STRING',
        "#{req}Type of #{obj}. Recognized types include:",
        *klass.KNOWN_TYPES.map { |k, v| "~ #{k}: #{v[:description]}" }
      ) { |v| self[:type] = v.downcase.to_sym }
    when :result, :result_opt
      opt.on(
        '-r', '--result STRING',
        "#{'(Mandatory) ' if w == :result}Name of the result",
        'Recognized names for dataset-specific results include:',
        *MiGA::Dataset.RESULT_DIRS.keys.map { |n| " ~ #{n}" },
        'Recognized names for project-wide results include:',
        *MiGA::Project.RESULT_DIRS.keys.map { |n| " ~ #{n}" }
      ) { |v| self[:result] = v.downcase.to_sym }
    when :result_dataset
      opt.on(
        '-r', '--result STRING',
        '(Mandatory) Name of the result, one of:',
        *MiGA::Dataset.RESULT_DIRS.keys.map { |n| " ~ #{n}" }
      ) { |v| self[:result] = v.downcase.to_sym }
    when :result_project
      opt.on(
        '-r', '--result STRING',
        '(Mandatory) Name of the result, one of:',
        *MiGA::Project.RESULT_DIRS.keys.map { |n| " ~ #{n}" }
      ) { |v| self[:result] = v.downcase.to_sym }
    else
      raise "Internal error: Unrecognized option: #{w}"
    end
  end
end