Skip to content

Commit

Permalink
Add --help command-line flag
Browse files Browse the repository at this point in the history
  • Loading branch information
YoshiRulz committed Jun 11, 2024
1 parent e04e053 commit 8a513ba
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 3 deletions.
25 changes: 23 additions & 2 deletions src/BizHawk.Client.Common/ArgParser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ public BespokeOption(string name, string description)

private static readonly Argument<string?> ArgumentRomFilePath = new("rom", () => null);

private static readonly IReadOnlyList<Option> GeneratedOptions;

private static readonly BespokeOption<string?> OptionAVDumpAudioSync = new(name: "--audiosync", description: "bool; `true` is the only truthy value, all else falsey; if not set, uses remembered state from config");

private static readonly BespokeOption<int?> OptionAVDumpEndAtFrame = new("--dump-length");
Expand Down Expand Up @@ -109,7 +111,7 @@ static ArgParser()

Parser = new CommandLineBuilder(root)
// .UseVersionOption() // "cannot be combined with other arguments" which is fair enough but `--config` is crucial on NixOS
// .UseHelp() //TODO
.UseHelp()
// .UseEnvironmentVariableDirective() // useless
.UseParseDirective()
.UseSuggestDirective()
Expand All @@ -119,10 +121,16 @@ static ArgParser()
// .UseExceptionHandler() // we're only using the parser, so nothing should be throwing
// .CancelOnProcessTermination() // we're only using the parser, so there's not really anything to cancel
.Build();
GeneratedOptions = root.Options.Where(static o =>
{
var t = o.GetType();
return !t.IsGenericType || t.GetGenericTypeDefinition() != typeof(BespokeOption<>); // no there is no simpler way to do this
}).ToArray();
}

/// <return>exit code, or <see langword="null"/> if should not exit</return>
/// <exception cref="ArgParserException">parsing failure, or invariant broken</exception>
public static void ParseArguments(out ParsedCLIFlags parsed, string[] args)
public static int? ParseArguments(out ParsedCLIFlags parsed, string[] args)
{
parsed = default;
var result = Parser.Parse(args);
Expand All @@ -133,6 +141,18 @@ public static void ParseArguments(out ParsedCLIFlags parsed, string[] args)
foreach (var error in result.Errors) Console.WriteLine(error.Message);
throw new ArgParserException($"failed to parse command-line arguments: {result.Errors[0].Message}");
}
var triggeredGeneratedOption = GeneratedOptions.FirstOrDefault(o => result.FindResultFor(o) is not null);
if (triggeredGeneratedOption is not null)
{
// means e.g. `./EmuHawkMono.sh --help` was passed, run whatever behaviour it normally has...
var exitCode = result.Invoke();
// ...and maybe exit
if (exitCode is not 0
|| triggeredGeneratedOption.Name is "help") // `Name` may be localised meaning this won't work? I can't grok the source for `HelpOption`
{
return exitCode;
}
}

var autoDumpLength = result.GetValueForOption(OptionAVDumpEndAtFrame);
HashSet<int>? currAviWriterFrameList = null;
Expand Down Expand Up @@ -199,6 +219,7 @@ public static void ParseArguments(out ParsedCLIFlags parsed, string[] args)
userdataUnparsedPairs: userdataUnparsedPairs,
cmdRom: result.GetValueForArgument(ArgumentRomFilePath)
);
return null;
}

public sealed class ArgParserException : Exception
Expand Down
3 changes: 2 additions & 1 deletion src/BizHawk.Client.EmuHawk/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -151,11 +151,12 @@ private static int SubMain(string[] args)
ParsedCLIFlags cliFlags = default;
try
{
ArgParser.ParseArguments(out cliFlags, args);
if (ArgParser.ParseArguments(out cliFlags, args) is int exitCode1) return exitCode1;
}
catch (ArgParser.ArgParserException e)
{
new ExceptionBox(e.Message).ShowDialog();
return 1;
}

var configPath = cliFlags.cmdConfigFile ?? Path.Combine(PathUtils.ExeDirectoryPath, "config.ini");
Expand Down

0 comments on commit 8a513ba

Please sign in to comment.