Snippets

Exec (Like subprocess.run)

open System.Diagnostics

module Subprocess =
    let private escapedArgs (args: string) =
        args.Replace("\"", "\\\"") 
    
    let private runBody (proc: Process) (args: string) =
        proc.StartInfo.FileName <- "/bin/bash"
        proc.StartInfo.Arguments <- sprintf  "-c \"%s\"" (escapedArgs args)
        proc.Start() |> ignore
        proc.WaitForExit()
        proc

    let run(args: string) =
        let proc = new Process()
        runBody proc args |> ignore    
    
    let checkOutput(args: string): string =
        let mutable proc = new Process()
        // redirect output
        proc.StartInfo.RedirectStandardOutput <- true
        proc.StartInfo.RedirectStandardError <- true
        proc <- runBody proc args
        proc.StandardOutput.ReadToEnd()

// Subprocess.run("ls")
// printfn "output:\n%s" (Subprocess.checkOutput("ls"))

IFSharp with Paket

#load "Paket.fsx"
Paket.Version ["Argu", "6.0.0"]
#load "Paket.Generated.Refs.fsx"

open Argu