Skip to content

Commit a68e8ea

Browse files
o-limamireh
authored andcommitted
Fix short-key notation for optional arguments
This fixes short-key notation for optional arguments when they are of the form "-sk VALUE", such that cli:add_opt("-sk VALUE", ...) will not generate an error.
1 parent 39f956e commit a68e8ea

File tree

2 files changed

+11
-3
lines changed

2 files changed

+11
-3
lines changed

spec/cliargs_parsing_spec.lua

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,14 @@ describe("Testing cliargs library parsing commandlines", function()
120120
assert.are.same(result, defaults)
121121
end)
122122

123+
it("tests option using -short-key value notation", function()
124+
_G.arg = { "-out", "outfile" }
125+
cli:add_opt("-out VALUE", "output file")
126+
defaults = { out = "outfile" }
127+
result = cli:parse()
128+
assert.are.same(result, defaults)
129+
end)
130+
123131
it("tests optional using --expanded-key notation, --x=VALUE", function()
124132
local args = { "--compress=lzma" }
125133
defaults = populate_optionals(cli)

src/cliargs.lua

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -77,11 +77,11 @@ local function disect(key)
7777
local k, ek, v
7878
local dummy
7979
-- if there is no comma, between short and extended, add one
80-
_, _, dummy = key:find("^%-([%a%d])[%s]%-%-")
80+
_, _, dummy = key:find("^%-([%a%d]+)[%s]%-%-")
8181
if dummy then key = key:gsub("^%-[%a%d][%s]%-%-", "-"..dummy..", --", 1) end
8282
-- for a short key + value, replace space by "="
83-
_, _, dummy = key:find("^%-([%a%d])[%s]")
84-
if dummy then key = key:gsub("^%-([%a%d])[ ]", "-"..dummy.."=", 1) end
83+
_, _, dummy = key:find("^%-([%a%d]+)[%s]")
84+
if dummy then key = key:gsub("^%-([%a%d]+)[ ]", "-"..dummy.."=", 1) end
8585
-- if there is no "=", then append one
8686
if not key:find("=") then key = key .. "=" end
8787
-- get value

0 commit comments

Comments
 (0)