mirror of
https://github.com/chylex/Brotli-Builder.git
synced 2025-04-16 00:15:42 +02:00
Add BrotliCalc command line argument parsing
This commit is contained in:
parent
f1f4079ffc
commit
616f2591d1
BrotliCalc
76
BrotliCalc/Arguments.cs
Normal file
76
BrotliCalc/Arguments.cs
Normal file
@ -0,0 +1,76 @@
|
||||
using System;
|
||||
using System.IO;
|
||||
using BrotliLib.Numbers;
|
||||
|
||||
namespace BrotliCalc{
|
||||
static class Arguments{
|
||||
public static void Read(string[] args){
|
||||
for(int index = 0; index < args.Length;){
|
||||
string key = args[index];
|
||||
string value = index + 1 < args.Length ? args[index + 1] : null;
|
||||
|
||||
index += ProcessArgument(key, value);
|
||||
}
|
||||
}
|
||||
|
||||
private static int ProcessArgument(string key, string value){
|
||||
switch(key){
|
||||
case "-help":
|
||||
static void Print(string arg, string description){
|
||||
Console.WriteLine();
|
||||
Console.ForegroundColor = ConsoleColor.White;
|
||||
Console.Write(" ");
|
||||
Console.WriteLine(arg);
|
||||
Console.ForegroundColor = ConsoleColor.Gray;
|
||||
Console.Write(" ");
|
||||
Console.WriteLine(description);
|
||||
}
|
||||
|
||||
Console.WriteLine("BrotliCalc arguments");
|
||||
Console.WriteLine("--------------------");
|
||||
|
||||
|
||||
Environment.Exit(0);
|
||||
return 1;
|
||||
|
||||
default:
|
||||
throw new ArgumentException($"Unknown argument {key}");
|
||||
}
|
||||
}
|
||||
|
||||
// Parsing
|
||||
|
||||
private static int ParseInt(string key, string value, IntRange range = default /* IntRange.Any */){
|
||||
if (value == null){
|
||||
throw new ArgumentException($"Missing value for argument {key}");
|
||||
}
|
||||
else if (!int.TryParse(value, out int result)){
|
||||
throw new FormatException($"Value of argument {key} must be a number");
|
||||
}
|
||||
else if (!range.Contains(result)){
|
||||
throw new ArgumentException($"Value of argument {key} must be in the range {range}");
|
||||
}
|
||||
else{
|
||||
return result;
|
||||
}
|
||||
}
|
||||
|
||||
private static string ParseFolder(string key, string value){
|
||||
if (value == null || !Directory.Exists(value)){
|
||||
throw new ArgumentException($"Folder specified by argument {key} does not exist: {value ?? "<missing>"}");
|
||||
}
|
||||
else{
|
||||
return value;
|
||||
}
|
||||
}
|
||||
|
||||
private static string ParseFile(string key, string value){
|
||||
if (value == null || !File.Exists(value)){
|
||||
throw new ArgumentException($"File specified by argument {key} does not exist: {value ?? "<missing>"}");
|
||||
}
|
||||
else{
|
||||
return value;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
@ -18,10 +18,21 @@ namespace BrotliCalc{
|
||||
new CmdBenchReserializeRebuild()
|
||||
};
|
||||
|
||||
private static void Main(){
|
||||
private static void Main(string[] args){
|
||||
Thread.CurrentThread.CurrentCulture = CultureInfo.InvariantCulture;
|
||||
Thread.CurrentThread.CurrentUICulture = CultureInfo.InvariantCulture;
|
||||
|
||||
try{
|
||||
Arguments.Read(args);
|
||||
}catch(Exception e){
|
||||
Console.ForegroundColor = ConsoleColor.Red;
|
||||
Console.WriteLine("Error parsing the arguments:");
|
||||
Console.ForegroundColor = ConsoleColor.Gray;
|
||||
Console.WriteLine(e.Message);
|
||||
Debug.Print(e.ToString());
|
||||
return;
|
||||
}
|
||||
|
||||
Console.ForegroundColor = ConsoleColor.White;
|
||||
Console.WriteLine("Available commands:");
|
||||
Console.WriteLine();
|
||||
@ -73,9 +84,9 @@ namespace BrotliCalc{
|
||||
}
|
||||
|
||||
IntRange range = command.ArgumentCount;
|
||||
string[] args = ParseCommandArguments(input.ElementAtOrDefault(1) ?? string.Empty);
|
||||
string[] arguments = ParseCommandArguments(input.ElementAtOrDefault(1) ?? string.Empty);
|
||||
|
||||
if (!range.Contains(args.Length)){
|
||||
if (!range.Contains(arguments.Length)){
|
||||
if (range.First == range.Last){
|
||||
Console.WriteLine($"Command requires exactly {range.First} argument(s).");
|
||||
}
|
||||
@ -93,7 +104,7 @@ namespace BrotliCalc{
|
||||
Console.ForegroundColor = ConsoleColor.White;
|
||||
|
||||
var stopwatch = Stopwatch.StartNew();
|
||||
var result = command.Process(args);
|
||||
var result = command.Process(arguments);
|
||||
stopwatch.Stop();
|
||||
|
||||
Console.WriteLine(result);
|
||||
|
Loading…
Reference in New Issue
Block a user