Skip to content

Commit a9eccb4

Browse files
committed
Add background execution option to RunCommand
This commit introduces a new boolean option `--background` (or `-b`) in the `RunCommand` to allow commands to run asynchronously. The `RunCommandOptions` class now includes a `Background` property to track this setting. Additionally, the `RunCommandOptionsHandler` has been updated to conditionally handle process output and error data based on the background execution state. #109
1 parent fab8e6b commit a9eccb4

3 files changed

Lines changed: 21 additions & 7 deletions

File tree

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,15 @@
1-
namespace FlowCtl.Commands.Run;
1+
using System.CommandLine;
2+
3+
namespace FlowCtl.Commands.Run;
24

35
internal class RunCommand : BaseCommand<RunCommandOptions, RunCommandOptionsHandler>
46
{
57
public RunCommand() : base("run", Resources.Commands_Run_Description)
68
{
9+
var backgroundOption = new Option<bool>(new[] { "-b", "--background" },
10+
getDefaultValue: () => false,
11+
description: Resources.Command_Run_BackgroundOption);
712

13+
AddOption(backgroundOption);
814
}
915
}

src/FlowCtl/Commands/Run/RunCommandOptions.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,5 +2,5 @@
22

33
internal class RunCommandOptions : ICommandOptions
44
{
5-
5+
public bool Background { get; set; } = false;
66
}

src/FlowCtl/Commands/Run/RunCommandOptionsHandler.cs

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -43,12 +43,20 @@ private Task Execute(RunCommandOptions options)
4343
};
4444

4545
var process = new Process { StartInfo = startInfo };
46-
process.OutputDataReceived += OutputDataHandler;
47-
process.ErrorDataReceived += ErrorDataHandler;
46+
47+
if (!options.Background) {
48+
process.OutputDataReceived += OutputDataHandler;
49+
process.ErrorDataReceived += ErrorDataHandler;
50+
}
51+
4852
process.Start();
49-
process.BeginOutputReadLine();
50-
process.BeginErrorReadLine();
51-
process?.WaitForExit();
53+
54+
if (!options.Background)
55+
{
56+
process.BeginOutputReadLine();
57+
process.BeginErrorReadLine();
58+
process?.WaitForExit();
59+
}
5260
}
5361
catch (Exception e)
5462
{

0 commit comments

Comments
 (0)