SonarQube.Common.CommandLineParser.ParseArguments C# (CSharp) Метод

ParseArguments() публичный Метод

Parses the supplied arguments. Logs errors for unrecognized, duplicate or missing arguments.
public ParseArguments ( string commandLineArgs, ILogger logger, IEnumerable &argumentInstances ) : bool
commandLineArgs string
logger ILogger
argumentInstances IEnumerable A list of argument instances that have been recognized
Результат bool
        public bool ParseArguments(string[] commandLineArgs, ILogger logger, out IEnumerable<ArgumentInstance> argumentInstances)
        {
            if (commandLineArgs == null)
            {
                throw new ArgumentNullException("commandLineArgs");
            }
            if (logger == null)
            {
                throw new ArgumentNullException("logger");
            }

            bool parsedOk = true;

            // List of values that have been recognized
            IList<ArgumentInstance> recognized = new List<ArgumentInstance>();

            foreach (string arg in commandLineArgs)
            {
                string prefix;
                ArgumentDescriptor descriptor;

                if (TryGetMatchingDescriptor(arg, out descriptor, out prefix))
                {
                    string newId = descriptor.Id;

                    if (!descriptor.AllowMultiple && IdExists(newId, recognized))
                    {
                        string existingValue;
                        ArgumentInstance.TryGetArgumentValue(newId, recognized, out existingValue);
                        logger.LogError(Resources.ERROR_CmdLine_DuplicateArg, arg, existingValue);
                        parsedOk = false;
                    }
                    else
                    {
                        // Store the argument
                        string argValue = arg.Substring(prefix.Length);
                        recognized.Add(new ArgumentInstance(descriptor, argValue));
                    }
                }
                else
                {
                    if (!this.allowUnrecognized)
                    {
                        logger.LogError(Resources.ERROR_CmdLine_UnrecognizedArg, arg);
                        parsedOk = false;
                    }

                    Debug.WriteLineIf(this.allowUnrecognized, "Ignoring unrecognized argument: " + arg);
                }
            }

            // We'll check for missing arguments this even if the parsing failed so we output as much detail
            // as possible about the failures.
            parsedOk &= CheckRequiredArgumentsSupplied(recognized, logger);

            argumentInstances = parsedOk ? recognized : Enumerable.Empty<ArgumentInstance>();

            return parsedOk;
        }

Usage Example

        /// <summary>
        /// Attempts to process the supplied command line arguments and
        /// reports any errors using the logger.
        /// Returns false if any parsing errors were encountered.
        /// </summary>
        public static bool TryProcessArgs(string[] commandLineArgs, ILogger logger, out IAnalysisPropertyProvider provider)
        {
            if (commandLineArgs == null)
            {
                throw new ArgumentNullException("commandLineArgs");
            }
            if (logger == null)
            {
                throw new ArgumentNullException("logger");
            }

            provider = null;
            IEnumerable<ArgumentInstance> arguments;

            // This call will fail if there are duplicate or missing arguments
            CommandLineParser parser = new CommandLineParser(Descriptors, false /* don't allow unrecognized arguments*/);
            bool parsedOk = parser.ParseArguments(commandLineArgs, logger, out arguments);

            if (parsedOk)
            {
                // Handler for command line analysis properties
                parsedOk &= CmdLineArgPropertyProvider.TryCreateProvider(arguments, logger, out provider);

                Debug.Assert(!parsedOk || provider != null);

                if (parsedOk && !AreParsedArgumentsValid(provider, logger))
                {
                    provider = null;
                }
            }

            return provider != null;
        }
All Usage Examples Of SonarQube.Common.CommandLineParser::ParseArguments