/// <summary> /// エラーメッセージを取得する /// </summary> /// <param name="exception">例外</param> /// <returns></returns> private static string GetParsingErrorMessage(CommandLineArgumentException exception) { string categoryMessage = exception.Category switch { CommandLineArgumentErrorCategory.ApplyValueError => "引数の型が不正です。", CommandLineArgumentErrorCategory.ArgumentValueConversion => "引数名が不正です。", CommandLineArgumentErrorCategory.CreateArgumentsTypeError => "コンストラクタエラーです。", CommandLineArgumentErrorCategory.DuplicateArgument => "引数が複数回指定されました。", CommandLineArgumentErrorCategory.InvalidDictionaryValue => "キーの重複があります。", CommandLineArgumentErrorCategory.MissingNamedArgumentValue => "引数の指定値が足りません。", CommandLineArgumentErrorCategory.MissingRequiredArgument => "引数の数が足りません。", CommandLineArgumentErrorCategory.TooManyArguments => "引数が多すぎます。", CommandLineArgumentErrorCategory.UnknownArgument => "引数が不明です。", CommandLineArgumentErrorCategory.Unspecified => "不明なエラーです。", _ => throw new InvalidOperationException() }; return($"{exception.ArgumentName}の{categoryMessage}"); }
/// <summary> /// サイレントモードを実行する /// </summary> /// <param name="param">パラメータ</param> /// <returns></returns> static void RunSilent(string[] param) { RenameLogger.ConsoleMode = true; NativeMethods.AttachConsole(uint.MaxValue); var args = param; //コマンドライン不正時の処理 if (!IsValidCommandLine(args, out string msg)) { var e = new CommandLineArgumentException( msg, CommandLineArgumentErrorCategory.ApplyValueError); RenameLogger.WriteLog(new LogItem { Exception = e, Level = LogLevel.Error, Message = InvalidCommandLineParam() });; throw e; } //実行コマンド var command = new RenameCommand(); //実行 command.Execute(new RenameExecuteParams { SourcePath = args[0], DestinationPath = args[1], PrefixName = args[2], Signature = args[3], Settings = new OptionSettingsSerializer().Load() }); //変換結果を出力 var outStream = Console.Out; foreach (var line in command.RecordItems) { outStream.WriteLine(line); } //コマンドの破棄 command.Dispose(); }
private static void HandleError(CommandLineArgumentException ex, CommandLineParser parser) { // Write logo banner to console if parser was created successfully if (parser != null) { Console.WriteLine(parser.LogoBanner); // insert empty line Console.Error.WriteLine(); } // Write message of exception to console Console.Error.WriteLine(ex.Message); // get first nested exception Exception nestedException = ex.InnerException; // set initial indentation level for the nested exceptions int exceptionIndentationLevel = 0; while (nestedException != null && !StringUtils.IsNullOrEmpty(nestedException.Message)) { // indent exception message with 4 extra spaces // (for each nesting level) exceptionIndentationLevel += 4; // output exception message Console.Error.WriteLine(new string(' ', exceptionIndentationLevel) + nestedException.Message); // move on to next inner exception nestedException = nestedException.InnerException; } // insert empty line Console.WriteLine(); // instruct users to check the usage instructions /* * Console.WriteLine("Try 'NAnt-Gui -help' for more information"); */ }