Skip to content

Commit

Permalink
Merge branch 'master' of github.com:amireh/lua_cliargs
Browse files Browse the repository at this point in the history
  • Loading branch information
Tieske committed Nov 26, 2012
2 parents fecf181 + 24b5627 commit 6080fe9
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 29 deletions.
4 changes: 2 additions & 2 deletions lua_cliargs-2.0-1.rockspec → lua_cliargs-2.1-1.rockspec
@@ -1,7 +1,7 @@
package = "lua_cliargs"
version = "2.0-1"
version = "2.1-1"
source = {
url = "https://github.com/downloads/amireh/lua_cliargs/lua_cliargs-2.0.tar.gz"
url = "https://github.com/downloads/amireh/lua_cliargs/lua_cliargs-2.1.tar.gz"
}
description = {
summary = "A command-line argument parser.",
Expand Down
54 changes: 27 additions & 27 deletions src/cliargs.lua
Expand Up @@ -73,7 +73,7 @@ end

local function disect(key)
-- characters allowed are a-z, A-Z, 0-9
-- extended + values also allow; # @ _ + -
-- extended + values also allow; # @ _ + -
local k, ek, v
local dummy
-- if there is no comma, between short and extended, add one
Expand Down Expand Up @@ -146,7 +146,7 @@ end
--- `cli:add_arg("root", "path to where root scripts can be found")`
function cli:add_arg(key, desc)
assert(type(key) == "string" and type(desc) == "string", "Key and description are mandatory arguments (Strings)")

if self:__lookup(key, nil, self.required) then
error("Duplicate argument: " .. key .. ", please rename one of them.")
end
Expand Down Expand Up @@ -214,7 +214,7 @@ function cli:add_opt(key, desc, default)
assert(type(default) == "string" or default == nil or default == false, "Default argument: expected a string or nil")

local k, ek, v = disect(key)

if default == false and v ~= nil then
error("A flag type option cannot have a value set; " .. key)
end
Expand All @@ -227,7 +227,7 @@ function cli:add_opt(key, desc, default)
-- set defaults
if v == nil then default = false end -- no value, so its a flag
if default == nil then default = "" end

-- below description of full entry record, nils included for reference
local entry = {
key = k,
Expand All @@ -241,7 +241,7 @@ function cli:add_opt(key, desc, default)

table.insert(self.optional, entry)
if #key > self.maxlabel then self.maxlabel = #key end

end

--- Define a flag argument (on/off). This is a convenience helper for cli.add_opt().
Expand All @@ -260,7 +260,7 @@ end
---
--- ### Parameters
--- 1. **noprint**: set this flag to prevent any information (error or help info) from being printed
--- 1. **dump**: set this flag to dump the parsed variables for debugging purposes, alternatively
--- 1. **dump**: set this flag to dump the parsed variables for debugging purposes, alternatively
--- set the first option to --__DEBUG__ (option with 2 trailing and leading underscores) to dump at runtime.
---
--- ### Returns
Expand All @@ -270,20 +270,20 @@ function cli:parse(noprint, dump)
arg = arg or {}
local args = {}
for k,v in pairs(arg) do args[k] = v end -- copy global args local

-- starts with --help? display the help listing and abort!
if args[1] and (args[1] == "--help" or args[1] == "-h") then
return nil, self:print_help(noprint)
end

-- starts with --__DUMP__; set dump to true to dump the parsed arguments
if dump == nil then
if dump == nil then
if args[1] and args[1] == "--__DUMP__" then
dump = true
table.remove(args, 1) -- delete it to prevent further parsing
end
end

while args[1] do
local entry = nil
local opt = args[1]
Expand All @@ -299,14 +299,14 @@ function cli:parse(noprint, dump)
if not optpref then
break -- no optional prefix, so options are done
end

if optkey:sub(-1,-1) == "=" then -- check on a blank value eg. --insert=
optval = ""
optkey = optkey:sub(1,-2)
end

if optkey then
entry =
entry =
self:__lookup(optpref == '-' and optkey or nil,
optpref == '--' and optkey or nil)
end
Expand Down Expand Up @@ -347,7 +347,7 @@ function cli:parse(noprint, dump)
return cli_error("unknown/bad flag; " .. opt, noprint)
end
end

entry.value = optval
end

Expand Down Expand Up @@ -383,10 +383,10 @@ function cli:parse(noprint, dump)
self.optargument.value = { self.optargument.default }
end
end

-- populate the results table
local results = {}
if self.optargument.maxcount > 0 then
if self.optargument.maxcount > 0 then
results[self.optargument.key] = self.optargument.value
end
for _, entry in pairs(self.required) do
Expand All @@ -403,14 +403,14 @@ function cli:parse(noprint, dump)
for i,v in ipairs(arg) do -- use gloabl 'arg' not the modified local 'args'
print(string.format("%3i = '%s'", i, v))
end

print("\n======= Parsed command line ===============")
if #self.required > 0 then print("\nArguments:") end
for i,v in ipairs(self.required) do
print(" " .. v.key .. string.rep(" ", self.maxlabel + 2 - #v.key) .. " => '" .. v.value .. "'")
end
if self.optargument.maxcount > 0 then

if self.optargument.maxcount > 0 then
print("\nOptional arguments:")
print(" " .. self.optargument.key .. "; allowed are " .. tostring(self.optargument.maxcount) .. " arguments")
if self.optargument.maxcount == 1 then
Expand All @@ -423,10 +423,10 @@ function cli:parse(noprint, dump)
end
end
end

if #self.optional > 0 then print("\nOptional parameters:") end
local doubles = {}
for _, v in pairs(self.optional) do
for _, v in pairs(self.optional) do
if not doubles[v] then
local m = v.value
if type(m) == "string" then
Expand All @@ -441,7 +441,7 @@ function cli:parse(noprint, dump)
print("\n===========================================\n\n")
return cli_error("commandline dump created as requested per '--__DUMP__' option", noprint)
end

if not _TEST then
-- cleanup entire module, as it's single use
-- remove from package.loaded table to enable the module to
Expand All @@ -457,7 +457,7 @@ function cli:parse(noprint, dump)
cli[k] = nil
end
end

return results
end

Expand Down Expand Up @@ -508,22 +508,22 @@ function cli:print_help(noprint)
col1 = col1 + 3 --add margins
if col2 == 0 then col2 = 72 - col1 end
if col2 <10 then col2 = 10 end

local append = function(label, desc)
label = " " .. label .. string.rep(" ", col1 - (#label + 2))
desc = wordwrap(desc, col2) -- word-wrap
desc = desc:gsub("\n", "\n" .. string.rep(" ", col1)) -- add padding

msg = msg .. label .. desc .. "\n"
end

if self.required[1] then
msg = msg .. "\nARGUMENTS: \n"
for _,entry in ipairs(self.required) do
append(entry.key, entry.desc .. " (required)")
end
end

if self.optargument.maxcount >0 then
append(self.optargument.key, self.optargument.desc .. " (optional, default: " .. self.optargument.default .. ")")
end
Expand All @@ -533,7 +533,7 @@ function cli:print_help(noprint)

for _,entry in ipairs(self.optional) do
local desc = entry.desc
if not entry.flag and entry.default then
if not entry.flag and entry.default and #tostring(entry.default) > 0 then
desc = desc .. " (default: " .. entry.default .. ")"
end
append(entry.label, desc)
Expand Down
Binary file added tarballs/lua_cliargs-2.1.tar.gz
Binary file not shown.

0 comments on commit 6080fe9

Please sign in to comment.