mirror of
https://bitbucket.org/anguist/ntpa
synced 2025-11-21 18:51:50 +00:00
Code cleanup
This commit is contained in:
@ -21,6 +21,6 @@
|
||||
|
||||
using System.Reflection;
|
||||
|
||||
[assembly: AssemblyVersion("0.7.1.0")]
|
||||
[assembly: AssemblyVersion("0.7.2.0")]
|
||||
[assembly: AssemblyCopyright("Copyright (c) 2013-2016 Carsten Sonne Larsen <cs@innolan.dk>")]
|
||||
[assembly: AssemblyCulture("")]
|
||||
@ -23,7 +23,6 @@ using System;
|
||||
using System.Globalization;
|
||||
using System.IO;
|
||||
using System.Threading;
|
||||
using Ntp.Analyzer.Common;
|
||||
using Ntp.Analyzer.Config;
|
||||
using Ntp.Analyzer.Config.Compiler;
|
||||
using Ntp.Analyzer.Config.Node;
|
||||
@ -33,89 +32,55 @@ namespace Ntp.Analyzer.Validate.Cli
|
||||
{
|
||||
public static class Program
|
||||
{
|
||||
private const int ParameterErrorCode = 2;
|
||||
private const int ConfigErrorCode = 1;
|
||||
private const int SuccessCode = 0;
|
||||
private static bool usage;
|
||||
|
||||
public static int Main(string[] args)
|
||||
{
|
||||
// Neutralize.
|
||||
Thread.CurrentThread.CurrentCulture = CultureInfo.InvariantCulture;
|
||||
bool exit = false;
|
||||
bool show = false;
|
||||
bool validate = false;
|
||||
int count;
|
||||
bool show = false;
|
||||
|
||||
const int parameterError = 2;
|
||||
const int configError = 1;
|
||||
const int success = 0;
|
||||
|
||||
Decompiler decompiler = new Decompiler();
|
||||
|
||||
var p =
|
||||
new OptionSet
|
||||
{
|
||||
{
|
||||
"h|?|help", v =>
|
||||
{
|
||||
ShowUsage();
|
||||
exit = true;
|
||||
}
|
||||
},
|
||||
{
|
||||
"s|show", v => { show = true; }
|
||||
},
|
||||
{
|
||||
"d|default", v => { decompiler.ShowDefaultValues = true; }
|
||||
},
|
||||
{
|
||||
"v|validate", v => { validate = true; }
|
||||
},
|
||||
{
|
||||
"t=|tabs=", v =>
|
||||
{
|
||||
decompiler.IndentChar = '\t';
|
||||
decompiler.IndentSize = int.TryParse(v, out count) ? count : 1;
|
||||
}
|
||||
},
|
||||
{
|
||||
"w=|whitespace=", v =>
|
||||
{
|
||||
decompiler.IndentChar = ' ';
|
||||
decompiler.IndentSize = int.TryParse(v, out count) ? count : 1;
|
||||
}
|
||||
}
|
||||
};
|
||||
var dec = new Decompiler();
|
||||
var param = new OptionSet
|
||||
{
|
||||
{"h|?|help", v => { ShowUsage(); }},
|
||||
{"s|show", v => { show = true; }},
|
||||
{"d|default", v => { dec.ShowDefaultValues = true; }},
|
||||
{"v|validate", v => { validate = true; }},
|
||||
{"t=|tabs=", v => SetDecompilerParam(dec, '\t', v)},
|
||||
{"w=|whitespace=", v => SetDecompilerParam(dec, ' ', v)}
|
||||
};
|
||||
|
||||
string[] rem;
|
||||
|
||||
try
|
||||
{
|
||||
rem = p.Parse(args).ToArray();
|
||||
rem = param.Parse(args).ToArray();
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
Console.WriteLine(e.Message);
|
||||
return parameterError;
|
||||
return ParameterErrorCode;
|
||||
}
|
||||
|
||||
if (exit)
|
||||
if (usage)
|
||||
{
|
||||
return parameterError;
|
||||
return ParameterErrorCode;
|
||||
}
|
||||
|
||||
if (rem.Length > 1)
|
||||
if (rem.Length != 1)
|
||||
{
|
||||
Console.WriteLine("Wrong parameters.");
|
||||
return parameterError;
|
||||
}
|
||||
|
||||
if (rem.Length == 0)
|
||||
{
|
||||
Console.WriteLine("Wrong parameters.");
|
||||
return parameterError;
|
||||
return ParameterErrorCode;
|
||||
}
|
||||
|
||||
if (!show && !validate)
|
||||
{
|
||||
ShowUsage();
|
||||
return parameterError;
|
||||
return ParameterErrorCode;
|
||||
}
|
||||
|
||||
Configuration config;
|
||||
@ -125,24 +90,24 @@ namespace Ntp.Analyzer.Validate.Cli
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
Console.WriteLine("Unexpected error while loading configuration file: " + e.Message);
|
||||
return configError;
|
||||
Console.WriteLine($"Unexpected error while loading configuration file: {e.Message}");
|
||||
return ConfigErrorCode;
|
||||
}
|
||||
|
||||
if (config == null)
|
||||
return configError;
|
||||
return ConfigErrorCode;
|
||||
|
||||
if (show)
|
||||
{
|
||||
decompiler.Configuration = config;
|
||||
Console.Write(decompiler.Execute());
|
||||
dec.Configuration = config;
|
||||
Console.Write(dec.Execute());
|
||||
}
|
||||
else if (validate)
|
||||
{
|
||||
Console.WriteLine("Configuration is valid.");
|
||||
}
|
||||
|
||||
return success;
|
||||
return SuccessCode;
|
||||
}
|
||||
|
||||
private static Configuration LoadConfig(string configFile)
|
||||
@ -155,27 +120,34 @@ namespace Ntp.Analyzer.Validate.Cli
|
||||
|
||||
var reader = new ConfigBuilder(configFile);
|
||||
var config = reader.Execute();
|
||||
if (config == null)
|
||||
if (config != null)
|
||||
return config;
|
||||
|
||||
foreach (string error in reader.Errors)
|
||||
{
|
||||
foreach (string error in reader.Errors)
|
||||
{
|
||||
Console.WriteLine(error);
|
||||
}
|
||||
return null;
|
||||
Console.WriteLine(error);
|
||||
}
|
||||
|
||||
return config;
|
||||
return null;
|
||||
}
|
||||
|
||||
private static void SetDecompilerParam(Decompiler decompiler, char indent, string count)
|
||||
{
|
||||
int parsed;
|
||||
decompiler.IndentChar = indent;
|
||||
decompiler.IndentSize = int.TryParse(count, out parsed) ? parsed : 1;
|
||||
}
|
||||
|
||||
private static void ShowUsage()
|
||||
{
|
||||
Console.WriteLine("NTP Analyzer validator tool " + VersionInfo.Text);
|
||||
Console.WriteLine("NTP Analyzer validator tool v0.7.2");
|
||||
Console.WriteLine("Usage: ntpav [-s|-v] [-d] [-t n|-w n] configuration");
|
||||
Console.WriteLine(" -s Show parsed configuration");
|
||||
Console.WriteLine(" -v Check if configuration is valid");
|
||||
Console.WriteLine(" -d Show all default value");
|
||||
Console.WriteLine(" -t Indent with n tabulators");
|
||||
Console.WriteLine(" -w Indent with n whitespaces");
|
||||
usage = true;
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user