From 3ec046d064ac0ce7a3935b200d340acb9091cf61 Mon Sep 17 00:00:00 2001 From: Andrea Ghensi Date: Mon, 12 Aug 2024 20:21:47 +0200 Subject: [PATCH] feat(cli): non-zero exit code on error --- dev/pyRevitLabs/pyRevitCLI/PyRevitCLI.cs | 21 ++++++------------- .../pyRevitCLI/PyRevitCLIAppHelps.cs | 16 +++++++------- 2 files changed, 14 insertions(+), 23 deletions(-) diff --git a/dev/pyRevitLabs/pyRevitCLI/PyRevitCLI.cs b/dev/pyRevitLabs/pyRevitCLI/PyRevitCLI.cs index 910744dc6..0ece364cc 100644 --- a/dev/pyRevitLabs/pyRevitCLI/PyRevitCLI.cs +++ b/dev/pyRevitLabs/pyRevitCLI/PyRevitCLI.cs @@ -1,20 +1,15 @@ using System; -using System.IO; using System.Collections.Generic; +using System.IO; using System.Linq; using System.Reflection; - using DocoptNet; - +using pyRevitCLI.Properties; using pyRevitLabs.Common; using pyRevitLabs.NLog; using pyRevitLabs.NLog.Config; using pyRevitLabs.NLog.Targets; - using pyRevitLabs.PyRevit; -using pyRevitLabs.TargetApps.Revit; -using pyRevitCLI.Properties; - using Console = Colorful.Console; @@ -28,7 +23,8 @@ // 6) Make sure PyRevitCLI.ProcessArguments checks and ask for help print -namespace pyRevitCLI { +namespace pyRevitCLI +{ internal enum PyRevitCLILogLevel { Quiet, @@ -152,12 +148,10 @@ static void Main(string[] args) { try { // now call methods based on inputs ProcessArguments(); - - // process global error codes - ProcessErrorCodes(); } catch (Exception ex) { LogException(ex, logLevel); + Environment.ExitCode = -1; } // Flush and close down internal threads and timers @@ -166,7 +160,7 @@ static void Main(string[] args) { catch (Exception ex) { // when docopt fails, print help logger.Debug("Arg processing failed. | {0}", ex.Message); - PyRevitCLIAppHelps.PrintHelp(PyRevitCLICommandType.Main); + PyRevitCLIAppHelps.PrintHelp(PyRevitCLICommandType.Main, -1); } } @@ -957,9 +951,6 @@ private static void PrintArguments(IDictionary arguments) { Console.WriteLine("{0} = {1}", arg.Key, arg.Value.ToString()); } - private static void ProcessErrorCodes() { - } - private static void LogException(Exception ex, PyRevitCLILogLevel logLevel) { if (logLevel == PyRevitCLILogLevel.Debug) logger.Error(string.Format("{0} ({1})\n{2}", ex.Message, ex.GetType().ToString(), ex.StackTrace)); diff --git a/dev/pyRevitLabs/pyRevitCLI/PyRevitCLIAppHelps.cs b/dev/pyRevitLabs/pyRevitCLI/PyRevitCLIAppHelps.cs index 3d82725d5..acb679e3b 100644 --- a/dev/pyRevitLabs/pyRevitCLI/PyRevitCLIAppHelps.cs +++ b/dev/pyRevitLabs/pyRevitCLI/PyRevitCLIAppHelps.cs @@ -1,15 +1,15 @@ using System; using System.Collections.Generic; - -using pyRevitLabs.Common; using pyRevitLabs.Common.Extensions; -using pyRevitLabs.TargetApps.Revit; -using pyRevitLabs.PyRevit; -namespace pyRevitCLI { +namespace pyRevitCLI +{ internal static class PyRevitCLIAppHelps { - internal static void - PrintHelp(PyRevitCLICommandType commandType) { + internal static void PrintHelp(PyRevitCLICommandType commandType) + { + PrintHelp(commandType, 0); + } + internal static void PrintHelp(PyRevitCLICommandType commandType, int exitCode) { switch (commandType) { case PyRevitCLICommandType.Main: @@ -366,7 +366,7 @@ internal static void } // now exit - Environment.Exit(0); + Environment.Exit(exitCode); } private static void BuildHelp(IEnumerable docoptKeywords,