From ddcf1834f67d9b782b67d6638d4530d2c9fb5c57 Mon Sep 17 00:00:00 2001 From: mpeterv Date: Thu, 23 Jan 2014 20:17:03 +0400 Subject: [PATCH] added info on recent additions to interface --- README.md | 39 +++++++++++++++++++++++++++++++-------- spec/usage_spec.lua | 45 ++++++++++++++++++++++----------------------- 2 files changed, 53 insertions(+), 31 deletions(-) diff --git a/README.md b/README.md index 9ea21be..2e83b39 100644 --- a/README.md +++ b/README.md @@ -8,6 +8,31 @@ Not everything stated here is implemented. Features: +* Declarative and usual interfaces. + + Declarative: + + ```lua + parser:argument "input" + :description "Path to input file. " + :convert(io.open) + parser:option "-v" "--verbose" + :description "Sets logging level. " + :count "*" + ``` + + Usual: + + ```lua + parser:argument("input", { + description = "Path to input file. ", + convert = io.open + }) + parser:option("-v", "--verbose", { + description = "Sets logging level. ", + count = "*" + }) + ``` * Parses: * Short options(e.g. `-q`); * Combined short options(e.g. `-zx`); @@ -21,14 +46,12 @@ Features: Example: ```lua - parser:option "-p" "--pair" { - count = "*", - args = 2 - } + parser:option "-p" "--pair" + :count "*" + :args(2) - parser:flag "-v" "--verbose" { - count = "*" - } + parser:flag "-v" "--verbose" + :count "*" local args = parser:parse{"--pair", "Alice", "Bob", "-p", "Emma", "John", "-vvv"} -- args = { @@ -41,5 +64,5 @@ Features: ``` * Supports default values and automatic conversions for arguments. -* [___NYI___] Automatically generates error, help and usage messages. +* Automatically generates error, usage and help(__NYI__) messages. * Supports commands(e.g. in [git](http://git-scm.com/) CLI `add`, `commit`, `push`, etc. are commands). Each command has its own set of options and arguments. diff --git a/spec/usage_spec.lua b/spec/usage_spec.lua index 1338d72..6741197 100644 --- a/spec/usage_spec.lua +++ b/spec/usage_spec.lua @@ -9,15 +9,13 @@ describe("tests related to usage message generation", function() it("creates correct usage message for arguments", function() local parser = argparse.parser "foo" parser:argument "first" - parser:argument "second-and-third" { - args = 2 - } - parser:argument "maybe-fourth" { - args = "?" - } - parser:argument "others" { - args = "*" - } + parser:argument "second-and-third" + :args "2" + parser:argument "maybe-fourth" + :args "?" + parser:argument "others" + :args "*" + assert.equal( [=[Usage: foo [] [] ...]=], parser:prepare():get_usage() @@ -27,11 +25,11 @@ describe("tests related to usage message generation", function() it("creates correct usage message for options", function() local parser = argparse.parser "foo" parser:flag "-q" "--quiet" - parser:option "--from" { - count = 1, - target = "server" - } + parser:option "--from" + :count "1" + :target "server" parser:option "--config" + assert.equal( [=[Usage: foo [-q] --from [--config ]]=], parser:prepare():get_usage() @@ -43,6 +41,7 @@ describe("tests related to usage message generation", function() parser:flag "-q" "--quiet" local run = parser:command "run" run:option "--where" + assert.equal( [=[Usage: foo [-q] [] ...]=], parser:prepare():get_usage() @@ -51,10 +50,10 @@ describe("tests related to usage message generation", function() describe("usage generation can be customized", function() it("uses message provided by user", function() - local parser = argparse.parser "foo" { - usage = "Usage: obvious" - } + local parser = argparse.parser "foo" + :usage "Usage: obvious" parser:flag "-q" "--quiet" + assert.equal( [=[Usage: obvious]=], parser:prepare():get_usage() @@ -63,9 +62,9 @@ describe("tests related to usage message generation", function() it("uses per-option message provided by user", function() local parser = argparse.parser "foo" - parser:flag "-q" "--quiet" { - usage = "[-q | --quiet]" - } + parser:flag "-q" "--quiet" + :usage "[-q | --quiet]" + assert.equal( [=[Usage: foo [-q | --quiet]]=], parser:prepare():get_usage() @@ -74,10 +73,10 @@ describe("tests related to usage message generation", function() it("uses argnames provided by user", function() local parser = argparse.parser "foo" - parser:argument "inputs" { - args = "1-2", - argname = "" - } + parser:argument "inputs" + :args "1-2" + :argname "" + assert.equal( [=[Usage: foo []]=], parser:prepare():get_usage()