From 3aaa36ee8155eb357f879b371a25191a61f9d5af Mon Sep 17 00:00:00 2001 From: Constantin Tews Date: Fri, 11 Aug 2023 21:24:58 +0200 Subject: [PATCH 1/2] Open files in default/assigned program. --- src/Fli/Command.fs | 28 +++++++++++++++++----------- 1 file changed, 17 insertions(+), 11 deletions(-) diff --git a/src/Fli/Command.fs b/src/Fli/Command.fs index 6922fd2..11b7031 100644 --- a/src/Fli/Command.fs +++ b/src/Fli/Command.fs @@ -28,16 +28,16 @@ module Command = | "" -> None | _ as s -> Some s - let private createProcess executable argumentString = + let private createProcess executable argumentString openDefault = ProcessStartInfo( FileName = executable, Arguments = argumentString, WindowStyle = ProcessWindowStyle.Hidden, CreateNoWindow = true, - UseShellExecute = false, - RedirectStandardInput = true, - RedirectStandardOutput = true, - RedirectStandardError = true + UseShellExecute = openDefault, + RedirectStandardInput = not(openDefault), + RedirectStandardOutput = not(openDefault), + RedirectStandardError = not(openDefault) ) let private trim (s: string) = s.TrimEnd([| '\r'; '\n' |]) @@ -87,8 +87,8 @@ module Command = let proc = Process.Start(startInfo = psi) proc |> inputFunc - let text = proc.StandardOutput.ReadToEnd() - let error = proc.StandardError.ReadToEnd() + let text = if psi.UseShellExecute |> not then proc.StandardOutput.ReadToEnd() else "" + let error = if psi.UseShellExecute |> not then proc.StandardError.ReadToEnd() else "" proc.WaitForExit() text |> outputFunc @@ -111,8 +111,11 @@ module Command = | None -> () let private addEnvironmentVariables (variables: (string * string) list option) (psi: ProcessStartInfo) = - ((variables |> Option.defaultValue [] |> List.iter (psi.Environment.Add)), psi) - |> snd + if psi.UseShellExecute |> not then + ((variables |> Option.defaultValue [] |> List.iter (psi.Environment.Add)), psi) + |> snd + else + psi let private addCredentials (credentials: Credentials option) (psi: ProcessStartInfo) = match credentials with @@ -182,7 +185,7 @@ module Command = let (proc, flag) = (context.config.Shell, context.config.Input) ||> shellToProcess let command = context |> quoteBashCommand - (createProcess proc $"""{flag} {command}""") + (createProcess proc $"""{flag} {command}""" false) .With(WorkingDirectory = (context.config.WorkingDirectory |> Option.defaultValue "")) .With(StandardOutputEncoding = (context.config.Encoding |> Option.defaultValue null)) .With(StandardErrorEncoding = (context.config.Encoding |> Option.defaultValue null)) @@ -191,7 +194,10 @@ module Command = static member internal buildProcess(context: ExecContext) = checkVerb context.config.Verb context.config.Program - (createProcess context.config.Program (context.config.Arguments |> Option.defaultValue "")) + let arguments = (context.config.Arguments |> Option.defaultValue "") + let openDefault = if arguments = "" && context.config.EnvironmentVariables.IsNone then true else false + + (createProcess context.config.Program arguments openDefault) .With(Verb = (context.config.Verb |> Option.defaultValue null)) .With(WorkingDirectory = (context.config.WorkingDirectory |> Option.defaultValue "")) .With(UserName = (context.config.UserName |> Option.defaultValue "")) From 3d47411849672dd9d6dfff03d22cdd580e5115dc Mon Sep 17 00:00:00 2001 From: Constantin Tews Date: Fri, 11 Aug 2023 23:16:19 +0200 Subject: [PATCH 2/2] Add some docs. --- README.md | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/README.md b/README.md index bea696b..27e32cf 100644 --- a/README.md +++ b/README.md @@ -66,6 +66,14 @@ cli { } |> Command.execute ``` +or open a file in the default/assigned program: +```fsharp +cli { + Exec "test.pdf" +} +|> Command.execute +``` +(Hint: if file extension is not assigned to any installed program, it will throw a `System.NullReferenceException`) Write output to a specific file: ```fsharp