Class: Ned::Cut
Instance Method Summary collapse
Methods inherited from Command
config, description, #execute, #execute_all, help, #initialize, #load_lines, long_name, option_parser, #options, #peek, require_all, #require_all, #require_all?, require_all?, short_name
Constructor Details
This class inherits a constructor from Ned::Command
Instance Method Details
#execute_internal(line) ⇒ Object
66 67 68 69 70 71 72 73 74 75 76 77 |
# File 'lib/ned/commands/cut.rb', line 66 def execute_internal(line) ending = line[-1] == "\n" ? line.slice!(-1) : '' columns = line.split([:pattern] || [:delimiter]) result = [:ranges].flat_map do |range| columns[range] end.join([:join]) result << ending @require_split ? result.split(/(?<=\n)/) : result end |
#parse(args) ⇒ Object
51 52 53 54 55 56 57 58 59 60 61 62 63 64 |
# File 'lib/ned/commands/cut.rb', line 51 def parse(args) super raise OptionParser::ParseError.new('specify only one of --delimiter and --pattern') if [:delimiter] && [:pattern] raise OptionParser::ParseError.new('you must specify one or more field indices or ranges') unless [:ranges] if [:delimiter] [:join] ||= [:delimiter] else [:join] ||= ' ' end @require_split = !![:join]&.index("\n") end |