Example #1
0
    public static int Main(string[] args)
    {
        try {
            var sc = new ConsoleSynchronizationContext();
            SynchronizationContext.SetSynchronizationContext(sc);

            Runtime.SetProcessName("mdtool");

            EnabledLoggingLevel verbosity = EnabledLoggingLevel.Fatal;
            bool regUpdate = true;
            bool listTools = false;
            bool showHelp  = false;
            bool badInput  = false;

            //read the arguments to mdtool itself
            int pi = 0;
            while (pi < args.Length && (args[pi][0] == '-' || args[pi][0] == '/'))
            {
                string arg = args[pi];
                pi++;
                switch (arg)
                {
                case "-v":
                case "/v":
                case "--verbose":
                case "/verbose":
                    verbosity = (EnabledLoggingLevel)((int)verbosity << 1) | EnabledLoggingLevel.Fatal;
                    continue;

                case "-q":
                case "/q":
                    listTools = true;
                    continue;

                case "--no-reg-update":
                case "/no-reg-update":
                case "-nru":
                case "/nru":
                    regUpdate = false;
                    continue;

                case "-h":
                case "/h":
                case "--help":
                case "/help":
                    showHelp = true;
                    continue;

                default:
                    Console.Error.WriteLine("Unknown argument '{0}'", arg);
                    badInput = true;
                    break;
                }
            }

            //determine the tool name and arguments
            string   toolName = null;
            string[] toolArgs = null;
            if (!showHelp && !listTools && !badInput)
            {
                if (pi < args.Length)
                {
                    toolName = args[pi];
                    toolArgs = new string [args.Length - 1 - pi];
                    Array.Copy(args, pi + 1, toolArgs, 0, args.Length - 1 - pi);
                }
                else if (args.Length == 0)
                {
                    showHelp = true;
                }
                else
                {
                    Console.Error.WriteLine("No tool specified");
                    badInput = true;
                }
            }

            //setup app needs to skip runtime initialization or we get addin engine races
            if (toolName == "setup")
            {
                return(RunSetup(toolArgs));
            }

            // Only log fatal errors unless verbosity is specified. Command line tools should already
            // be providing feedback using the console.
            var logger = (ConsoleLogger)LoggingService.GetLogger("ConsoleLogger");
            logger.EnabledLevel = verbosity;

            Runtime.Initialize(regUpdate);

            if (showHelp || badInput)
            {
                ShowHelp(badInput);
                return(badInput? 1 : 0);
            }

            var tool = Runtime.ApplicationService.GetApplication(toolName);
            if (tool == null)
            {
                Console.Error.WriteLine("Tool '{0}' not found.", toolName);
                listTools = true;
                badInput  = true;
            }

            if (listTools)
            {
                ShowAvailableTools();
                return(badInput? 1 : 0);
            }

            var task = tool.Run(toolArgs);
            task.ContinueWith((t) => sc.ExitLoop());
            sc.RunMainLoop();
            return(task.Result);
        } catch (UserException ex) {
            Console.WriteLine(ex.Message);
            return(-1);
        } catch (Exception ex) {
            LoggingService.LogFatalError(ex.ToString());
            return(-1);
        } finally {
            try {
                Runtime.Shutdown();
            } catch {
                // Ignore shutdown exceptions
            }
        }
    }
Example #2
0
	public static int Main (string[] args)
	{
		try {
			var sc = new ConsoleSynchronizationContext ();
			SynchronizationContext.SetSynchronizationContext (sc);

			Runtime.SetProcessName ("mdtool");

			EnabledLoggingLevel verbosity = EnabledLoggingLevel.Fatal;
			bool regUpdate = true;
			bool listTools = false;
			bool showHelp = false;
			bool badInput = false;

			//read the arguments to mdtool itself
			int pi = 0;
			while (pi < args.Length && (args[pi][0] == '-' || args[pi][0] == '/')) {
				string arg = args[pi];
				pi++;
				switch (arg) {
				case "-v":
				case "/v":
				case "--verbose":
				case "/verbose":
					verbosity = (EnabledLoggingLevel)((int)verbosity << 1) | EnabledLoggingLevel.Fatal;
					continue;
				case "-q":
				case "/q":
					listTools = true;
					continue;
				case "--no-reg-update":
				case "/no-reg-update":
				case "-nru":
				case "/nru":
					regUpdate = false;
					continue;
				case "-h":
				case "/h":
				case "--help":
				case "/help":
					showHelp = true;
					continue;
				default:
					Console.Error.WriteLine ("Unknown argument '{0}'", arg);
					badInput = true;
					break;
				}
			}

			//determine the tool name and arguments
			string toolName = null;
			string[] toolArgs = null;
			if (!showHelp && !listTools && !badInput) { 
				if (pi < args.Length) {
					toolName = args[pi];
					toolArgs = new string [args.Length - 1 - pi];
					Array.Copy (args, pi + 1, toolArgs, 0, args.Length - 1 - pi);
				} else if (args.Length == 0) {
					showHelp = true;
				} else {
					Console.Error.WriteLine ("No tool specified");
					badInput = true;
				}
			}

			//setup app needs to skip runtime initialization or we get addin engine races
			if (toolName == "setup") {
				return RunSetup (toolArgs);
			}

			// Only log fatal errors unless verbosity is specified. Command line tools should already
			// be providing feedback using the console.
			var logger = (ConsoleLogger)LoggingService.GetLogger ("ConsoleLogger");
			logger.EnabledLevel = verbosity;

			Runtime.Initialize (regUpdate);

			if (showHelp || badInput) {
				ShowHelp (badInput);
				return badInput? 1 : 0;
			}

			var tool = Runtime.ApplicationService.GetApplication (toolName);
			if (tool == null) {
				Console.Error.WriteLine ("Tool '{0}' not found.", toolName);
				listTools = true;
				badInput = true;
			}

			if (listTools) {
				ShowAvailableTools ();
				return badInput? 1 : 0;
			}

			var task = tool.Run (toolArgs);
			task.ContinueWith ((t) => sc.ExitLoop ());
			sc.RunMainLoop ();
			return task.Result;

		} catch (UserException ex) {
			Console.WriteLine (ex.Message);
			return -1;
		} catch (Exception ex) {
			LoggingService.LogFatalError (ex.ToString ());
			return -1;
		} finally {
			try {
				Runtime.Shutdown ();
			} catch {
				// Ignore shutdown exceptions
			}
		}
	}