Class: OsCtl::Lib::Cli::ParameterSelector

Inherits:
Object
  • Object
show all
Defined in:
lib/libosctl/cli/parameter_selector.rb

Overview

Process output parameters for CLI options -o, –output and -L

Instance Method Summary collapse

Constructor Details

#initialize(all_params:, default_params: nil, allow_user_attributes: true) ⇒ ParameterSelector

Returns a new instance of ParameterSelector.

Parameters:

  • all_params (Array<Symbol>)

    list of all possible parameters

  • default_params (Array<Symbol>, nil) (defaults to: nil)

    list of default parameters

  • allow_user_attributes (Boolean) (defaults to: true)

    allow custom user attributes, i.e. <vendor>:<key>



7
8
9
10
11
# File 'lib/libosctl/cli/parameter_selector.rb', line 7

def initialize(all_params:, default_params: nil, allow_user_attributes: true)
  @all_params = all_params.map(&:to_sym)
  @default_params = default_params ? default_params.map(&:to_sym) : @all_params
  @allow_user_attributes = allow_user_attributes
end

Instance Method Details

#parse_option(option, default_params: nil) ⇒ Array<Symbol>

Parse input from CLI option and return a list of wanted parameters

Parameters:

  • option (String, nil)

    comma-separated list of output parameters

  • default_params (Array<Symbol>, nil) (defaults to: nil)

    list of default parameters

Returns:

  • (Array<Symbol>)

Raises:

  • (GLI::BadCommandLine)


18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/libosctl/cli/parameter_selector.rb', line 18

def parse_option(option, default_params: nil)
  default_params ||= @default_params
  return default_params if option.nil?

  stripped = option.strip
  return [] if stripped.empty?

  wanted = stripped.split(',').map(&:to_sym)

  if wanted.length == 1 && wanted.first == :all
    return @all_params
  end

  if wanted.any?
    if wanted.first.start_with?('+')
      wanted[0] = wanted[0][1..].to_sym
      wanted = default_params + wanted
    elsif wanted.first.start_with?('-')
      wanted[0] = wanted[0][1..].to_sym
      wanted = default_params - wanted
    end
  end

  wanted.each do |param|
    if !@all_params.include?(param) \
       && (!@allow_user_attributes || !param.to_s.include?(':'))
      raise GLI::BadCommandLine, "unknown output parameter #{param.to_s.inspect}"
    end
  end

  wanted
end

#to_sObject



51
52
53
# File 'lib/libosctl/cli/parameter_selector.rb', line 51

def to_s
  @all_params.join("\n")
end