Generate, parse and escape command lines.
open BlackFox.CommandLine
type Configuration = | Release | Debug
let noRestore = true
let framework = Some "netstandard2.0"
let configuration = Release
let cmd =
CmdLine.empty
|> CmdLine.append "build"
|> CmdLine.appendIf noRestore "--no-restore"
|> CmdLine.appendPrefixIfSome "--framework" framework
|> CmdLine.appendPrefixf "--configuration" "%A" configuration
|> CmdLine.toString
// dotnet build --no-restore --framework netstandard2.0 --configuration Release
printfn "dotnet %s" cmd
The CmdLine
record and module implement a simple, pipeable API to generate command lines.
Create an empty command line.
CmdLine.empty
|> CmdLine.toString // (empty string)
Concatenate command lines
let first = CmdLine.empty |> CmdLine.append "foo"
let second = CmdLine.empty |> CmdLine.append "--bar"
CmdLine.concat [first; second]
|> CmdLine.toString // foo --bar
Append a raw (Non escaped) argument to a command line.
CmdLine.empty
|> CmdLine.appendRaw "foo bar"
|> CmdLine.appendRaw "baz"
|> CmdLine.toString // foo bar baz
Append an argument to a command line.
CmdLine.empty
|> CmdLine.append "foo bar"
|> CmdLine.append ""
|> CmdLine.append "baz"
|> CmdLine.toString // "foo bar" "" baz
Append an argument to a command line using printf-like syntax.
CmdLine.empty
|> CmdLine.appendf "--values=%s" "foo bar"
|> CmdLine.toString // "--values=foo bar"
Append an argument prefixed by another.
CmdLine.empty
|> CmdLine.appendPrefix "--foo" "bar baz"
|> CmdLine.toString // --foo "bar baz"
Append an argument prefixed by another using printf-like syntax.
CmdLine.empty
|> CmdLine.appendPrefixf "--foo" "+:%s" "bar"
|> CmdLine.toString // --foo +:bar
Append an argument to a command line if a condition is true.
CmdLine.empty
|> CmdLine.appendIf true "--foo"
|> CmdLine.appendIf false "--bar"
|> CmdLine.toString // --foo
Append an argument to a command line if a condition is true using printf-like syntax.
CmdLine.empty
|> CmdLine.appendIff true "--foo=%s" "baz"
|> CmdLine.appendIff false "--bar=%s" "baz"
|> CmdLine.toString // --foo=baz
Append an argument to a command line if a condition is true.
CmdLine.empty
|> CmdLine.appendPrefixIf true "--foo" "baz"
|> CmdLine.appendPrefixIf false "--bar" "baz"
|> CmdLine.toString // --foo baz
Append an argument to a command line if a condition is true using printf-like syntax.
CmdLine.empty
|> CmdLine.appendPrefixIff "--foo" true "+:%s" "baz"
|> CmdLine.appendPrefixIff "--bar" false "+:%s" "baz"
|> CmdLine.toString // --foo +:baz
Append an argument to a command line if the value is Some.
CmdLine.empty
|> CmdLine.appendIfSome (Some "--foo")
|> CmdLine.appendIfSome None
|> CmdLine.toString // --foo
Append an argument to a command line if the value is Some using printf-like syntax (With a single argument).
CmdLine.empty
|> CmdLine.appendIfSomef "--foo=%s" (Some "baz")
|> CmdLine.appendIfSomef "--bar=" None
|> CmdLine.toString // --foo=baz
Append an argument prefixed by another if the value is Some.
CmdLine.empty
|> CmdLine.appendPrefixIfSome "--foo" (Some "baz")
|> CmdLine.appendPrefixIfSome "--bar" None
|> CmdLine.toString // --foo baz
Append an argument prefixed by another if the value is Some using printf-like syntax (With a single argument).
CmdLine.empty
|> CmdLine.appendPrefixIfSomef "--foo" "+:%s" (Some "baz")
|> CmdLine.appendPrefixIfSomef "--bar" "+:%s" None
|> CmdLine.toString // --foo +:baz
Append a sequence of arguments.
CmdLine.empty
|> CmdLine.appendSeq ["--foo"; "bar"]
|> CmdLine.toString // --foo bar
Append a sequence of arguments using printf-like syntax (With a single argument).
CmdLine.empty
|> CmdLine.appendSeqf "--foo=%s" ["bar"; "baz"]
|> CmdLine.toString // --foo=bar --foo=baz
Append a sequence of arguments each being prefixed.
CmdLine.empty
|> CmdLine.appendPrefixSeq "--add" ["foo"; "bar"; "baz"]
|> CmdLine.toString // --add foo --add bar --add baz
Append a sequence of arguments each being prefixed using printf-like syntax (With a single argument).
CmdLine.empty
|> CmdLine.appendPrefixSeqf "--add" "./%s" ["bar"; "baz"]
|> CmdLine.toString // --add ./bar ./baz
Append an argument if the value isn't null or empty.
CmdLine.empty
|> CmdLine.appendIfNotNullOrEmpty ""
|> CmdLine.appendIfNotNullOrEmpty "--foo"
|> CmdLine.appendIfNotNullOrEmpty null
|> CmdLine.toString // --foo
Append an argument if the value isn't null or empty using printf-like syntax (With a single string argument).
CmdLine.empty
|> CmdLine.appendIfNotNullOrEmptyf "--foo=%s" ""
|> CmdLine.appendIfNotNullOrEmptyf "--foo=%s" "bar"
|> CmdLine.appendIfNotNullOrEmptyf "--foo=%s" null
|> CmdLine.toString // --foo=bar
Append an argument if the value isn't null or empty.
CmdLine.empty
|> CmdLine.appendPrefixIfNotNullOrEmpty "--foo" ""
|> CmdLine.appendPrefixIfNotNullOrEmpty "--foo" "bar"
|> CmdLine.appendPrefixIfNotNullOrEmpty "--foo" null
|> CmdLine.toString // --foo bar
appendPrefixIfNotNullOrEmptyf string -> StringFormat<string -> string> -> string -> CmdLine -> CmdLine
Append an argument prefixed by another if the value isn't null or empty using printf-like syntax (With a single string argument).
CmdLine.empty
|> CmdLine.appendPrefixIfNotNullOrEmptyf "--foo" "./%s" ""
|> CmdLine.appendPrefixIfNotNullOrEmptyf "--foo" "./%s" "bar"
|> CmdLine.appendPrefixIfNotNullOrEmptyf "--foo" "./%s" null
|> CmdLine.toString // --foo ./bar
Create a command line from a sequence of arguments.
seq { yield "foo bar"; yield "baz" }
|> CmdLine.fromSeq
|> CmdLine.toString // "foo bar" baz
Create a command line from a list of arguments.
["foo bar"; "baz"]
|> CmdLine.fromList
|> CmdLine.toString // "foo bar" baz
Create a command line from a list of arguments.
[|"foo bar"; "baz"|]
|> CmdLine.fromArray
|> CmdLine.toString // "foo bar" baz
Get a list of arguments from a command line (No escaping is applied).
CmdLine.empty
|> CmdLine.append "foo bar"
|> CmdLine.append "baz"
|> CmdLine.toList // ["foo bar"; "baz"]
Get an array of arguments from a command line (No escaping is applied).
CmdLine.empty
|> CmdLine.append "foo bar"
|> CmdLine.append "baz"
|> CmdLine.toArray // [|"foo bar"; "baz"|]
Convert a command line to string using the Microsoft C Runtime (Windows default) rules.
Convert a command line to string as expected by System.Diagnostics.Process
.
The MsvcrCommandLine
module is specific to the way the Microsoft C Runtime algorithm works on Windows. It's how the vast majority of arguments are parsed on the Windows platform.
Record type of settings for escape
:
AlwaysQuoteArguments: bool
: Specify that arguments should always be quoted, even simple valuesDoubleQuoteEscapeQuote
: Use""
to escape a quote, otherwise\"
is used- Forces all arguments containing quotes to be surrounded by quotes
- This isn't compatible with pre-2008 msvcr
Escape arguments in a form that programs parsing it as Microsoft C Runtime will successfully understand.
Parse a string representing arguments as the Microsoft C Runtime does.
- Newaita icon pack for the base of the icon (License: CC BY-NC-SA 3.0)
- @matthid for finding a bug when comparing this implementation to the one in FAKE 5