Browse Source

Switch to CommandLineParser and add directory override args

pull/324/head
Ionite 1 year ago
parent
commit
9461e181c5
No known key found for this signature in database
  1. 34
      StabilityMatrix.Avalonia/Models/AppArgs.cs
  2. 63
      StabilityMatrix.Avalonia/Program.cs
  3. 1
      StabilityMatrix.Avalonia/StabilityMatrix.Avalonia.csproj

34
StabilityMatrix.Avalonia/Models/AppArgs.cs

@ -1,4 +1,7 @@
namespace StabilityMatrix.Avalonia.Models; using System;
using CommandLine;
namespace StabilityMatrix.Avalonia.Models;
/// <summary> /// <summary>
/// Command line arguments passed to the application. /// Command line arguments passed to the application.
@ -8,41 +11,70 @@ public class AppArgs
/// <summary> /// <summary>
/// Whether to enable debug mode /// Whether to enable debug mode
/// </summary> /// </summary>
[Option("debug", HelpText = "Enable debug mode")]
public bool DebugMode { get; set; } public bool DebugMode { get; set; }
/// <summary> /// <summary>
/// Whether to use the exception dialog while debugger is attached. /// Whether to use the exception dialog while debugger is attached.
/// When no debugger is attached, the exception dialog is always used. /// When no debugger is attached, the exception dialog is always used.
/// </summary> /// </summary>
[Option("debug-exception-dialog", HelpText = "Use exception dialog while debugger is attached")]
public bool DebugExceptionDialog { get; set; } public bool DebugExceptionDialog { get; set; }
/// <summary> /// <summary>
/// Whether to use Sentry when a debugger is attached. /// Whether to use Sentry when a debugger is attached.
/// </summary> /// </summary>
[Option("debug-sentry", HelpText = "Use Sentry when debugger is attached")]
public bool DebugSentry { get; set; } public bool DebugSentry { get; set; }
/// <summary> /// <summary>
/// Whether to force show the one-click install dialog. /// Whether to force show the one-click install dialog.
/// </summary> /// </summary>
[Option("debug-one-click-install", HelpText = "Force show the one-click install dialog")]
public bool DebugOneClickInstall { get; set; } public bool DebugOneClickInstall { get; set; }
/// <summary> /// <summary>
/// Whether to disable Sentry. /// Whether to disable Sentry.
/// </summary> /// </summary>
[Option("no-sentry", HelpText = "Disable Sentry")]
public bool NoSentry { get; set; } public bool NoSentry { get; set; }
/// <summary> /// <summary>
/// Whether to disable window chrome effects /// Whether to disable window chrome effects
/// </summary> /// </summary>
[Option("no-window-chrome-effects", HelpText = "Disable window chrome effects")]
public bool NoWindowChromeEffects { get; set; } public bool NoWindowChromeEffects { get; set; }
/// <summary> /// <summary>
/// Flag to indicate if we should reset the saved window position back to (O,0) /// Flag to indicate if we should reset the saved window position back to (O,0)
/// </summary> /// </summary>
[Option("reset-window-position", HelpText = "Reset the saved window position back to (0,0)")]
public bool ResetWindowPosition { get; set; } public bool ResetWindowPosition { get; set; }
/// <summary> /// <summary>
/// Flag for disabling hardware acceleration / GPU rendering /// Flag for disabling hardware acceleration / GPU rendering
/// </summary> /// </summary>
[Option("disable-gpu-rendering", HelpText = "Disable hardware acceleration / GPU rendering")]
public bool DisableGpuRendering { get; set; } public bool DisableGpuRendering { get; set; }
/// <summary>
/// Override global app data directory
/// Defaults to (%APPDATA%|~/.config)/StabilityMatrix
/// </summary>
[Option("global-dir", HelpText = "Override global app data directory")]
public string? GlobalDirectory { get; set; }
/// <summary>
/// Override data directory
/// This takes precedence over relative portable directory and global directory
/// </summary>
[Option("data-dir", HelpText = "Override data directory")]
public string? DataDirectory { get; set; }
/// <summary>
/// Custom Uri protocol handler
/// This will send the Uri to the running instance of the app via IPC and exit
/// </summary>
[Option("uri", Hidden = true)]
public string? Uri { get; set; }
} }

63
StabilityMatrix.Avalonia/Program.cs

