module GreenHat::Args

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

Public Class Methods

arg_normalize(field, value) click to toggle source

Naturlalize Values, Manipluate Special Values

# 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) click to toggle source

Arg Scan (Split – values into keys)

# 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_split() click to toggle source

Arguments that Accept multiple options / Comma Delimted

# 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) click to toggle source

Move Valued Flags to arguments (–truncate=5)

# 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_list() click to toggle source

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

# 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
  ]
end
flag_arg_defaults(field) click to toggle source

Arg Defaults

# 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) click to toggle source
# 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) click to toggle source

Collect All Flags

# 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 = []) click to toggle source
# 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