Skip to content

Commit

Permalink
Merge pull request #1301 from baronfel/better_arg_parsing
Browse files Browse the repository at this point in the history
Better Old-Style Arg Parsing
  • Loading branch information
forki committed Jul 14, 2016
2 parents b0b408f + 159c510 commit 5edc4a9
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 16 deletions.
39 changes: 28 additions & 11 deletions src/app/FAKE/CommandlineParams.fs
Expand Up @@ -4,18 +4,35 @@ open Fake

let printAllParams() = printfn "FAKE.exe [buildScript] [Target] Variable1=Value1 Variable2=Value2 ... "

(*
This is a set of flags that exist in code lower in the target processing that MUST be normalized to a certain form. This is necessary because
If any old styole variables are present in the FAKE invocation, Argu parsing will fail and we will not have parsed out those commands correctly
from the overall command line.
You can typically find usages of 'hasBuildParam' in this codebase to find places where these values are required.
*)
let specialFlags =
[
"-st", "single-target"
"--single-target", "single-target"
"-pd", "details"
"--print-details", "details"
] |> Map.ofList

let parseArgs cmdArgs =
let splitter = [| '=' |]
let split (arg:string) =
let pos = arg.IndexOfAny splitter
[| arg.Substring(0, pos); arg.Substring(pos + 1, arg.Length - pos - 1) |]
let (|KeyValue|Flag|TargetName|) ((i,arg) : int * string) =
if i = 0 then TargetName arg
else
match arg.IndexOf '=' with
| -1 -> Flag arg
| i -> KeyValue (arg.Substring(0, i), arg.Substring(i + 1, arg.Length - i - 1))

cmdArgs
|> Seq.skip 1
|> Seq.mapi (fun (i : int) (arg : string) ->
if arg.Contains "=" then
let s = split arg
if s.[0] = "logfile" then addXmlListener s.[1]
s.[0], s.[1]
else if i = 0 then "target", arg
else arg, "true")
|> Seq.mapi (fun i a -> match (i, a) with
| TargetName t -> "target", t
| Flag f when Map.containsKey f specialFlags -> Map.find f specialFlags, "true"
| Flag f -> f, "true"
| KeyValue (k,v) when k = "logfile" -> addXmlListener v; (k,v)
| KeyValue kvp -> kvp )
|> Seq.toList
11 changes: 6 additions & 5 deletions src/app/FAKE/Program.fs
Expand Up @@ -132,15 +132,16 @@ try
match Boot.ParseCommandLine(cmdArgs) with
| None ->
let buildScriptArg = if cmdArgs.Length > 1 && cmdArgs.[1].EndsWith ".fsx" then cmdArgs.[1] else Seq.head buildScripts
let fakeArgs = cmdArgs |> Array.filter (fun x -> x.StartsWith "-d:" = false)
let fsiArgs = cmdArgs |> Array.filter (fun x -> x.StartsWith "-d:") |> Array.toList
let args = CommandlineParams.parseArgs (fakeArgs |> Seq.filter ((<>) buildScriptArg) |> Seq.filter ((<>) "details"))
let fsiArgs, fakeArgs = cmdArgs |> Array.partition (fun x -> x.StartsWith "-d:")
let args = CommandlineParams.parseArgs (fakeArgs |> Seq.filter ((<>) buildScriptArg))

traceStartBuild()
let printDetails = containsParam "details" cmdArgs
let printDetails = args |> List.exists (fst >> ((=) "details"))
let argsMinusDetails = args |> List.filter (fst >> ((=) "details"))
let fsiArgsList = fsiArgs |> Seq.toList
if printDetails then
printEnvironment cmdArgs args
if not (runBuildScript printDetails buildScriptArg fsiArgs args true) then Environment.ExitCode <- 1
if not (runBuildScript printDetails buildScriptArg fsiArgsList argsMinusDetails true) then Environment.ExitCode <- 1
else if printDetails then log "Ready."
| Some handler ->
handler.Interact()
Expand Down

0 comments on commit 5edc4a9

Please sign in to comment.