From f787afbe034dba2f14a0da57c9c738422aa07d05 Mon Sep 17 00:00:00 2001 From: "Gary V. Vaughan" Date: Sat, 3 Jun 2017 17:12:33 -0700 Subject: [PATCH] optparse: fix parser hang with split line option and description * specs/optparse_spec.yaml (optparse): Add a split line option description to example help text. * lib/optparse/init.lua (_init): Add a trailing space to each option line to be matched as a separator with option patterns using trailing %s. (OptionParser): Add an example to luadocs. * NEWS.md (Bug fixes): Update. Reported-by: Jakub Jirutka Signed-off-by: Gary V. Vaughan --- NEWS.md | 3 +++ lib/optparse/init.lua | 5 ++++- specs/optparse_spec.yaml | 2 ++ 3 files changed, 9 insertions(+), 1 deletion(-) diff --git a/NEWS.md b/NEWS.md index 385745d..72241f2 100644 --- a/NEWS.md +++ b/NEWS.md @@ -7,6 +7,9 @@ - don't crash when first unrecognized argument is also a handler name (boolean, file, finished, flag, etc...) + - don't hang when help text option table formats long option name + on its own line before indented description. + ## Noteworthy changes in release 1.1.1 (2016-02-07) [stable] diff --git a/lib/optparse/init.lua b/lib/optparse/init.lua index b7bab03..bcbb89b 100644 --- a/lib/optparse/init.lua +++ b/lib/optparse/init.lua @@ -648,7 +648,8 @@ local function _init (self, spec) -- Register option handlers according to the help text. for _, spec in ipairs (specs) do - local options, handler = {} + -- append a trailing space separator to match %s in patterns below + local options, spec, handler = {}, spec .. ' ' -- Loop around each '-' prefixed option on this line. while spec:sub (1, 1) == "-" do @@ -776,6 +777,8 @@ return setmetatable ({ -- -b a short option with no long option -- --long a long option with no short option -- --another-long a long option with internal hypen + -- --really-long-option-name + -- with description on following line -- -v, --verbose a combined short and long option -- -n, --dryrun, --dry-run several spellings of the same option -- -u, --name=USER require an argument diff --git a/specs/optparse_spec.yaml b/specs/optparse_spec.yaml index d48798e..da14219 100644 --- a/specs/optparse_spec.yaml +++ b/specs/optparse_spec.yaml @@ -32,6 +32,8 @@ specify optparse: --another-long a long option with internal hypen --true a Lua keyword as an option name --boolean an optparse handler as an option name + --really-long-option-name + with description on following line -v, --verbose a combined short and long option -n, --dryrun, --dry-run several spellings of the same option -u, --name=USER require an argument