@ -2,7 +2,6 @@
using System.Diagnostics; using System.Diagnostics;
using System.Diagnostics.CodeAnalysis; using System.Diagnostics.CodeAnalysis;
using System.IO; using System.IO;
using System.Linq;
using System.Reflection; using System.Reflection;
using System.Runtime.InteropServices; using System.Runtime.InteropServices;
using System.Threading; using System.Threading;
@ -13,7 +12,7 @@ using Avalonia;
using Avalonia.Controls; using Avalonia.Controls;
using Avalonia.Controls.ApplicationLifetimes; using Avalonia.Controls.ApplicationLifetimes;
using Avalonia.Threading; using Avalonia.Threading;
using FluentAvalonia.Core; using CommandLine;
using NLog; using NLog;
using Polly.Contrib.WaitAndRetry; using Polly.Contrib.WaitAndRetry;
using Projektanker.Icons.Avalonia; using Projektanker.Icons.Avalonia;
@ -29,14 +28,12 @@ using StabilityMatrix.Core.Updater;
namespace StabilityMatrix.Avalonia; namespace StabilityMatrix.Avalonia;
[SuppressMessage("ReSharper", "ClassNeverInstantiated.Global")] public static class Program
[SuppressMessage("ReSharper", "MemberCanBePrivate.Global")]
public class Program
{ {
private static Logger? _logger; private static Logger? _logger;
private static Logger Logger => _logger ??= LogManager.GetCurrentClassLogger(); private static Logger Logger => _logger ??= LogManager.GetCurrentClassLogger();
public static AppArgs Args { get; } = new(); public static AppArgs Args { get; private set; } = new();
public static bool IsDebugBuild { get; private set; } public static bool IsDebugBuild { get; private set; }
@ -54,30 +51,46 @@ public class Program
{ {
StartupTimer.Start(); StartupTimer.Start();
Args.DebugMode = args.Contains("--debug"); SetDebugBuild();
Args.DebugExceptionDialog = args.Contains("--debug-exception-dialog");
Args.DebugSentry = args.Contains("--debug-sentry"); var parseResult = Parser.Default
Args.DebugOneClickInstall = args.Contains("--debug-one-click-install"); .ParseArguments<AppArgs>(args)
Args.NoSentry = args.Contains("--no-sentry"); .WithNotParsed(errors =>
Args.NoWindowChromeEffects = args.Contains("--no-window-chrome-effects"); {
Args.ResetWindowPosition = args.Contains("--reset-window-position"); foreach (var error in errors)
Args.DisableGpuRendering = args.Contains("--disable-gpu"); {
Console.Error.WriteLine(error.ToString());
// Launched for custom URI scheme, send to main process }
if (args.Contains("--uri")) });
Args = parseResult.Value;
// Launched for custom URI scheme, handle and exit
if (Args.Uri is { } uriArg)
{ {
var uriArg = args.ElementAtOrDefault(args.IndexOf("--uri") + 1); try
if (
Uri.TryCreate(uriArg, UriKind.Absolute, out var uri)
&& string.Equals(uri.Scheme, UriHandler.Scheme, StringComparison.OrdinalIgnoreCase)
)
{ {
UriHandler.SendAndExit(uri); if (
Uri.TryCreate(uriArg, UriKind.Absolute, out var uri)
&& string.Equals(
uri.Scheme,
UriHandler.Scheme,
StringComparison.OrdinalIgnoreCase
)
)
{
UriHandler.SendAndExit(uri);
}
Environment.Exit(0);
}
catch (Exception e)
{
Console.Error.WriteLine($"Uri handler encountered an error: {e.Message}");
Environment.Exit(1);
} }
} }
SetDebugBuild();
HandleUpdateReplacement(); HandleUpdateReplacement();
var infoVersion = Assembly var infoVersion = Assembly

1
StabilityMatrix.Avalonia/StabilityMatrix.Avalonia.csproj

@ -32,6 +32,7 @@
<PackageReference Include="Avalonia.HtmlRenderer" Version="11.0.0" /> <PackageReference Include="Avalonia.HtmlRenderer" Version="11.0.0" />
<PackageReference Include="Avalonia.Xaml.Behaviors" Version="11.0.5" /> <PackageReference Include="Avalonia.Xaml.Behaviors" Version="11.0.5" />
<PackageReference Include="AvaloniaEdit.TextMate" Version="11.0.5" /> <PackageReference Include="AvaloniaEdit.TextMate" Version="11.0.5" />
<PackageReference Include="CommandLineParser" Version="2.9.1" />
<PackageReference Include="CommunityToolkit.Mvvm" Version="8.2.2" /> <PackageReference Include="CommunityToolkit.Mvvm" Version="8.2.2" />
<PackageReference Include="DiscordRichPresence" Version="1.2.1.24" /> <PackageReference Include="DiscordRichPresence" Version="1.2.1.24" />
<PackageReference Include="Dock.Avalonia" Version="11.0.0.3" /> <PackageReference Include="Dock.Avalonia" Version="11.0.0.3" />

Loading…
Cancel
Save