Add basic arguments validation

This commit is contained in:
Herve-M
2015-09-20 19:22:21 +02:00
parent c79f5a20a5
commit 1485dd1675
24 changed files with 146 additions and 53 deletions

View File

@@ -17,6 +17,8 @@ namespace OpenRA
/// </summary>
string Name { get; }
bool ValidateArguments(string[] args);
void Run(ModData modData, string[] args);
}
}

View File

@@ -9,12 +9,7 @@
#endregion
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Reflection;
using OpenRA.FileSystem;
using OpenRA.Traits;
using StyleCop;
namespace OpenRA.Mods.Common.UtilityCommands
@@ -24,6 +19,11 @@ namespace OpenRA.Mods.Common.UtilityCommands
public string Name { get { return "--check-code-style"; } }
int violationCount;
public bool ValidateArguments(string[] args)
{
return args.Length >= 2;
}
[Desc("DIRECTORY", "Check the *.cs source code files in a directory for code style violations.")]
public void Run(ModData modData, string[] args)
{

View File

@@ -9,14 +9,9 @@
#endregion
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Reflection;
using OpenRA.FileSystem;
using OpenRA.Graphics;
using OpenRA.Traits;
using StyleCop;
namespace OpenRA.Mods.Common.UtilityCommands
{
@@ -24,6 +19,11 @@ namespace OpenRA.Mods.Common.UtilityCommands
{
public string Name { get { return "--check-sequence-sprites"; } }
public bool ValidateArguments(string[] args)
{
return true;
}
[Desc("Check the sequence definitions for missing sprite files.")]
public void Run(ModData modData, string[] args)
{

View File

@@ -11,7 +11,6 @@
using System;
using System.Collections.Generic;
using System.Linq;
using OpenRA.FileSystem;
using OpenRA.Traits;
namespace OpenRA.Mods.Common.UtilityCommands
@@ -34,6 +33,11 @@ namespace OpenRA.Mods.Common.UtilityCommands
Console.WriteLine("OpenRA.Utility(1,1): Warning: {0}", e);
}
public bool ValidateArguments(string[] args)
{
return true;
}
[Desc("[MAPFILE]", "Check a mod or map for certain yaml errors.")]
public void Run(ModData modData, string[] args)
{

View File

@@ -15,7 +15,6 @@ using System.Drawing.Imaging;
using System.IO;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using OpenRA.FileFormats;
using OpenRA.Mods.Common.SpriteLoaders;
@@ -25,6 +24,11 @@ namespace OpenRA.Mods.Common.UtilityCommands
{
public string Name { get { return "--shp"; } }
public bool ValidateArguments(string[] args)
{
return args.Length >= 2;
}
[Desc("PNGFILE [PNGFILE ...]", "Combine a list of PNG images into a SHP")]
public void Run(ModData modData, string[] args)
{

View File

@@ -9,14 +9,11 @@
#endregion
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Drawing.Imaging;
using System.IO;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using OpenRA.FileFormats;
using OpenRA.Graphics;
namespace OpenRA.Mods.Common.UtilityCommands
@@ -25,6 +22,11 @@ namespace OpenRA.Mods.Common.UtilityCommands
{
public string Name { get { return "--png"; } }
public bool ValidateArguments(string[] args)
{
return args.Length >= 3;
}
[Desc("SPRITEFILE PALETTE [--noshadow] [--nopadding]",
"Convert a shp/tmp/R8 to a series of PNGs, optionally removing shadow")]
public void Run(ModData modData, string[] args)

View File

@@ -17,6 +17,11 @@ namespace OpenRA.Mods.Common.UtilityCommands
{
public string Name { get { return "--man-page"; } }
public bool ValidateArguments(string[] args)
{
return true;
}
[Desc("Create a man page in troff format.")]
public void Run(ModData modData, string[] args)
{

View File

@@ -8,7 +8,6 @@
*/
#endregion
using System;
using System.Data;
using System.Text;

View File

@@ -9,10 +9,8 @@
#endregion
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using OpenRA.FileSystem;
namespace OpenRA.Mods.Common.UtilityCommands
@@ -21,6 +19,11 @@ namespace OpenRA.Mods.Common.UtilityCommands
{
public string Name { get { return "--extract"; } }
public bool ValidateArguments(string[] args)
{
return args.Length >= 2;
}
[Desc("Extract files from mod packages to the current directory")]
public void Run(ModData modData, string[] args)
{

View File

@@ -12,7 +12,6 @@ using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
namespace OpenRA.Mods.Common.UtilityCommands
{
@@ -20,6 +19,11 @@ namespace OpenRA.Mods.Common.UtilityCommands
{
public string Name { get { return "--extract-language-strings"; } }
public bool ValidateArguments(string[] args)
{
return true;
}
[Desc("Extract translatable strings that are not yet localized and update chrome layout.")]
public void Run(ModData modData, string[] args)
{

View File

@@ -21,6 +21,11 @@ namespace OpenRA.Mods.Common.UtilityCommands
{
public string Name { get { return "--lua-docs"; } }
public bool ValidateArguments(string[] args)
{
return true;
}
[Desc("Generate Lua API documentation in MarkDown format.")]
public void Run(ModData modData, string[] args)
{

View File

@@ -20,6 +20,11 @@ namespace OpenRA.Mods.Common.UtilityCommands
{
public string Name { get { return "--docs"; } }
public bool ValidateArguments(string[] args)
{
return true;
}
[Desc("Generate trait documentation in MarkDown format.")]
public void Run(ModData modData, string[] args)
{

View File

@@ -9,15 +9,12 @@
#endregion
using System;
using System.Collections.Generic;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Reflection;
using OpenRA.FileSystem;
using OpenRA.Graphics;
using OpenRA.Traits;
using StyleCop;
namespace OpenRA.Mods.Common.UtilityCommands
{
@@ -25,6 +22,11 @@ namespace OpenRA.Mods.Common.UtilityCommands
{
public string Name { get { return "--fix-classic-tilesets"; } }
public bool ValidateArguments(string[] args)
{
return args.Length >= 2;
}
[Desc("EXTENSIONS", "Fixes missing template tile definitions and adds filename extensions.")]
public void Run(ModData modData, string[] args)
{

View File

@@ -9,10 +9,7 @@
#endregion
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using OpenRA.FileSystem;
using OpenRA.Graphics;
@@ -22,6 +19,11 @@ namespace OpenRA.Mods.Common.UtilityCommands
{
public string Name { get { return "--map-preview"; } }
public bool ValidateArguments(string[] args)
{
return args.Length >= 2;
}
[Desc("MAPFILE", "Render PNG minimap of specified oramap file.")]
public void Run(ModData modData, string[] args)
{

View File

@@ -9,9 +9,6 @@
#endregion
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace OpenRA.Mods.Common.UtilityCommands
{
@@ -19,6 +16,11 @@ namespace OpenRA.Mods.Common.UtilityCommands
{
public string Name { get { return "--map-hash"; } }
public bool ValidateArguments(string[] args)
{
return args.Length >= 2;
}
[Desc("MAPFILE", "Generate hash of specified oramap file.")]
public void Run(ModData modData, string[] args)
{

View File

@@ -17,6 +17,11 @@ namespace OpenRA.Mods.Common.UtilityCommands
{
public string Name { get { return "--map-import"; } }
public bool ValidateArguments(string[] args)
{
return args.Length >= 2;
}
[Desc("FILENAME", "Convert a legacy INI/MPR map to the OpenRA format.")]
public void Run(ModData modData, string[] args)
{

View File

@@ -10,8 +10,6 @@
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Globalization;
using System.IO;
using System.Linq;
using System.Text;

View File

@@ -13,8 +13,6 @@ using System.Collections.Generic;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using OpenRA.FileFormats;
using OpenRA.FileSystem;
using OpenRA.Graphics;
using OpenRA.Mods.Common.SpriteLoaders;
@@ -26,6 +24,11 @@ namespace OpenRA.Mods.Common.UtilityCommands
{
public string Name { get { return "--remap"; } }
public bool ValidateArguments(string[] args)
{
return args.Length >= 5;
}
[Desc("SRCMOD:PAL DESTMOD:PAL SRCSHP DESTSHP", "Remap SHPs to another palette")]
public void Run(ModData modData, string[] args)
{

View File

@@ -18,6 +18,11 @@ namespace OpenRA.Mods.Common.UtilityCommands
{
public string Name { get { return "--replay-metadata"; } }
public bool ValidateArguments(string[] args)
{
return args.Length >= 2;
}
[Desc("REPLAYFILE", "Print the game metadata from a replay file.")]
public void Run(ModData modData, string[] args)
{

View File

@@ -8,17 +8,17 @@
*/
#endregion
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace OpenRA.Mods.Common.UtilityCommands
{
class UpgradeMapCommand : IUtilityCommand
{
public string Name { get { return "--upgrade-map"; } }
public bool ValidateArguments(string[] args)
{
return args.Length >= 3;
}
[Desc("MAP", "CURRENTENGINE", "Upgrade map rules to the latest engine version.")]
public void Run(ModData modData, string[] args)
{

View File

@@ -9,10 +9,8 @@
#endregion
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
namespace OpenRA.Mods.Common.UtilityCommands
{
@@ -20,6 +18,11 @@ namespace OpenRA.Mods.Common.UtilityCommands
{
public string Name { get { return "--upgrade-mod"; } }
public bool ValidateArguments(string[] args)
{
return args.Length >= 2;
}
[Desc("CURRENTENGINE", "Upgrade mod rules to the latest engine version.")]
public void Run(ModData modData, string[] args)
{

View File

@@ -17,6 +17,11 @@ namespace OpenRA.Mods.D2k.UtilityCommands
{
public string Name { get { return "--import-d2k-map"; } }
public bool ValidateArguments(string[] args)
{
return args.Length >= 3;
}
[Desc("FILENAME", "TILESET", "Convert a legacy Dune 2000 MAP file to the OpenRA format.")]
public void Run(ModData modData, string[] args)
{

View File

@@ -20,6 +20,11 @@ namespace OpenRA.Mods.TS.UtilityCommands
{
public string Name { get { return "--tileset-import"; } }
public bool ValidateArguments(string[] args)
{
return args.Length >= 3;
}
[Desc("FILENAME", "TEMPLATEEXTENSION", "Convert a legacy tileset to the OpenRA format.")]
public void Run(ModData modData, string[] args)
{

View File

@@ -40,17 +40,37 @@ namespace OpenRA.Utility
Game.InitializeSettings(Arguments.Empty);
var modData = new ModData(modName);
args = args.Skip(1).ToArray();
var actions = new Dictionary<string, Action<ModData, string[]>>();
var actions = new Dictionary<string, KeyValuePair<Action<ModData, string[]>, Func<string[], bool>>>();
foreach (var commandType in modData.ObjectCreator.GetTypesImplementing<IUtilityCommand>())
{
var command = (IUtilityCommand)Activator.CreateInstance(commandType);
actions.Add(command.Name, command.Run);
var kvp = new KeyValuePair<Action<ModData, string[]>, Func<string[], bool>>(command.Run, command.ValidateArguments);
actions.Add(command.Name, kvp);
}
if (args.Length == 0)
{
PrintUsage(actions);
return;
}
try
{
var action = Exts.WithDefault((a, b) => PrintUsage(actions), () => actions[args[0]]);
action(modData, args);
if (!actions.ContainsKey(args[0]))
throw new ArgumentException();
var action = actions[args[0]].Key;
var validateActionArgs = actions[args[0]].Value;
if (validateActionArgs.Invoke(args))
{
action.Invoke(modData, args);
}
else
{
Console.WriteLine("Invalid arguments for '{0}'", args[0]);
GetActionUsage(args[0], action);
}
}
catch (Exception e)
{
@@ -58,12 +78,17 @@ namespace OpenRA.Utility
Log.Write("utility", "Received args: {0}", args.JoinWith(" "));
Log.Write("utility", "{0}", e);
if (e is ArgumentException)
Console.WriteLine("No such command '{0}'", args[0]);
else
{
Console.WriteLine("Error: Utility application crashed. See utility.log for details");
throw;
}
}
}
static void PrintUsage(IDictionary<string, Action<ModData, string[]>> actions)
static void PrintUsage(IDictionary<string, KeyValuePair<Action<ModData, string[]>, Func<string[], bool>>> actions)
{
Console.WriteLine("Run `OpenRA.Utility.exe [MOD]` to see a list of available commands.");
Console.WriteLine("The available mods are: " + string.Join(", ", ModMetadata.AllMods.Keys));
@@ -76,11 +101,17 @@ namespace OpenRA.Utility
foreach (var key in keys)
{
var descParts = actions[key].Method.GetCustomAttributes<DescAttribute>(true)
GetActionUsage(key, actions[key].Key);
}
}
static void GetActionUsage(string key, Action<ModData, string[]> action)
{
var descParts = action.Method.GetCustomAttributes<DescAttribute>(true)
.SelectMany(d => d.Lines).ToArray();
if (descParts.Length == 0)
continue;
return;
var args = descParts.Take(descParts.Length - 1).JoinWith(" ");
var desc = descParts[descParts.Length - 1];
@@ -88,5 +119,4 @@ namespace OpenRA.Utility
Console.WriteLine(" {0} {1}{3} {2}{3}", key, args, desc, Environment.NewLine);
}
}
}
}