Redesign argument storing

* Use state objects instead of tons of locals in the main
  function.
* Use actions for storing arguments into result table.
  Actions are now called at the end of each invocation,
  with result table, target index, arguments and overwrite flag as
  arguments.
* Remove command actions.
* Improve error messages, refer to options by the last used alias
  instead of the main name.

TODO:

* Improve error messages further ("argument 'foo' is required"
  -> "missing argument 'foo'", etc.).
* Add actions for positional arguments.
* Add actions for commands (should be called with final results
  after parsing is over, in "innermost first" order).
* Allow referring to built-in actions by strings a-la Python
  (e.g. action = "store_false").
* Allow setting initial value to be stored at target index
  for each option (perhaps use default value for that).
* Add more tests, particularly for actions.
This commit is contained in:
mpeterv
2015-10-29 14:20:58 +03:00
parent 98114c6976
commit 247c8a9cce
7 changed files with 426 additions and 388 deletions

View File

@@ -97,7 +97,7 @@ describe("tests related to positional arguments", function()
local parser = Parser()
parser:argument "foo"
assert.has_error(function() parser:parse{} end, "too few arguments")
assert.has_error(function() parser:parse{} end, "argument 'foo' is required")
end)
it("handles extra arguments with several arguments correctly", function()
@@ -113,7 +113,7 @@ describe("tests related to positional arguments", function()
parser:argument "foo1"
parser:argument "foo2"
assert.has_error(function() parser:parse{"bar"} end, "too few arguments")
assert.has_error(function() parser:parse{"bar"} end, "argument 'foo2' is required")
end)
it("handles too few arguments with multi-argument correctly", function()
@@ -121,7 +121,7 @@ describe("tests related to positional arguments", function()
parser:argument "foo" {
args = "+"
}
assert.has_error(function() parser:parse{} end, "too few arguments")
assert.has_error(function() parser:parse{} end, "argument 'foo' is required")
end)
it("handles too many arguments with multi-argument correctly", function()
@@ -137,7 +137,7 @@ describe("tests related to positional arguments", function()
parser:argument "foo" {
args = "2-4"
}
assert.has_error(function() parser:parse{"foo"} end, "too few arguments")
assert.has_error(function() parser:parse{"foo"} end, "argument 'foo' requires at least 2 arguments")
end)
it("handles too many arguments with several multi-arguments correctly", function()
@@ -159,7 +159,7 @@ describe("tests related to positional arguments", function()
parser:argument "foo2" {
args = "*"
}
assert.has_error(function() parser:parse{} end, "too few arguments")
assert.has_error(function() parser:parse{} end, "argument 'foo1' is required")
end)
end)
end)