From 394dcf52882a1cff5af585fba5a7d95550ed1de6 Mon Sep 17 00:00:00 2001 From: Felix Schlitter Date: Sat, 14 May 2016 07:59:25 +1200 Subject: [PATCH] Fix issue #31 The syntax is still fairly constrained - but on purpose in order to keep the `--` the definitive EOA marker. The reason `[--] ARGS` is allowed, despite not being "correct" is for compatibilites sake. --- examples/git/add.js | 3 +-- examples/git/bisect.js | 3 +-- examples/git/checkout.js | 1 - examples/git/clone.js | 3 +-- examples/git/commit.js | 3 +-- examples/git/diff.js | 3 +-- examples/git/fetch.js | 1 - examples/git/grep.js | 4 +--- src/Language/Docopt/Parser/Usage.purs | 19 ++++++++++++++++--- testcases.docopt | 19 +++++++++++++++++++ 10 files changed, 41 insertions(+), 18 deletions(-) diff --git a/examples/git/add.js b/examples/git/add.js index 6e045b97..f2d75fb9 100644 --- a/examples/git/add.js +++ b/examples/git/add.js @@ -1,9 +1,8 @@ require('shelljs/global'); -// XXX: -- should be [--]. See #31 module.exports = (argv) => { const args = require('../..').run(` -usage: git add [options] -- ... +usage: git add [options] [--] ... Options: -n, --dry-run dry run diff --git a/examples/git/bisect.js b/examples/git/bisect.js index bc3d448e..25d7ada9 100644 --- a/examples/git/bisect.js +++ b/examples/git/bisect.js @@ -3,7 +3,6 @@ const _ = require('lodash'); // The git-bisect help text is a bit unconventional and requires some special // treatment for prepartion -// XXX: -- should be [--]. See #31 module.exports = (argv) => { const help = ` @@ -11,7 +10,7 @@ usage: git bisect [help|start|bad|good|skip|next|reset|visualize|replay|log|run] git bisect help print this long help message. -git bisect start [--no-checkout] [ [...]] -- [...] +git bisect start [--no-checkout] [ [...]] [--] [...] reset bisect state and start bisection. git bisect bad [] mark a known-bad revision. diff --git a/examples/git/checkout.js b/examples/git/checkout.js index 55ee43af..c756f776 100644 --- a/examples/git/checkout.js +++ b/examples/git/checkout.js @@ -1,6 +1,5 @@ require('shelljs/global'); -// XXX: -- should be [--]. See #31 module.exports = (argv) => { const args = require('../..').run(` diff --git a/examples/git/clone.js b/examples/git/clone.js index 99c47151..00f26ea3 100644 --- a/examples/git/clone.js +++ b/examples/git/clone.js @@ -1,10 +1,9 @@ require('shelljs/global'); -// XXX: -- should be [--]. See #31 module.exports = (argv) => { const args = require('../..').run(` -usage: git clone [options] -- [] +usage: git clone [options] [--] [] options: -v, --verbose be more verbose diff --git a/examples/git/commit.js b/examples/git/commit.js index a4743d60..cf02f4a9 100644 --- a/examples/git/commit.js +++ b/examples/git/commit.js @@ -1,10 +1,9 @@ require('shelljs/global'); -// XXX: -- should be [--]. See #31 module.exports = (argv) => { const args = require('../..').run(` -usage: git commit [options] -- ... +usage: git commit [options] [--] ... options: -q, --quiet suppress summary after successful commit diff --git a/examples/git/diff.js b/examples/git/diff.js index efdf132d..44652578 100644 --- a/examples/git/diff.js +++ b/examples/git/diff.js @@ -1,9 +1,8 @@ require('shelljs/global'); -// XXX: -- should be [--]. See #31 module.exports = (argv) => { const args = require('../..').run(` -usage: git diff [] [ []] -- [...] +usage: git diff [] [ []] [--] [...] `, { argv: argv, smartOptions: true }); echo(args); diff --git a/examples/git/fetch.js b/examples/git/fetch.js index fc1f3db2..91360612 100644 --- a/examples/git/fetch.js +++ b/examples/git/fetch.js @@ -1,6 +1,5 @@ require('shelljs/global'); -// XXX: -- should be [--]. See #31 // Note: `[]` has been edited to `[options]`. module.exports = (argv) => { const args = require('../..').run(` diff --git a/examples/git/grep.js b/examples/git/grep.js index e7fd6575..308be544 100644 --- a/examples/git/grep.js +++ b/examples/git/grep.js @@ -1,11 +1,9 @@ require('shelljs/global'); -// XXX: -- should be [--]. See #31 -// original: usage: git grep [options] [-e] [...] [[--] ...] module.exports = (argv) => { const args = require('../..').run(` -usage: git grep [options] [...] -- [...] +usage: git grep [options] [...] [[--] ...] options: --cached search in index instead of in the work tree diff --git a/src/Language/Docopt/Parser/Usage.purs b/src/Language/Docopt/Parser/Usage.purs index 967136b4..ca857fee 100644 --- a/src/Language/Docopt/Parser/Usage.purs +++ b/src/Language/Docopt/Parser/Usage.purs @@ -6,6 +6,7 @@ module Language.Docopt.Parser.Usage ( ) where import Prelude +import Debug.Trace import Language.Docopt.Parser.Lexer as L import Language.Docopt.Parser.Usage.Option as O import Language.Docopt.Parser.Usage.Usage as U @@ -25,7 +26,7 @@ import Language.Docopt.Parser.Usage.Argument (Argument(..)) import Language.Docopt.Parser.Usage.Usage (Usage(..)) import Text.Parsing.Parser (ParseError) as P import Text.Parsing.Parser.Combinators (try, optional, choice, sepBy1, between, - lookAhead) as P + optionMaybe, lookAhead) as P import Text.Parsing.Parser.Combinators ((), ()) import Text.Parsing.Parser.Pos (Position(Position)) as P @@ -72,9 +73,12 @@ usageParser smartOpts = do (many $ P.try $ moreIndented *> elem) `P.sepBy1` L.vbar eoa <- P.choice [ P.try $ do - moreIndented *> L.doubleDash - P.optional do + maybeInParens do + maybeInParens do + L.doubleDash + many elem many elem + many elem return $ Just EOA , (do L.eof <|> (P.lookAhead $ lessIndented <|> sameIndent) @@ -92,6 +96,15 @@ usageParser smartOpts = do (\as -> as ++ (singleton e)) xs + maybeInParens p = do + Tuple close v <- moreIndented *> do + Tuple + <$> (P.optionMaybe $ P.choice [ L.lparen *> return L.rparen + , L.lsquare *> return L.rsquare ]) + <*> p + fromMaybe (pure unit) close + return v + elem :: L.TokenParser Argument elem = defer \_ -> do P.choice $ P.try <$> diff --git a/testcases.docopt b/testcases.docopt index 973e06ff..40fb62f6 100644 --- a/testcases.docopt +++ b/testcases.docopt @@ -2721,3 +2721,22 @@ $ prog/s -b 2000 $ prog/s -a 1000 -b 2000 {"-a": [1000], "-b": [2000]} + +# Fix issue #31 +# The syntax is still fairly constrained - but on purpose in order to keep +# the `--` the definitive EOA marker. The reason `[--] ARGS` is allowed, despite +# not being "correct" is for compatibilites sake. + +r""" +Usage: foo [--] ... +""" + +$ prog +{"--": []} + +r""" +Usage: foo [[--] ARGS] +""" + +$ prog +{"--": []}