Module: ImageFilterDsl::Engine::CLI

Extended by:
CLI
Included in:
CLI
Defined in:
lib/image_filter_dsl/engine/cli.rb

Overview

CLI Argument parser and handler module

Defined Under Namespace

Classes: Options

Constant Summary collapse

RequiredArgs =
{
    process: [:filter, :input, :output],
    compile: [:input, :output]
}

Instance Method Summary collapse

Instance Method Details

#do_action(opts) ⇒ Object



73
74
75
76
77
78
79
80
# File 'lib/image_filter_dsl/engine/cli.rb', line 73

def do_action(opts)
    if opts.action == :process
        ip = ImageFilterDsl.image_processor(opts.filter)
        ip.process_image(opts.input, opts.output)
    elsif opts.action == :compile
        print "(Not yet implemented :)\n"
    end
end

#parse(args) ⇒ Object



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
# File 'lib/image_filter_dsl/engine/cli.rb', line 18

def parse(args)
    opts = Options.new
    args=args.map{|s|s.strip}

    p = args[0]
    
    if !p.nil? && p.downcase == '--process'
        opts.action = :process
    elsif !p.nil? && p.downcase == '--compile'
        opts.action = :compile
    end

    unless opts.action.nil?
        case opts.action
        when :process
            f,i,o = args[1..-1]
            opts.filter = f
            opts.input = i
            opts.output = o
        when :compile
            s,k = args[1..-1]
            opts.input = s
            opts.output = k
        end
    end

    return opts
end

#runObject



47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/image_filter_dsl/engine/cli.rb', line 47

def run()
    options = parse(ARGV)
    if options.action.nil?
        show_usage
    else
        p options
        if RequiredArgs[options.action].select{|o| options[o].nil?}.length == 0
            do_action(options)
        else
            show_usage(options.action)
        end
    end
end

#show_usage(action = nil) ⇒ Object



61
62
63
64
65
66
67
68
69
70
71
# File 'lib/image_filter_dsl/engine/cli.rb', line 61

def show_usage(action=nil)
    if !action.nil?
        print "Invalid arguments for #{action.to_s}\n"
    end
    print [
        "Usage: image_filter_dsl <action<",
        "\t--process <kernel_file> <image in> <image out>\t\tProcess image with binary kernel",
        "\t--compile <source_file> <output_kernel>\t\tCompile given ruby source to binary kernel"
    ].join("\n")
    print "\n"
end