1
0
mirror of https://bitbucket.org/anguist/ntpa synced 2025-10-06 11:04:40 +00:00
Files
ntpa/Ntp.Analyzer.Validate.Cli/Program.cs
2016-08-11 19:32:27 +02:00

191 lines
6.1 KiB
C#

//
// Copyright (c) 2013-2016 Carsten Sonne Larsen <cs@innolan.dk>
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
using System;
using System.Globalization;
using System.IO;
using System.Threading;
using Ntp.Analyzer.Common;
using Ntp.Analyzer.Config;
using Ntp.Analyzer.Config.Node;
using Ntp.Interop;
using Ntp.Analyzer.Config.Compiler;
namespace Ntp.Analyzer.Validate.Cli
{
public static class Program
{
public static int Main(string[] args)
{
// Neutralize.
Thread.CurrentThread.CurrentCulture = CultureInfo.InvariantCulture;
bool exit = false;
bool show = false;
bool validate = false;
int count;
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';
if (int.TryParse(v, out count))
{
decompiler.IndentSize = count;
} else
{
decompiler.IndentSize = 1;
}
}
},
{
"w=|whitespace=", v => {
decompiler.IndentChar = ' ';
if (int.TryParse(v, out count))
{
decompiler.IndentSize = count;
} else
{
decompiler.IndentSize = 1;
}
}
}
};
string[] rem;
try
{
rem = p.Parse(args).ToArray();
}
catch (Exception e)
{
Console.WriteLine(e.Message);
return ParameterError;
}
if (exit)
{
return ParameterError;
}
if (rem.Length > 1)
{
Console.WriteLine("Wrong parameters.");
return ParameterError;
}
if (rem.Length == 0)
{
Console.WriteLine("Wrong parameters.");
return ParameterError;
}
if (!show && !validate)
{
ShowUsage();
return ParameterError;
}
Configuration config;
try
{
config = LoadConfig(rem[0]);
}
catch (Exception e)
{
Console.WriteLine("Unexpected error while loading configuration file: " + e.Message);
return ConfigError;
}
if (config == null)
return ConfigError;
if (show)
{
decompiler.Configuration = config;
Console.Write(decompiler.Execute ());
}
else if (validate)
{
Console.WriteLine("Configuration is valid.");
}
return Success;
}
private static Configuration LoadConfig(string configFile)
{
if (!File.Exists(configFile))
{
Console.WriteLine($"Cannot find configuration file {configFile}");
return null;
}
var reader = new ConfigBuilder(configFile);
var config = reader.Execute();
if (config == null)
{
foreach (string error in reader.Errors)
{
Console.WriteLine(error);
}
return null;
}
return config;
}
private static void ShowUsage()
{
Console.WriteLine("NTP Analyzer validator tool " + VersionInfo.Text);
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");
}
}
}