Beispiel #1
0
        private static int Execute <TOptions>(
            AbstractCommand <TOptions> command,
            AbstractCommandLineOptions options) where TOptions : AbstractCommandOptions
        {
#if DEBUG
            if (ShouldLog(Verbosity.Diagnostic))
            {
                WriteLine("--- RAW PARAMETERS ---", Verbosity.Diagnostic);
                DiagnosticWriter.WriteParameters(options);
                WriteLine("--- END OF RAW PARAMETERS ---", Verbosity.Diagnostic);

                WriteLine("--- APP PARAMETERS ---", Verbosity.Diagnostic);
                command.Options.WriteDiagnostic();
                WriteLine("--- END OF APP PARAMETERS ---", Verbosity.Diagnostic);
            }
#endif
            CommandResult result = command.Execute();

            switch (result)
            {
            case CommandResult.Success:
                return(ExitCodes.Match);

            case CommandResult.NoMatch:
                return(ExitCodes.NoMatch);

            case CommandResult.Fail:
            case CommandResult.Canceled:
                return(ExitCodes.Error);

            default:
                throw new InvalidOperationException($"Unknown enum value '{result}'.");
            }
        }
Beispiel #2
0
        private static bool ParseVerbosityAndOutput(AbstractCommandLineOptions options)
        {
            var defaultVerbosity = Verbosity.Normal;

            if (options.Verbosity != null &&
                !TryParseVerbosity(options.Verbosity, out defaultVerbosity))
            {
                return(false);
            }

            ConsoleOut.Verbosity = defaultVerbosity;

            if (options is BaseCommandLineOptions baseOptions)
            {
                if (!TryParseOutputOptions(
                        baseOptions.Output,
                        OptionNames.Output,
                        out string?filePath,
                        out Verbosity fileVerbosity,
                        out Encoding? encoding,
                        out bool append))
                {
                    return(false);
                }

                if (filePath != null)
                {
                    FileMode fileMode = (append)
                        ? FileMode.Append
                        : FileMode.Create;

                    var stream = new FileStream(filePath, fileMode, FileAccess.Write, FileShare.Read);
                    var writer = new StreamWriter(stream, encoding, bufferSize: 4096, leaveOpen: false);
                    Out = new TextWriterWithVerbosity(writer)
                    {
                        Verbosity = fileVerbosity
                    };
                }
            }

            return(true);
        }
Beispiel #3
0
        internal static void WriteParameters(AbstractCommandLineOptions commandLineOptions)
        {
            Type type = commandLineOptions.GetType();

            var values  = new List <(int index, object?value)>();
            var options = new List <(string name, object?value)>();

            foreach (PropertyInfo propertyInfo in type.GetRuntimeProperties())
            {
                OptionAttribute?optionAttribute = propertyInfo.GetCustomAttribute <OptionAttribute>();

                if (optionAttribute != null)
                {
                    object?value = propertyInfo.GetValue(commandLineOptions);

                    options.Add((optionAttribute.LongName, value));
                }
                else
                {
                    ValueAttribute?valueAttribute = propertyInfo.GetCustomAttribute <ValueAttribute>();

                    if (valueAttribute != null)
                    {
                        object?value = propertyInfo.GetValue(commandLineOptions);

                        values.Add((valueAttribute.Index, value));
                    }
                }
            }

            foreach ((int index, object?value) in values.OrderBy(f => f.index))
            {
                WriteParameterValue(index.ToString(), value);
            }

            foreach ((string name, object?value) in options.OrderBy(f => f.name))
            {
                WriteParameterValue(name, value);
            }
        }