Class: DirCatBuild

Inherits:
Object
  • Object
show all
Defined in:
lib/dircat/cli/dircat_build.rb

Overview

Build a catalogue starting from a directory

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.runObject



12
13
14
# File 'lib/dircat/cli/dircat_build.rb', line 12

def self.run
  self.new.parse_args( ARGV )
end

Instance Method Details

#parse_args(argv) ⇒ Object



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
# File 'lib/dircat/cli/dircat_build.rb', line 16

def parse_args( argv )
  options = { :verbose => true, :force => false }
  opts = OptionParser.new
  opts.banner << "Usage: dircat_build [options]\n"
  opts.banner << "\n"
  opts.banner << "Build a catalogue starting from a directory\n";
  opts.banner << "\n"

  opts.on("-h", "--help", "Print this message") do
    puts opts
    return
  end

  opts.on("-v", "--[no-]verbose", "Run verbosely") do |v|
    options[:verbose] = v
  end

  opts.on("-q", "--quiet", "quiet mode as --no-verbose") do |v|
    options[:verbose] = false
  end

  opts.on("-f", "--force", "force write on existent file") do |v|
    options[:force] = true
  end

  opts.on("-o [FILE]", "--output [FILE]",String) do |v|
    options[:output] = v
  end

  rest = opts.parse(argv)

  # p options
  # p ARGV

  if rest.length < 1
    puts "inserire il nome della directory di cui creare il catalogo"
    puts "-h to print help"
    return
  end

  dirname = rest[0]
  dirname = File.expand_path( dirname )
  if not FileTest.directory?(dirname)
    puts "directory "#{dirname} not exists or is not a directory"
    return 
  end

  #
  # option verbose
  #

  if options.has_key?(:verbose)
    if options[:verbose]
      $VERBOSE_LEVEL = 1
    end
  end

  #
  # option: output, force
  #
  output = $stdout
  if options.has_key?(:output)
    if options[:output]
      filename = options[:output]
    else
      filename = "cat_" + File.basename( dirname ) + "_" + Date.today.strftime("%Y%m%d") + ".yaml"
    end
    if File.exist?(filename) and not options[:force]
      puts "File #{filename} exists use --force or -f to overwrite"
      return
    end
    output = File.open(filename, "w")
  end

  start_datetime = DateTime.now
  s = DirCat.loadfromdir(dirname)
  end_datetime = DateTime.now

  # s.pr
  # f.puts s.to_yaml
  s.savetofile( output )

  if output != $stdout
    output.close
  end
  $stderr.puts s.report
  $stderr.puts "tempo: #{end_datetime - start_datetime}"
end