Module: GreenHat::Args

Defined in:
lib/greenhat/shell/args.rb

Overview

Variable Breakdown Args:Array supplied or default values for entry looping action Flags:Hash Key/Value filter Actionables Files:Array/String are any non start ‘-` dash entries

Class Method Summary collapse

Class Method Details

.arg_normalize(field, value) ⇒ Object

Naturlalize Values, Manipluate Special Values



109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
# File 'lib/greenhat/shell/args.rb', line 109

def self.arg_normalize(field, value)
  # Special Comma Params With Symbol
  return value.split(',').map(&:to_sym) if arg_special_split.include?(field)

  # Integer Arguments
  return value.to_i if value.numeric?

  # Other Field Manipulation
  case field
  when :page then value == 'true'
  else

    # Default Original
    value
  end
end

.arg_scan(arg) ⇒ Object

Arg Scan (Split – values into keys)



79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
# File 'lib/greenhat/shell/args.rb', line 79

def self.arg_scan(arg)
  arg.scan(/^-+([^=]+)=(.*)/).map do |field, value|
    bang = false
    if field.include? '!'
      field.delete!('!')
      bang = true
    end

    # Symbolize
    field = field.to_sym

    {
      field: field,
      value: arg_normalize(field, value),
      bang: bang
    }
  end
end

.arg_special_splitObject

Arguments that Accept multiple options / Comma Delimted



127
128
129
130
131
# File 'lib/greenhat/shell/args.rb', line 127

def self.arg_special_split
  %i[
    slice except uniq pluck sort archive stats exists
  ]
end

.arg_to_flag(flags, args) ⇒ Object

Move Valued Flags to arguments (–truncate=5)



56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/greenhat/shell/args.rb', line 56

def self.arg_to_flag(flags, args)
  args.reject! do |arg|
    # Entries specifically to move to Args
    if arg_to_flag_list.include?(arg.field)
      flags[arg.field] = arg.value

      true
    # Ignore Good Entries
    else
      false
    end
  end
end

.arg_to_flag_listObject

Flags Anything that isn’t sent as a key/filter



71
72
73
74
75
76
# File 'lib/greenhat/shell/args.rb', line 71

def self.arg_to_flag_list
  %i[
    archive end except exists json limit pluck reverse round slice sort start
    stats truncate uniq page time_zone table_style
  ]
end

.flag_arg_defaults(field) ⇒ Object

Arg Defaults



134
135
136
137
138
139
140
141
142
143
144
# File 'lib/greenhat/shell/args.rb', line 134

def self.flag_arg_defaults(field)
  case field
  when :round then 2
  when :limit then (TTY::Screen.height / 3) - 3
  when :truncate then TTY::Screen.width * 4
  # when :page, :case, :exact then :true # Override Specials
  when *arg_special_split then []
  else
    true
  end
end

.flag_defaults(flags, skip_flags) ⇒ Object



39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/greenhat/shell/args.rb', line 39

def self.flag_defaults(flags, skip_flags)
  # Update other user defaults
  Settings.default_log_flags(flags, skip_flags)

  # FastStats / Don't set default flags
  return if skip_flags.include? :logic

  # Default Logic
  if flags.key?(:or)
    flags.logic = :any?
    flags.delete(:or)
  else
    flags.logic = :all?
  end
end

.flag_scan(arg) ⇒ Object

Collect All Flags



99
100
101
102
103
104
105
106
# File 'lib/greenhat/shell/args.rb', line 99

def self.flag_scan(arg)
  arg.scan(/^-+([^=]+)$/).map do |field, _val|
    # Symbolize
    field = field.to_sym

    [field, flag_arg_defaults(field)]
  end
end

.parse(raw, skip_flags = []) ⇒ Object



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/greenhat/shell/args.rb', line 15

def self.parse(raw, skip_flags = [])
  # Don't affect original object / Better deep clone?
  cmd = raw.clone
  # cmd = Marshal.load(Marshal.dump(raw))

  # Extract Files
  files = cmd.grep_v(/^-+([^=]+)/)

  # Collect and Naturalize Arguments
  args = cmd.flat_map { |arg| arg_scan(arg) }

  # Collect all Flags to Hash
  flags = cmd.flat_map { |flag| flag_scan(flag) }.to_h

  # Move Flags to Args
  arg_to_flag(flags, args)

  # Logic
  # And / Or
  flag_defaults(flags, skip_flags)

  [files, flags, args]
end