Class: Resedit::AppCommand

Inherits:
Object
  • Object
show all
Defined in:
lib/resedit/app/app_command.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(names, type = :std) ⇒ AppCommand

Returns a new instance of AppCommand.



5
6
7
8
9
10
11
12
13
14
15
16
# File 'lib/resedit/app/app_command.rb', line 5

def initialize(names, type=:std)
    @names, @continue = names, type
    @names = [@names] if not @names.kind_of?(Array)
    @opts = {}
    @params = []
    @ohash = {}
    addOption('verbose','v',false, 'verbose output')
    addOption('unverbose',nil,false, 'unverbose output')
    addOption('quiet','q',false, 'supress output')
    addOption('color',nil,false, 'set colored output')
    addOption('uncolor',nil,false, 'unset colored output')
end

Instance Attribute Details

#namesObject (readonly)

Returns the value of attribute names.



4
5
6
# File 'lib/resedit/app/app_command.rb', line 4

def names
  @names
end

#ohashObject (readonly)

Returns the value of attribute ohash.



4
5
6
# File 'lib/resedit/app/app_command.rb', line 4

def ohash
  @ohash
end

#optsObject (readonly)

Returns the value of attribute opts.



4
5
6
# File 'lib/resedit/app/app_command.rb', line 4

def opts
  @opts
end

#paramsObject (readonly)

Returns the value of attribute params.



4
5
6
# File 'lib/resedit/app/app_command.rb', line 4

def params
  @params
end

#typeObject (readonly)

Returns the value of attribute type.



4
5
6
# File 'lib/resedit/app/app_command.rb', line 4

def type
  @type
end

Instance Method Details

#addOption(longname, shortname, param, descr, type = :std, setter = proc{|val, opt| val}) ⇒ Object



22
23
24
25
# File 'lib/resedit/app/app_command.rb', line 22

def addOption(longname, shortname, param, descr, type = :std, setter= proc{|val, opt| val})
    @opts[longname] = {:name => longname, :param => param, :descr => descr, :type => type, :setter => setter}
    @ohash[shortname] = longname if shortname
end

#addParam(name, descr, default = nil, type = :std) ⇒ Object



18
19
20
# File 'lib/resedit/app/app_command.rb', line 18

def addParam(name, descr, default = nil, type=:std)
    @params +=[{:name => name, :def => default, :descr => descr}]
end

#job(params) ⇒ Object



85
86
87
# File 'lib/resedit/app/app_command.rb', line 85

def job(params)
    raise "Unimplemented command #{@names[0]}"
end

#log(fmt, *args) ⇒ Object



92
93
94
# File 'lib/resedit/app/app_command.rb', line 92

def log(fmt, *args)
    App::get().log(fmt,*args)
end

#logd(fmt, *args) ⇒ Object



89
90
91
# File 'lib/resedit/app/app_command.rb', line 89

def logd(fmt, *args)
    App::get().logd(fmt,*args)
end

#loge(fmt, *args) ⇒ Object



95
96
97
# File 'lib/resedit/app/app_command.rb', line 95

def loge(fmt, *args)
    App::get().loge(fmt,*args)
end

#parseParams(params) ⇒ Object



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
# File 'lib/resedit/app/app_command.rb', line 27

def parseParams(params)
    res = {}
    pidx = 0
    idx=0
    @params.each{|p|
        res[p[:name]] = p[:def]
    }
    @opts.each{|k,o|
        res[k] = o[:param]
    }
    while idx<params.length
        p = params[idx]
        idx+=1
        if p[0]=='-'
            val = true
            s=p.split('=')
            if s.length==2
                val=s[1]
                p=s[0]
            end
            if (p[1]=='-')
                opt = @opts[p[2..-1]]
            else
                opt = @opts[@ohash[p[1..-1]]] || @opts[p[1..-1]]
            end
            raise "Unknown option #{p}" if !opt
            if opt[:param]!=false and val==true
                raise "No option #{p} value" if idx>=params.length
                val = params[idx]
                idx+=1
            end
            proc = opt[:setter]
            res[opt[:name]] = proc.call(val, opt)
        else
            raise "Unknown param #{p}" if !@params[pidx]
            if (@params[pidx][:type] == :text)
                p = params[idx-1..-1]
                idx=params.length
            end
            res[@params[pidx][:name]] = p
            pidx+=1
        end
    end
    @params.each{|pp|
        raise "Expected parameter #{pp[:name]}" if res[pp[:name]]==nil
    }
    return res
end

#run(params) ⇒ Object



76
77
78
79
80
81
82
83
# File 'lib/resedit/app/app_command.rb', line 76

def run(params)
    App::get().logger.level = Logger::DEBUG if params['verbose']
    App::get().logger.level = Logger::INFO if params['unverbose']
    App::get().logger.level = Logger::ERROR if params['quiet']
    App::get().col.on = true if params['color']
    App::get().col.on = false if params['uncolor']
    job(params)
end