Class: RDF::CLI
- Inherits:
-
Object
- Object
- RDF::CLI
- Defined in:
- lib/rdf/cli.rb
Constant Summary collapse
- COMMANDS =
{ "count" => lambda do |argv, opts| start = Time.new count = 0 self.parse(argv, opts) do |reader| reader.each_statement do |statement| count += 1 end end secs = Time.new - start $stdout.puts "Parsed #{count} statements with #{@readers.join(', ')} in #{secs} seconds @ #{count/secs} statements/second." end, "lenghts" => lambda do |argv, opts| self.parse(argv, opts) do |reader| reader.each_statement do |statement| $stdout.puts statement.to_s.size end end end, "objects" => lambda do |argv, opts| self.parse(argv, opts) do |reader| reader.each_statement do |statement| $stdout.puts statement.object.to_ntriples end end end, "predicates" => lambda do |argv, opts| self.parse(argv, opts) do |reader| reader.each_statement do |statement| $stdout.puts statement.predicate.to_ntriples end end end, "serialize" => lambda do |argv, opts| writer_class = RDF::Writer.for(opts[:output_format]) || RDF::NTriples::Writer out = opts[:output] || $stdout opts = opts.merge(:prefixes => {}) writer_opts = opts.merge(:standard_prefixes => true) self.parse(argv, opts) do |reader| writer_class.new(out, writer_opts) do |writer| writer << reader end end end, "subjects" => lambda do |argv, opts| self.parse(argv, opts) do |reader| reader.each_statement do |statement| $stdout.puts statement.subject.to_ntriples end end end }
Class Method Summary collapse
- .abort(msg) ⇒ void
- .basename ⇒ String
-
.commands ⇒ Array<String>
List of executable commands.
- .exec_command(command, args, options = {}) ⇒ Boolean
- .options {|options| ... } ⇒ OptionParser
-
.parse(files, options = {}) {|reader| ... } ⇒ nil
Parse each file, STDIN or specified string in options yielding a reader.
Class Method Details
.abort(msg) ⇒ void
This method returns an undefined value.
196 197 198 |
# File 'lib/rdf/cli.rb', line 196 def self.abort(msg) Kernel.abort "#{basename}: #{msg}" end |
.basename ⇒ String
75 |
# File 'lib/rdf/cli.rb', line 75 def self.basename() File.basename($0) end |
.commands ⇒ Array<String>
Returns list of executable commands.
164 165 166 |
# File 'lib/rdf/cli.rb', line 164 def self.commands COMMANDS.keys end |
.exec_command(command, args, options = {}) ⇒ Boolean
154 155 156 157 158 159 160 |
# File 'lib/rdf/cli.rb', line 154 def self.exec_command(command, args, = {}) unless COMMANDS.has_key?(command) abort "#{File.basename($0)}: unknown command `#{command}'" end COMMANDS[command].call(args, ) end |
.options {|options| ... } ⇒ OptionParser
81 82 83 84 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 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 |
# File 'lib/rdf/cli.rb', line 81 def self.(&block) = OptionParser.new opts = . = { :base_uri => nil, :canonicalize => false, :debug => false, :evaluate => nil, :format => nil, :output => $stdout, :output_format => :ntriples, :validate => false, } # Command-specific options if block_given? case block.arity when 1 then block.call() else .instance_eval(&block) end end . ||= "Usage: #{self.basename} [options] command [args...]" .on('--canonicalize', 'Canonicalize input.') do opts[:canonicalize] = true end .on('-d', '--debug', 'Enable debug output for troubleshooting.') do opts[:debug] = $DEBUG = true end .on("-e", "--evaluate STRING", "Evaluate argument as RDF input, if no files are specified") do |arg| opts[:evaluate] = arg end .on("--input-format FORMAT", "Format of input file, uses heuristic if not specified") do |arg| opts[:format] = arg.downcase.to_sym end .on("-o", "--output FILE", "File to write output, defaults to STDOUT") do |arg| opts[:output] = File.open(arg, "w") end .on("--output-format FORMAT", "Format of output file, defaults to NTriples") do |arg| opts[:output_format] = arg.downcase.to_sym end .on('--uri URI', 'Base URI of input file, defaults to the filename.') do |arg| opts[:base_uri] = arg end .on('--validate', 'Validate input file.') do opts[:validate] = true end .on_tail("-h", "--help", "Show this message") do $stdout.puts $stdout.puts "Available commands:\n\t#{self.commands.join("\n\t")}" exit end begin .parse! rescue OptionParser::InvalidOption => e abort e end end |
.parse(files, options = {}) {|reader| ... } ⇒ nil
Parse each file, STDIN or specified string in options yielding a reader
176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 |
# File 'lib/rdf/cli.rb', line 176 def self.parse(files, = {}, &block) if files.empty? # If files are empty, either use options[:execute] input = [:evaluate] ? StringIO.new([:evaluate]) : STDIN RDF::Reader.for([:format] || :ntriples).new(input, ) do |reader| yield(reader) end else files.each do |file| RDF::Reader.open(file, ) do |reader| (@readers ||= []) << reader.class.to_s yield(reader) end end end end |