category = "General Options"
e2option.flag("help", "show manpage",
function()
- e2option.showtoolmanpage()
+ local rc, re = e2option.showtoolmanpage()
+ if not rc then
+ e2lib.abort(re)
+ end
+
+ e2lib.finish(0)
end,
category)
local function userdefaultoptions(opts)
local home = e2lib.globals.homedir
if not home then
- return
+ return true
end
local file = home .. "/.e2/e2rc"
if not e2util.exists(file) then
- return
+ return true
end
local e2rc = {}
local rc, e = e2lib.dofile_protected(file, { e2rc = function(t) e2rc = t end })
if not rc then
- e2lib.abort(e)
+ return false, e
end
for _,tbl in pairs(e2rc) do
if type(tbl) ~= "table" then
- e2lib.abort(string.format("could not parse user defaults.\n"..
- "'%s' is not in the expected format.", file))
+ return false, err.new("could not parse user defaults.\n"..
+ "'%s' is not in the expected format.", file)
end
local opt=tbl[1]
local val=tbl[2]
if type(opt) ~= "string" or string.len(opt) == 0 then
- e2lib.abort(string.format("could not parse user defaults.\n"..
- "'%s' has a malformed option", file))
+ return false, err.new("could not parse user defaults.\n"..
+ "'%s' has a malformed option", file)
end
opt = aliases[opt] or opt
if not options[opt] then
- e2lib.abort("unknown option in user defaults: " .. opt)
+ return false, err.new("unknown option in user defaults: %s", opt)
end
if options[opt].type == "flag" and val then
- e2lib.abort(string.format(
- "user default option '%s' does not take an argument ",
- opt))
+ return false, err.new(
+ "user default option '%s' does not take an argument ", opt)
elseif options[opt].type == "option" and not val then
- e2lib.abort(
- "argument missing for user default option: " .. opt)
+ return false,
+ err.new("argument missing for user default option: %s", opt)
end
if options[opt].proc then
e2lib.bomb("user default option has no effect")
end
end
+
+ return true
end
--- fill in defaults, parse user defauls and parse normal options
-- @param args table: command line arguments (usually the arg global variable)
--- @return table: option_table
+-- @return table: option_table or false on error.
-- @return table of unparsed arguments (everything not identified as an option)
+-- or an error object on failure.
function e2option.parse(args)
defaultoptions()
local opts = {}
local vals = {}
+ local rc, re
- userdefaultoptions(opts)
+ rc, re = userdefaultoptions(opts)
+ if not rc then
+ return false, re
+ end
local i = 1
while i <= #args do -- we may modify args
opt = aliases[opt] or opt
if options[opt] then
if options[opt].type == "flag" then
- e2lib.abort(string.format(
- "option '%s' does not take an argument\n"..
- "Try the --help option for usage information.", opt))
+ return false, err.new(
+ "option '%s' does not take an argument\n"..
+ "Try the --help option for usage information.", opt)
end
local proc = options[opt].proc
opts[opt] = val
else
- e2lib.abort(string.format("unknown option: %s\n"..
- "Try the --help option for usage information.", opt))
+ return false, err.new("unknown option: %s\n"..
+ "Try the --help option for usage information.", opt)
end
else
s, e, opt = string.find(v, "^%-%-?(.*)$")
local proc = options[opt].proc
if options[opt].type == "option" then
if i == #args then
- e2lib.abort("argument missing for option: " .. opt)
+ return false,
+ err.new("argument missing for option: %s", opt)
end
if proc then
opts[opt] = proc(args[i + 1])
for k, v in pairs(set) do
if not options[v] then
- e2lib.abort(string.format("unknown option: %s\n"..
- "Try the --help option for usage information.", opt))
+ return false, err.new("unknown option: %s\n"..
+ "Try the --help option for usage information.",
+ opt)
else
table.insert(args, "-" .. v)
end
if e2lib.isfile(file) then
prefix = e2lib.join(buildconfig.PREFIX, 'share')
else
- e2lib.abort(err.new('tool "%s" does not exist', tool))
+ return false, err.new('tool "%s" does not exist', tool)
end
end
mpage = e2lib.join(prefix, mpage)
if not e2lib.isfile(mpage) then
- e2lib.abort(err.new('manual page for "%s" does not exist (%s)',
- tool, mpage))
+ return false, err.new('manual page for "%s" does not exist (%s)',
+ tool, mpage)
end
if not tools.isinitialized() then
local rc, re = tools.init()
if not rc then
- e2lib.abort(re)
+ return false, re
end
end
end
if #cmd < 1 then
- e2lib.abort("Could not find manual viewer to display help")
+ return false, err.new("Could not find manual viewer to display help")
end
table.insert(cmd, e2lib.shquote(mpage))
os.execute(table.concat(cmd, ' '))
- e2lib.finish(0)
+ return true
end
return strict.lock(e2option)