mirror of
https://github.com/game-ci/unity-builder.git
synced 2025-07-04 12:25:19 -04:00
Abstract out the argument parser
This commit is contained in:
parent
a513e5b640
commit
ad65b0ece4
@ -0,0 +1,76 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using UnityEditor;
|
||||
|
||||
namespace UnityBuilderAction.Input
|
||||
{
|
||||
public class ArgumentsParser
|
||||
{
|
||||
static string EOL = Environment.NewLine;
|
||||
|
||||
public static Dictionary<string, string> GetValidatedOptions()
|
||||
{
|
||||
ParseCommandLineArguments(out var validatedOptions);
|
||||
|
||||
if (!validatedOptions.TryGetValue("projectPath", out var projectPath)) {
|
||||
Console.WriteLine("Missing argument -projectPath");
|
||||
EditorApplication.Exit(110);
|
||||
}
|
||||
|
||||
if (!validatedOptions.TryGetValue("buildTarget", out var buildTarget)) {
|
||||
Console.WriteLine("Missing argument -buildTarget");
|
||||
EditorApplication.Exit(120);
|
||||
}
|
||||
|
||||
if (!Enum.IsDefined(typeof(BuildTarget), buildTarget)) {
|
||||
EditorApplication.Exit(121);
|
||||
}
|
||||
|
||||
if (!validatedOptions.TryGetValue("customBuildPath", out var customBuildPath)) {
|
||||
Console.WriteLine("Missing argument -customBuildPath");
|
||||
EditorApplication.Exit(130);
|
||||
}
|
||||
|
||||
const string defaultCustomBuildName = "TestBuild";
|
||||
if (!validatedOptions.TryGetValue("customBuildName", out var customBuildName)) {
|
||||
Console.WriteLine($"Missing argument -customBuildName, defaulting to {defaultCustomBuildName}.");
|
||||
validatedOptions.Add("customBuildName", defaultCustomBuildName);
|
||||
} else if (customBuildName == "") {
|
||||
Console.WriteLine($"Invalid argument -customBuildName, defaulting to {defaultCustomBuildName}.");
|
||||
validatedOptions.Add("customBuildName", defaultCustomBuildName);
|
||||
}
|
||||
|
||||
return validatedOptions;
|
||||
}
|
||||
|
||||
static void ParseCommandLineArguments(out Dictionary<string, string> providedArguments)
|
||||
{
|
||||
providedArguments = new Dictionary<string, string>();
|
||||
string[] args = Environment.GetCommandLineArgs();
|
||||
|
||||
Console.WriteLine(
|
||||
$"{EOL}" +
|
||||
$"###########################{EOL}" +
|
||||
$"# Parsing settings #{EOL}" +
|
||||
$"###########################{EOL}" +
|
||||
$"{EOL}"
|
||||
);
|
||||
|
||||
// Extract flags with optional values
|
||||
for (int current = 0, next = 1; current < args.Length; current++, next++) {
|
||||
// Parse flag
|
||||
bool isFlag = args[current].StartsWith("-");
|
||||
if (!isFlag) continue;
|
||||
string flag = args[current].TrimStart('-');
|
||||
|
||||
// Parse optional value
|
||||
bool flagHasValue = next < args.Length && !args[next].StartsWith("-");
|
||||
string value = flagHasValue ? args[next].TrimStart('-') : "";
|
||||
|
||||
// Assign
|
||||
Console.WriteLine($"Found flag \"{flag}\" with value \"{value}\".");
|
||||
providedArguments.Add(flag, value);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
@ -0,0 +1,3 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 46d2ec4a86604575be2b2d02b0df7b74
|
||||
timeCreated: 1587503354
|
Loading…
Reference in New Issue
Block a user