Class: Glark::InputOptions

Inherits:
InputSpec show all
Includes:
OptionUtil, Logue::Loggable
Defined in:
lib/glark/input/options.rb

Constant Summary

Constants included from OptionUtil

OptionUtil::NONE

Constants inherited from InputSpec

Glark::InputSpec::VALID_BINARY_FILE_TYPES

Instance Attribute Summary

Attributes inherited from InputSpec

#binary_files, #dir_criteria, #directory, #exclude_matching, #file_criteria, #max_depth, #range, #split_as_path

Instance Method Summary collapse

Methods included from OptionUtil

#add_opt, #add_opt_arg, #add_opt_blk, #add_opt_false, #add_opt_int, #add_opt_str, #add_opt_true, #colorize, #set, #set_var, #to_boolean

Constructor Details

#initialize(optdata) ⇒ InputOptions

Returns a new instance of InputOptions.



17
18
19
20
# File 'lib/glark/input/options.rb', line 17

def initialize optdata
  super()
  add_as_options optdata
end

Instance Method Details

#add_as_options(optdata) ⇒ Object



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
117
118
119
# File 'lib/glark/input/options.rb', line 85

def add_as_options optdata    
  optdata << {
    :res => [ Regexp.new('^ -0 (\d{1,3})? $ ', Regexp::EXTENDED) ],
    :set => Proc.new { |md| rs = md ? md[1] : 0; set_record_separator rs }
  }

  @range.add_as_option optdata

  optdata << {
    :tags => %w{ -r --recurse },
    :set  => Proc.new { set_directory("recurse") }
  }

  optdata << {
    :tags => %w{ --directories -d },
    :arg  => [ :string ],
    :set  => Proc.new { |val| set_directory val },
  }

  re = Regexp.new '^[\'\"]?(' + VALID_BINARY_FILE_TYPES.join('|') + ')[\'\"]?$'
  optdata << {
    :tags => %w{ --binary-files },
    :arg  => [ :required, :regexp, re ],
    :set  => Proc.new { |md| @binary_files = md },
    :rc   => %w{ binary-files },
  }
  
  @file_criteria.add_as_options optdata
  @dir_criteria.add_as_options optdata

  add_opt_true optdata, :exclude_matching, %w{ -M --exclude-matching }

  add_opt_false optdata, :split_as_path, %w{ --no-split-as-path }
  add_opt_true optdata, :split_as_path, %w{ --split-as-path }
end

#config_fieldsObject



46
47
48
49
50
51
52
53
# File 'lib/glark/input/options.rb', line 46

def config_fields
  fields = {
    "binary-files" => binary_files,
    "split-as-path" => @split_as_path,
  }
  fields.merge! @dir_criteria.config_fields
  fields.merge! @file_criteria.config_fields
end

#create_fileset(files) ⇒ Object



22
23
24
25
26
27
28
29
30
31
# File 'lib/glark/input/options.rb', line 22

def create_fileset files
  fsargs = Hash.new
  fsargs[:max_depth] = @max_depth
  fsargs[:binary_files] = @binary_files
  fsargs[:dir_criteria] = @dir_criteria
  fsargs[:file_criteria] = @file_criteria
  fsargs[:split_as_path] = @split_as_path
  
  FileSet.new files, fsargs
end

#dump_fieldsObject



55
56
57
58
59
60
61
62
63
64
65
# File 'lib/glark/input/options.rb', line 55

def dump_fields
  fields = {
    "binary_files" => binary_files,
    "directory" => @directory,
    "exclude_matching" => @exclude_matching,
    "split-as-path" => @split_as_path,
  }
  fields.merge! @dir_criteria.dump_fields
  fields.merge! @file_criteria.dump_fields
  fields
end

#set_directory(val) ⇒ Object



79
80
81
82
83
# File 'lib/glark/input/options.rb', line 79

def set_directory val
  @max_depth = val == 'list' ? 0 : nil
  @dir_criteria.skip_all = val == 'skip'
  # otherwise, it's recurse
end

#set_record_separator(sep) ⇒ Object



33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/glark/input/options.rb', line 33

def set_record_separator sep
  $/ = if sep && sep.to_i > 0
         begin
           sep.oct.chr
         rescue RangeError
           # out of range (e.g., 777) means nil:
           nil
         end
       else
         "\n\n"
       end
end

#update_fields(fields) ⇒ Object



67
68
69
70
71
72
73
74
75
76
77
# File 'lib/glark/input/options.rb', line 67

def update_fields fields
  @dir_criteria.update_fields fields
  @file_criteria.update_fields fields

  fields.each do |name, value|
    case name
    when 'split-as-path'
      @split_as_path = to_boolean value
    end
  end
end