Beispiel #1
0
        public static object?ParseOption(CmdLineOption option, string rawValue, out string?errorMessage)
        {
            Type theDataType = option.DataType;

            if (rawValue.TryConvert(theDataType, CultureInfo.InvariantCulture, out object?theConvertedValue))
            {
                errorMessage = null;
                return(theConvertedValue);
            }
            else
            {
                errorMessage = $"Value \"{rawValue}\" for option {option.ToLongCmdLineString()} is invalid.";
                return(null);
            }
        }
        public List <CmdLineToken> Tokenize(string[] args, ref CmdLineSubcommand?subcommand)
        {
            var theResult = new List <CmdLineToken>();
            var theExclusionGroupCounts = new Dictionary <string, List <CmdLineOption> >();

            int theParameterIndex = 0;

            for (int theIndex = 0; theIndex < args.Length; theIndex++)
            {
                string theArg = args[theIndex];
                if (theIndex == 0 && myDefinition.HasDifferentiatedSubcommands)
                {
                    // Muss Subcommand sein
                    subcommand = myDefinition.Subcommands.SingleOrDefault(sc => sc.Name == theArg);
                    if (subcommand != null)
                    {
                        theResult.Add(new CmdLineToken(CmdLineTokenKind.Subcommand, theArg));
                        continue;
                    }
                    else
                    {
                        if (theArg.StartsWith("-"))
                        {
                            myUsageWriter.WriteAndExit("Subcommand is missing.");
                        }
                        else
                        {
                            myUsageWriter.WriteAndExit($"Unknown subcommand: {theArg}");
                        }
                    }
                }

                if (theArg.StartsWith("-"))
                {
                    // Option
                    if (subcommand == null)
                    {
                        myUsageWriter.WriteAndExit();
                    }
                    else
                    {
                        bool isLongNameOption = theArg.StartsWith("--");

                        string theOptionTextWithoutDashes = isLongNameOption ? theArg.Substring(2) : theArg.Substring(1);

                        CmdLineOption theOption =
                            isLongNameOption ?
                            subcommand.Options.SingleOrDefault(o => o.LongName == theOptionTextWithoutDashes) :
                            subcommand.Options.SingleOrDefault(o => o.Letter.ToString() == theOptionTextWithoutDashes);

                        if (theOption != null)
                        {
                            foreach (string theExclusionGroup in theOption.ExclusionGroups)
                            {
                                if (theExclusionGroupCounts.TryGetValue(theExclusionGroup,
                                                                        out List <CmdLineOption> theOptions))
                                {
                                    theOptions.Add(theOption);
                                    theExclusionGroupCounts[theExclusionGroup] = theOptions;
                                }
                                else
                                {
                                    theOptions = new List <CmdLineOption> {
                                        theOption
                                    };
                                    theExclusionGroupCounts[theExclusionGroup] = theOptions;
                                }
                            }

                            object?theValue = true;
                            if (!theOption.IsSwitch)
                            {
                                if (theIndex < args.Length - 1)
                                {
                                    theIndex++;
                                    string theRawValue = args[theIndex];

                                    theValue = ParseUtils.ParseOption(theOption, theRawValue, out string?theErrorMessage);

                                    if (theErrorMessage != null)
                                    {
                                        myUsageWriter.WriteAndExit(subcommand, theErrorMessage);
                                    }
                                }
                                else
                                {
                                    // Value kommt nicht
                                    myUsageWriter.WriteAndExit(
                                        subcommand,
                                        $"Missing value for option {theOption.ToLongCmdLineString()}."
                                        );
                                }
                            }

                            theResult.Add(
                                new CmdLineToken(CmdLineTokenKind.Option, theOption.Letter.ToString())
                            {
                                Value = theValue !
                            }