1
0
mirror of https://bitbucket.org/anguist/ntpa synced 2025-10-05 10:30:56 +00:00
Files
ntpa/Ntp.Analyzer.Cli/Program.cs
2016-04-10 11:50:35 +02:00

123 lines
4.2 KiB
C#

//
// Program.cs
//
// Author:
// Carsten Sonne Larsen <cs@innolan.dk>
//
// Copyright (c) 2013-2016 Carsten Sonne Larsen
//
// 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.Localize;
using Ntp.Analyzer.Log;
using Ntp.Analyzer.Process;
using Ntp.Interop;
namespace Ntp.Analyzer.Cli
{
/// <summary>
/// Entry point for CLI application.
/// </summary>
public static class Program
{
/// <summary>
/// The entry point of the program, where the program control starts and ends.
/// </summary>
/// <param name="args">The command-line arguments.</param>
public static void Main (string[] args)
{
// Neutralize.
Thread.CurrentThread.CurrentCulture = CultureInfo.InvariantCulture;
string name = "default";
string tempDir = Directory.GetCurrentDirectory();
string configFile = null;
string pidFile = null;
bool exit = false;
OptionSet p = new OptionSet () { { "h|?|help", v => {
ShowUsage ();
exit = true;
}
}, { "daemon=", v => {
name = v;
}
}, { "config=", v => {
configFile = v;
}
}, { "writepid=", v => {
pidFile = v;
}
}, { "temp=", v => {
tempDir = v;
}
}
};
String[] rem = p.Parse (args).ToArray ();
if (exit) {
return;
} else if (rem.Length > 0) {
Console.WriteLine ("Unknown option: " + rem [0]);
return;
} else if (configFile == null) {
Console.WriteLine ("Please specify configuration file with option --config");
return;
}
// Initialize system settings
ShellCommand.WorkingDirectory = tempDir;
int pid = ProcessInfo.ProcessId;
if (pidFile != null) {
try {
File.WriteAllText (pidFile, pid.ToString ());
} catch (Exception e) {
Console.WriteLine ("Failed to write pid file: " + e.Message);
return;
}
}
try {
Main main = new Main (configFile, pid, name);
main.Run ();
} catch (Exception e) {
Console.WriteLine ("Unexpected error: " + e.Message);
Console.WriteLine (e.StackTrace);
}
if (pidFile != null) {
try {
File.Delete (pidFile);
} catch {
}
}
}
private static void ShowUsage()
{
Console.WriteLine("NTP Analyzer " + VersionInfo.Text);
Console.WriteLine("Usage: ntpa --config file [--temp dir] [--writepid file] [--daemon name]");
}
}
}