summaryrefslogtreecommitdiff
path: root/src/bin/elua/apps/lualian.lua
blob: 4ffbf58ee124ec57f8ba4ebd2ed28598450448a7 (plain) (blame)
1
2
3
4
5
6
7
8
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
-- Lualian application
-- for use with Elua

local lualian = require("lualian")
local  getopt = require("getopt")

local gen_file = function(opts, i, fname)
    local printv  = opts["v"] and print or function() end
    printv("Generating for file: " .. fname)
    local ofile   = opts["o"] and opts["o"][i] or nil
    local fstream = io.stdout
    if ofile then
        printv("  Output file: " .. ofile)
        fstream = io.open(ofile, "w")
        if not fstream then
            error("Cannot open output file: " .. ofile)
        end
    else
        printv("  Output file: printing to stdout...")
    end
    lualian.generate(fname, fstream)
end

getopt.parse {
    usage = "Usage: %prog [OPTIONS] file1.eo file2.eo ... fileN.eo",
    args  = arg,
    descs = {
        { category = "General" },

        { "h", "help", nil, help = "Show this message.", metavar = "CATEGORY",
            callback = getopt.help_cb(io.stdout)
        },
        { "v", "verbose", false, help = "Be verbose." },

        { category = "Generator" },

        { "I", "include", true, help = "Include a directory.", metavar = "DIR",
            list = {}
        },
        { "o", "output", true, help = "Specify output file name(s), by "
            .. "default goes to stdout.",
            list = {}
        }
    },
    error_cb = function(parser, msg)
        io.stderr:write(msg, "\n")
        getopt.help(parser, io.stderr)
    end,
    done_cb = function(parser, opts, args)
        if not opts["h"] then
            for i, v in ipairs(opts["I"] or {}) do
                lualian.include_dir(v)
            end
            if os.getenv("EFL_RUN_IN_TREE") then
                lualian.system_directory_scan()
            end
            lualian.load_eot_files()
            for i, fname in ipairs(args) do
                gen_file(opts, i, fname)
            end
        end
    end
}

return true