functionutils.errorhandler(err) print("ERROR: ", err) end
functionutils.xpcall(statements) returnxpcall (function()return statements end, utils.errorhandler) end
-- cat file functionutils.catfile(file) local f = assert(io.open(file, "r")) local content = f:read("*all") f:close() return content end
-- @mode: w a r+ w+ a+ b -- @content: strings functionutils.savefile(file, mode, content) local f = assert(io.open(file, mode)) f:write(content) f:close() end
return utils
上面对文件读写函数以及异常处理函数进行了封装,在其它文件中就可以通过 require 调用了。
1 2 3 4 5 6 7
require"utils"
-- cat demo.lua and write into test.txt utils.savefile("test.txt", "w", utils.catfile("demo.lua"))
/usr/bin/ld: /usr/local/lib/liblua.a(lobject.o): infunction `numarith.isra.0': lobject.c:(.text+0x1fb): undefined reference to `fmod' /usr/bin/ld: lobject.c:(.text+0x221): undefined reference to `pow' /usr/bin/ld: /usr/local/lib/liblua.a(lvm.o): in function `luaV_execute': lvm.c:(.text+0x2145): undefined reference to `pow' /usr/bin/ld: lvm.c:(.text+0x24e0): undefined reference to `fmod' /usr/bin/ld: /usr/local/lib/liblua.a(lmathlib.o): infunction `math_log10': lmathlib.c:(.text+0xa3): undefined reference to `log10' /usr/bin/ld: /usr/local/lib/liblua.a(lmathlib.o): infunction `math_pow': lmathlib.c:(.text+0x1b8): undefined reference to `pow' /usr/bin/ld: /usr/local/lib/liblua.a(lmathlib.o): infunction `math_tanh': lmathlib.c:(.text+0x1e3): undefined reference to `tanh' /usr/bin/ld: /usr/local/lib/liblua.a(lmathlib.o): infunction `math_sinh': lmathlib.c:(.text+0x213): undefined reference to `sinh' /usr/bin/ld: /usr/local/lib/liblua.a(lmathlib.o): infunction `math_cosh': lmathlib.c:(.text+0x243): undefined reference to `cosh' /usr/bin/ld: /usr/local/lib/liblua.a(lmathlib.o): infunction `math_tan': lmathlib.c:(.text+0x273): undefined reference to `tan' /usr/bin/ld: /usr/local/lib/liblua.a(lmathlib.o): infunction `math_sqrt': lmathlib.c:(.text+0x2d6): undefined reference to `sqrt' /usr/bin/ld: /usr/local/lib/liblua.a(lmathlib.o): infunction `math_sin': lmathlib.c:(.text+0x303): undefined reference to `sin' /usr/bin/ld: /usr/local/lib/liblua.a(lmathlib.o): infunction `math_log': lmathlib.c:(.text+0x649): undefined reference to `log10' /usr/bin/ld: lmathlib.c:(.text+0x656): undefined reference to `log' /usr/bin/ld: lmathlib.c:(.text+0x678): undefined reference to `log2' /usr/bin/ld: lmathlib.c:(.text+0x68c): undefined reference to `log' /usr/bin/ld: lmathlib.c:(.text+0x6a0): undefined reference to `log' /usr/bin/ld: /usr/local/lib/liblua.a(lmathlib.o): infunction `math_exp': lmathlib.c:(.text+0x723): undefined reference to `exp' /usr/bin/ld: /usr/local/lib/liblua.a(lmathlib.o): infunction `math_cos': lmathlib.c:(.text+0x753): undefined reference to `cos' /usr/bin/ld: /usr/local/lib/liblua.a(lmathlib.o): infunction `math_atan': lmathlib.c:(.text+0x7b0): undefined reference to `atan2' /usr/bin/ld: /usr/local/lib/liblua.a(lmathlib.o): infunction `math_asin': lmathlib.c:(.text+0x7e3): undefined reference to `asin' /usr/bin/ld: /usr/local/lib/liblua.a(lmathlib.o): infunction `math_acos': lmathlib.c:(.text+0x813): undefined reference to `acos' /usr/bin/ld: /usr/local/lib/liblua.a(lmathlib.o): infunction `math_fmod': lmathlib.c:(.text+0xca3): undefined reference to `fmod' collect2: error: ld returned 1 exit status
如果不添加 -ldl, 会提示以下错误。
1 2 3 4 5 6 7 8
/usr/bin/ld: /usr/local/lib/liblua.a(loadlib.o): infunction `lookforfunc': loadlib.c:(.text+0x565): undefined reference to `dlsym' /usr/bin/ld: loadlib.c:(.text+0x5c6): undefined reference to `dlopen' /usr/bin/ld: loadlib.c:(.text+0x649): undefined reference to `dlerror' /usr/bin/ld: loadlib.c:(.text+0x671): undefined reference to `dlerror' /usr/bin/ld: /usr/local/lib/liblua.a(loadlib.o): in function `gctm': loadlib.c:(.text+0x831): undefined reference to `dlclose' collect2: error: ld returned 1 exit status
--- /*@from: https://github.com/skeeto/getopt-lua --- getopt(3)-like functionality for Lua 5.1 and later -- This is free and unencumbered software released into the public domain.
--- getopt(argv, optstring [, nonoptions]) -- -- Returns a closure suitable for "for ... in" loops. On each call the -- closure returns the next (option, optarg). For unknown options, it -- returns ('?', option). When a required optarg is missing, it returns -- (':', option). It is reasonable to continue parsing after errors. -- Returns nil when done. -- -- The optstring follows the same format as POSIX getopt(3). However, -- this function will never print output on its own. -- -- Non-option arguments are accumulated, in order, in the optional -- "nonoptions" table. If a "--" argument is encountered, appends the -- remaining arguments to the nonoptions table and returns nil. -- -- The input argv table is left unmodified.*/
localfunctiongetopt(argv, optstring, nonoptions) local optind = 1 local optpos = 2 nonoptions = nonoptions or {} returnfunction() whiletruedo localarg = argv[optind] ifarg == nilthen returnnil elseifarg == '--'then for i = optind + 1, #argv do table.insert(nonoptions, argv[i]) end returnnil elseifarg:sub(1, 1) == '-'then local opt = arg:sub(optpos, optpos) local start, stop = optstring:find(opt .. ':?') ifnot start then optind = optind + 1 optpos = 2 return'?', opt elseif stop > start and #arg > optpos then local optarg = arg:sub(optpos + 1) optind = optind + 1 optpos = 2 return opt, optarg elseif stop > start then local optarg = argv[optind + 1] optind = optind + 2 optpos = 2 if optarg == nilthen return':', opt end return opt, optarg else optpos = optpos + 1 if optpos > #argthen optind = optind + 1 optpos = 2 end return opt, nil end else optind = optind + 1 table.insert(nonoptions, arg) end end end end
return getopt
--[[ /*Examples: getopt = require('getopt')
local append = false local binary = false local color = 'white' local nonoptions = {} local infile = io.input()
for opt, arg in getopt(arg, 'abc:h', nonoptions) do if opt == 'a' then append = true elseif opt == 'b' then binary = true elseif opt == 'c' then color = arg elseif opt == 'h' then usage() os.exit(0) elseif opt == '?' then print('error: unknown option: ' .. arg) os.exit(1) elseif opt == ':' then print('error: missing argument: ' .. arg) os.exit(1) end end
if #nonoptions == 1 then infile = io.open(nonoptions[1], 'r') elseif #nonoptions > 1 then print('error: wrong number of arguments: ' .. #nonoptions) os.exit(1) end */]]