Skip to content

Commit

Permalink
feat: correctly parse cli args
Browse files Browse the repository at this point in the history
  • Loading branch information
lars-berger committed Jul 31, 2023
1 parent c4482fe commit 0da5308
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 13 deletions.
4 changes: 2 additions & 2 deletions GlazeWM.Application.CLI/Cli.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,9 @@ public Cli(WebsocketClient websocketClient)
_websocketClient = websocketClient;
}

public void Start(string message)
public void Start(string[] args)
{
_websocketClient.Send(message.Split(" "));
_websocketClient.Send(string.Join(" ", args));
// var response = _websocketClient.Send(message);
// var response = message.MapResult(
// (SubscribeMessage message) => HandleSubscribe(message),
Expand Down
21 changes: 17 additions & 4 deletions GlazeWM.Application/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
using CommandLine;
using GlazeWM.Application.CLI;
using GlazeWM.Application.IpcServer;
using GlazeWM.Application.IpcServer.Messages;
using GlazeWM.Application.WM;
using GlazeWM.Bar;
using GlazeWM.Domain;
Expand Down Expand Up @@ -37,11 +38,23 @@ private static int Main(string[] args)
bool isSingleInstance;
using var _ = new Mutex(false, "Global\\" + AppGuid, out isSingleInstance);

var parsedArgs = Parser.Default.ParseArguments<WmStartupOptions>(args);
var parsedArgs = Parser.Default.ParseArguments<
WmStartupOptions,
InvokeCommandMessage,
SubscribeMessage,
GetMonitorsMessage,
GetWorkspacesMessage,
GetWindowsMessage
>(args);

return (int)parsedArgs.MapResult(
(WmStartupOptions options) => StartWm(options, isSingleInstance),
_ => StartCli(parsedArgs.ToString(), isSingleInstance)
(InvokeCommandMessage _) => StartCli(args, isSingleInstance),
(SubscribeMessage _) => StartCli(args, isSingleInstance),
(GetMonitorsMessage _) => StartCli(args, isSingleInstance),
(GetWorkspacesMessage _) => StartCli(args, isSingleInstance),
(GetWindowsMessage _) => StartCli(args, isSingleInstance),
_ => throw new Exception()
);
}

Expand All @@ -68,15 +81,15 @@ private static ExitCode StartWm(WmStartupOptions options, bool isSingleInstance)
return ExitCode.Success;
}

private static ExitCode StartCli(string message, bool isSingleInstance)
private static ExitCode StartCli(string[] args, bool isSingleInstance)
{
if (isSingleInstance)
return ExitCode.Error;

ServiceLocator.Provider = BuildCliServiceProvider();

var cli = ServiceLocator.GetRequiredService<Cli>();
cli.Start(message);
cli.Start(args);

return ExitCode.Success;
}
Expand Down
1 change: 1 addition & 0 deletions GlazeWM.Domain/Common/WmStartupOptions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace GlazeWM.Domain.Common
{
[Verb("start", isDefault: true)]
public class WmStartupOptions
{
[Option(
Expand Down
10 changes: 3 additions & 7 deletions GlazeWM.Infrastructure/Utils/WebsocketClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -70,17 +70,13 @@ protected override void OnError(SocketError error)

public class WebsocketClient
{
public void Send(string[] args)
public void Send(string message)
{
// WebSocket server address
var address = "127.0.0.1";
if (args.Length > 0)
address = args[0];
const string address = "127.0.0.1";

// WebSocket server port
var port = 61423;
if (args.Length > 1)
port = int.Parse(args[1]);
const int port = 61423;

Console.WriteLine($"WebSocket server address: {address}");
Console.WriteLine($"WebSocket server port: {port}");
Expand Down

0 comments on commit 0da5308

Please sign in to comment.