# File lib/facter/application.rb, line 3
 3:     def self.run(argv)
 4:       require 'optparse'
 5:       require 'facter'
 6: 
 7:       options = parse(argv)
 8: 
 9:       # Accept fact names to return from the command line
10:       names = argv
11: 
12:       # Create the facts hash that is printed to standard out.
13:       unless names.empty?
14:         facts = {}
15:         names.each do |name|
16:           begin
17:             facts[name] = Facter.value(name)
18:           rescue => error
19:             $stderr.puts "Could not retrieve #{name}: #{error}"
20:             exit 10
21:           end
22:         end
23:       end
24: 
25:       # Print everything if they didn't ask for specific facts.
26:       facts ||= Facter.to_hash
27: 
28:       # Print the facts as YAML and exit
29:       if options[:yaml]
30:         require 'yaml'
31:         puts YAML.dump(facts)
32:         exit(0)
33:       end
34: 
35:       # Print the facts as JSON and exit
36:       if options[:json]
37:         begin
38:           require 'rubygems'
39:           require 'json'
40:           puts JSON.dump(facts)
41:           exit(0)
42:         rescue LoadError
43:           $stderr.puts "You do not have JSON support in your version of Ruby. JSON output disabled"
44:           exit(1)
45:         end
46:       end
47: 
48:       # Print the value of a single fact, otherwise print a list sorted by fact
49:       # name and separated by "=>"
50:       if facts.length == 1
51:         if value = facts.values.first
52:           puts value
53:         end
54:       else
55:         facts.sort_by{ |fact| fact.first }.each do |name,value|
56:           puts "#{name} => #{value}"
57:         end
58:       end
59: 
60:     rescue => e
61:       if options && options[:trace]
62:         raise e
63:       else
64:         $stderr.puts "Error: #{e}"
65:         exit(12)
66:       end
67:     end