Skip to content

Commit

Permalink
feat(cli): non-zero exit code on error
Browse files Browse the repository at this point in the history
  • Loading branch information
Andrea Ghensi committed Aug 12, 2024
1 parent 76bef4f commit 3ec046d
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 23 deletions.
21 changes: 6 additions & 15 deletions dev/pyRevitLabs/pyRevitCLI/PyRevitCLI.cs
Original file line number Diff line number Diff line change
@@ -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;


Expand All @@ -28,7 +23,8 @@
// 6) Make sure PyRevitCLI.ProcessArguments checks and ask for help print


namespace pyRevitCLI {
namespace pyRevitCLI
{

internal enum PyRevitCLILogLevel {
Quiet,
Expand Down Expand Up @@ -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
Expand All @@ -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);
}
}

Expand Down Expand Up @@ -957,9 +951,6 @@ private static void PrintArguments(IDictionary<string, ValueObject> 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));
Expand Down
16 changes: 8 additions & 8 deletions dev/pyRevitLabs/pyRevitCLI/PyRevitCLIAppHelps.cs
Original file line number Diff line number Diff line change
@@ -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:
Expand Down Expand Up @@ -366,7 +366,7 @@ internal static void
}

// now exit
Environment.Exit(0);
Environment.Exit(exitCode);
}

private static void BuildHelp(IEnumerable<string> docoptKeywords,
Expand Down

0 comments on commit 3ec046d

Please sign in to comment.