Class: CliTree

Inherits:
Object
  • Object
show all
Defined in:
lib/treevisitor/cli/cli_tree.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.runObject



15
16
17
# File 'lib/treevisitor/cli/cli_tree.rb', line 15

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

Instance Method Details

#parse_args(argv) ⇒ Object



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
# File 'lib/treevisitor/cli/cli_tree.rb', line 19

def parse_args( argv )

  options = { :verbose => true, :force => false, :algo => 'build' }

  opts = OptionParser.new
  opts.banner = "Usage: tree.rb [options] [directory]"
  
  opts.separator ""
  opts.separator "list contents of directories in a tree-like format"
  opts.separator "this is a clone of tree unix command written in ruby"
  
  opts.separator ""
  opts.separator "options: "
  
  opts.on("-h", "--help", "Show this message") do
    puts opts
    return 0
  end

  opts.on("--version", "Show the version") do
    puts "tree.rb version #{TreeVisitor::VERSION}"
    return 0
  end

  opts.on("-a", "All file are listed") do |v|
    options[:all_files] = true
  end

  opts.on("-d", "List directories only") do |v|
    options[:only_directories] = true
  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
  
  algos = %w[build visit]
  algo_aliases = { "b" => "build", "v" => "visit" }

  algo_list = (algo_aliases.keys + algos).join(',')
  opts.on("--algo ALGO", algos, algo_aliases, "Select algo","  (#{algo_list})") do |algo|
    options[:algo] = algo
  end

  rest = opts.parse(argv)

  # p options

  # p ARGV


  if rest.length < 1
    puts opts
    return 1
  end

  dirname = rest[0]
  dirname = File.expand_path( dirname )

  # puts "reading : #{dirname}"


  dtw = DirTreeWalker.new( dirname )
  unless options[:all_files]
    # TODO: la regex e' corretta ed un file che inizia con ".."??

    dtw.add_ignore_pattern(/^\.[^.]+/) # ignore all file starting with "."

  end

  dtw.visit_leaf = !options[:only_directories]

  case options[:algo]
  when 'build'
    visitor = BuildDirTreeVisitor.new
    dtw.run( visitor )
    puts visitor.root.to_str
    puts 
    puts "#{visitor.nr_directories} directories, #{visitor.nr_files} files"
  when 'visit'  
    visitor = PrintDirTreeVisitor.new
    dtw.run( visitor )
  end
  return 0
end