From 0fedcdeadeb4cc21824aac40bba3e41c70f5473d Mon Sep 17 00:00:00 2001 From: Kyle McLamb Date: Wed, 17 Jun 2015 03:41:36 -0400 Subject: [PATCH] Add handle_options property to Parser When `parser:handle_options(true)` (the default), the parser will behave as before. When `parser:handle_options(false)`, all options will be passed verbatim to the argument list, as if the input included double-hyphens. --- spec/options_spec.lua | 11 +++++++++++ src/argparse.lua | 8 ++++++-- 2 files changed, 17 insertions(+), 2 deletions(-) diff --git a/spec/options_spec.lua b/spec/options_spec.lua index 7233f55..9e0aed7 100644 --- a/spec/options_spec.lua +++ b/spec/options_spec.lua @@ -125,6 +125,17 @@ describe("tests related to options", function() assert.same({input = "foo", exclude = {}}, args) end) + it("does not interpret options if disabled", function() + local parser = Parser() + parser:handle_options(false) + parser:argument "input" + :args "*" + parser:option "-f" "--foo" + :args "*" + local args = parser:parse{"bar", "-f", "--foo" , "bar"} + assert.same({input = {"bar", "-f", "--foo" , "bar"}}, args) + end) + describe("Special chars set", function() it("handles windows-style options", function() local parser = Parser() diff --git a/src/argparse.lua b/src/argparse.lua index 225550a..5b109ed 100644 --- a/src/argparse.lua +++ b/src/argparse.lua @@ -183,7 +183,8 @@ local Parser = new_class({ _options = {}, _commands = {}, _mutexes = {}, - _require_command = true + _require_command = true, + _handle_options = true }, { typechecked("name", "string"), typechecked("description", "string"), @@ -191,6 +192,7 @@ local Parser = new_class({ typechecked("usage", "string"), typechecked("help", "string"), typechecked("require_command", "boolean"), + typechecked("handle_options", "boolean"), add_help }) @@ -204,6 +206,7 @@ local Command = new_class({ typechecked("usage", "string"), typechecked("help", "string"), typechecked("require_command", "boolean"), + typechecked("handle_options", "boolean"), typechecked("action", "function"), add_help }, Parser) @@ -673,6 +676,7 @@ function Parser:_parse(args, errhandler) local cur_arg_i = 1 local cur_arg local targets = {} + local handle_options = true local function error_(fmt, ...) return errhandler(parser, fmt:format(...)) @@ -824,6 +828,7 @@ function Parser:_parse(args, errhandler) invoke(argument) end + handle_options = parser._handle_options cur_arg = arguments[cur_arg_i] commands = parser._commands com_context = {} @@ -891,7 +896,6 @@ function Parser:_parse(args, errhandler) end local function mainloop() - local handle_options = true for _, data in ipairs(args) do local plain = true