From d37e9d584f706965a89bc2fcec49d1b2a440b57d Mon Sep 17 00:00:00 2001 From: Daniel Strobusch <1847260+dastrobu@users.noreply.github.com> Date: Thu, 3 May 2018 19:46:36 +0200 Subject: [PATCH] generated help flag inserted first. --- README.md | 8 +++----- Sources/ArgTree.swift | 6 ++---- 2 files changed, 5 insertions(+), 9 deletions(-) diff --git a/README.md b/README.md index 93b5427..4d70f7a 100644 --- a/README.md +++ b/README.md @@ -191,15 +191,13 @@ flags: ``` ##### Parse Order -The generated help flag is always added before the first [Var Args](#varargs) parser (which should usually be last), or as last parser, if there is no var args parser. -This ensures parse order plays together with var args nicely. +The generated help flag is always added as first parser to make sure it plays together with +[Var Args](#varargs) nicely. The order of the parsers can be changed after creation of the `ArgTree` object by manipulating its elements via the `MutableCollection` protocol (like an array). For example, to move the auto generated help flag parser to the end, do: ```swift var argTree = ArgTree(description: "foo") - -let help = a.remove(at: a.index(where: {$0 is Help})!) // find help -a.append(help) // add it at the end +argTree.append(argTree.removeFirst()) ``` ##### Default Action diff --git a/Sources/ArgTree.swift b/Sources/ArgTree.swift index 4ea35a6..ae14704 100644 --- a/Sources/ArgTree.swift +++ b/Sources/ArgTree.swift @@ -90,8 +90,7 @@ public class ArgTree: ParserNode { helpPrinted() } // add help as first parse, to play together with the var arg parser - let i = parsers.index(where: { $0 is VarArgs }) ?? 0 - insert(Help(longName: "help", shortName: "h", parsed: { _ in writeHelp() }), at: i) + insert(Help(longName: "help", shortName: "h", parsed: { _ in writeHelp() }), at: 0) defaultAction = writeHelp } @@ -118,8 +117,7 @@ public class ArgTree: ParserNode { helpPrinted() } // add help as first parse, to play together with the var arg parser - let i = parsers.index(where: { $0 is VarArgs }) ?? 0 - insert(Help(longName: "help", shortName: "h", parsed: { _ in printHelp() }), at: i) + insert(Help(longName: "help", shortName: "h", parsed: { _ in printHelp() }), at: 0) defaultAction = printHelp }