Class: CodeStock::CLI_Cdstk

Inherits:
Object
  • Object
show all
Defined in:
lib/cdstk/cli_cdstk.rb

Class Method Summary collapse

Class Method Details

.execute(stdout, arguments = []) ⇒ Object



9
10
11
12
13
14
15
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
# File 'lib/cdstk/cli_cdstk.rb', line 9

def self.execute(stdout, arguments=[])
  opt = OptionParser.new <<EOF
#{File.basename($0)} COMMAND [ARGS]

The most commonly used #{File.basename($0)} are:
  init        Init db.
  update      Update db.
  add         Add contents. (ex. ~/Documents/cdstock, git://github.com/ongaeshi/cdstock.git)
  remove      Remove contents.
  list        List all contents. 
  rebuild     Rebuild db.
  dump        Dump database contents.
EOF

  subopt = Hash.new
  
  init_default = false
  subopt['init'] = OptionParser.new("#{File.basename($0)} init")
  subopt['init'].on('--default', 'Init db default path. (Maybe ~/.codestock)') { init_default = true }
  
  subopt['update'] = OptionParser.new("#{File.basename($0)} update")
  subopt['add'] = OptionParser.new("#{File.basename($0)} add content1 [content2 ...]")
  subopt['remove'] = OptionParser.new("#{File.basename($0)} remove content1 [content2 ...]")
  subopt['list'] = OptionParser.new("#{File.basename($0)} list")
  subopt['rebuild'] = OptionParser.new("#{File.basename($0)} rebuild")
  subopt['dump'] = OptionParser.new("#{File.basename($0)} dump")

  opt.order!(arguments)
  subcommand = arguments.shift

  if (subopt[subcommand])
    subopt[subcommand].parse!(arguments) unless arguments.empty?

    db_dir = select_dbdir(subcommand, init_default)
    obj = Cdstk.new(stdout, db_dir)

    case subcommand
    when 'init'
      FileUtils.mkdir_p db_dir if (init_default)
      obj.init 
    when 'update'
      obj.update
    when 'add'
      obj.add *arguments
    when 'remove'
      obj.remove *arguments
    when 'list'
      obj.list
    when 'rebuild'
      obj.rebuild
    when 'dump'
      obj.dump
    end
  else
    if subcommand
      $stderr.puts "#{File.basename($0)}: '#{subcommand}' is not a #{File.basename($0)} command. See '#{File.basename($0)} --help'"
    else
      stdout.puts opt.help
    end
  end
end