Skip to content

Commit

Permalink
optparse: don't lookup non-option arguments in method table.
Browse files Browse the repository at this point in the history
Fixes #3.
* lib/optparse/init.lua (parse): Any argument that does not begin
with a '-' is now an unrecognized option, and does not crash the
parser.
* specs/optparse_spec.yaml(parse): New examples to prevent future
regressions.
* NEWS.md (Bug fixes): Update.

Signed-off-by: Gary V. Vaughan <[email protected]>
  • Loading branch information
gvvaughan committed Jun 3, 2017
1 parent c08ab0f commit b3549c6
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 1 deletion.
8 changes: 8 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,13 @@
# optparse NEWS - User visible changes

## Noteworthy changes in release ?.?.? (????-??-??) [?]

### Bug fixes

- don't crash when first unrecognized argument is also a handler
name (boolean, file, finished, flag, etc...)


## Noteworthy changes in release 1.1.1 (2016-02-07) [stable]

### Bug fixes
Expand Down
2 changes: 1 addition & 1 deletion lib/optparse/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -590,7 +590,7 @@ local function parse (self, arglist, defaults)
while i > 0 and i <= len (arglist) do
local opt = arglist[i]

if self[opt] == nil then
if self[opt] == nil or opt:match "^[^%-]" then
table_insert (self.unrecognised, opt)
i = i + 1

Expand Down
8 changes: 8 additions & 0 deletions specs/optparse_spec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ specify optparse:
--long a long option with no short option
--another-long a long option with internal hypen
--true a Lua keyword as an option name
--boolean an optparse handler as an option name
-v, --verbose a combined short and long option
-n, --dryrun, --dry-run several spellings of the same option
-u, --name=USER require an argument
Expand Down Expand Up @@ -103,6 +104,13 @@ specify optparse:
]]
parse = bind (luaproc, {code})
- it collects non-option arguments:
expect (parse {"foo"}).to_output "args = { foo }\n"
- it collects non-options arguments matching argparse handler names:
expect (parse {"file"}).to_output "args = { file }\n"
- it collects arguments matching argparse handler names:
expect (parse {"--boolean"}).to_output "opts = { boolean = true }\n"

- it responds to --version with version text:
expect (parse {"--version"}).
to_match_output "^%s*parseme .*Copyright .*NO WARRANTY%.\n$"
Expand Down

0 comments on commit b3549c6

Please sign in to comment.