Ejemplo n.º 1
0
        internal bool IsOption(string programArgument)
        {
            try
            {
                (OptionType type, string fullKey, List <char> shortKeys, _) = OptionTokenizer.TokenizeProgramArgument(programArgument);

                switch (type)
                {
                case OptionType.Full:
                    return(IsFullOption(fullKey));

                case OptionType.Short:
                    return(IsShortOption(shortKeys.Single()));

                case OptionType.Stacked:
                    return(shortKeys.Count == shortKeys.Distinct().Count() &&
                           shortKeys.All(_shortOptionInfo.ContainsKey));

                default:
                    throw new ArgumentOutOfRangeException($"'{type}' is not a valid option type.");
                }
            }
            catch (ArgumentException)
            {
                return(false);
            }

            // local functions
            bool IsFullOption(string s) => _fullOptionInfo.ContainsKey(s);

            bool IsShortOption(char c) => _shortOptionInfo.ContainsKey(c);
        }
Ejemplo n.º 2
0
                private static Action <int> KeysMustBeUnique <TRunInfo>(CommandBase <TRunInfo> command)
                    where TRunInfo : class
                {
                    return(commandLevel =>
                    {
                        var fullKeys = new List <string>();
                        var shortKeys = new List <char>();

                        command.Options.ForEach(o =>
                        {
                            var(fullKey, shortKey) = OptionTokenizer.TokenizeKeyConfiguration(o.Key);

                            fullKeys.Add(fullKey);

                            if (shortKey.HasValue)
                            {
                                shortKeys.Add(shortKey.Value);
                            }
                        });

                        bool duplicateFull = fullKeys.Count != fullKeys.Distinct().Count();
                        if (duplicateFull)
                        {
                            throw new CommandValidationException("Command contains options with duplicate full keys.",
                                                                 CommandValidationError.DuplicateKey, commandLevel);
                        }

                        bool duplicateShort = shortKeys.Count != shortKeys.Distinct().Count();
                        if (duplicateShort)
                        {
                            throw new CommandValidationException("Command contains options with duplicate short keys.",
                                                                 CommandValidationError.DuplicateKey, commandLevel);
                        }
                    });
                }
Ejemplo n.º 3
0
        internal override string GetHelpToken()
        {
            if (!string.IsNullOrWhiteSpace(HelpToken))
            {
                return(HelpToken);
            }

            (string fullKey, char?shortKey) = OptionTokenizer.TokenizeKeyConfiguration(Key);

            string result = $"[--{fullKey}";

            if (shortKey.HasValue)
            {
                result += $"|-{shortKey.Value}";
            }

            return(result + "]");
        }
Ejemplo n.º 4
0
            internal static void OptionKeysMustBeUnique <TRunInfo>(List <OptionBase <TRunInfo> > options,
                                                                   List <OptionBase <TRunInfo> > otherOptions, int commandLevel)
                where TRunInfo : class
            {
                var fullKeys  = new List <string>();
                var shortKeys = new List <char>();

                if (otherOptions != null)
                {
                    options = options.Concat(otherOptions).ToList();
                }

                options.ForEach(o =>
                {
                    var(fullKey, shortKey) = OptionTokenizer.TokenizeKeyConfiguration(o.Key);

                    fullKeys.Add(fullKey);

                    if (shortKey.HasValue)
                    {
                        shortKeys.Add(shortKey.Value);
                    }
                });

                bool duplicateFull = fullKeys.Count != fullKeys.Distinct().Count();

                if (duplicateFull)
                {
                    throw new CommandValidationException("Command contains options with duplicate full keys.",
                                                         CommandValidationError.DuplicateKey, commandLevel);
                }

                bool duplicateShort = shortKeys.Count != shortKeys.Distinct().Count();

                if (duplicateShort)
                {
                    throw new CommandValidationException("Command contains options with duplicate short keys.",
                                                         CommandValidationError.DuplicateKey, commandLevel);
                }
            }
Ejemplo n.º 5
0
        private void InitializeMaps(List <OptionBase <TRunInfo> > options)
        {
            foreach (OptionBase <TRunInfo> option in options)
            {
                addProcessInfo(option);

                if (option.Type == typeof(bool))
                {
                    addToBoolMaps(option);
                }
            }

            // local functions
            void addProcessInfo(OptionBase <TRunInfo> option)
            {
                var(fullKey, shortKey) = OptionTokenizer.TokenizeKeyConfiguration(option.Key);
                OptionProcessInfo <TRunInfo> processInfo = option.GetProcessInfo();

                _fullOptionInfo.Add(fullKey, processInfo);

                if (shortKey != null)
                {
                    _shortOptionInfo.Add(shortKey.Value, processInfo);
                }
            }

            void addToBoolMaps(OptionBase <TRunInfo> option)
            {
                var(fullKey, shortKey) = OptionTokenizer.TokenizeKeyConfiguration(option.Key);

                _fullBoolTypeKeys.Add(fullKey);

                if (shortKey != null)
                {
                    _shortBoolTypeKeys.Add(shortKey.Value);
                }
            }
        }
Ejemplo n.º 6
0
        internal override ProcessStageResult ProcessStage(ProcessContext <TRunInfo> context,
                                                          Action <CommandBase <TRunInfo> > resetContextFunc)
        {
            while (context.ProgramArguments.HasMore())
            {
                if (context.ProgramArguments.NextIsSubCommand())
                {
                    return(ProcessResult.Continue);
                }

                if (!context.ProgramArguments.NextIsOption())
                {
                    return(ProcessResult.Continue);                    // results are optional so just continue
                }

                string option = context.ProgramArguments.Dequeue();

                var(type, fullKey, shortKeys, valueFromToken) = OptionTokenizer.TokenizeProgramArgument(option);

                bool isBoolType = fullKey != null
                                        ? context.Options.IsBoolType(fullKey)
                                        : shortKeys.All(context.Options.IsBoolType);

                if (type == OptionType.Stacked && !isBoolType)
                {
                    throw new ProcessException($"Stacked options can only be mapped to "
                                               + $"boolean properties but found one or more invalid options in: {string.Join("", shortKeys)}",
                                               ProcessError.InvalidStackedOption, context.CommandLevel);
                }

                string value = ResolveValue(valueFromToken, isBoolType, context);

                ProcessStageResult optionResult;
                switch (type)
                {
                case OptionType.Full:
                    optionResult = ProcessFull(fullKey, value, context);
                    break;

                case OptionType.Short:
                    optionResult = ProcessShort(shortKeys.Single(), value, context);
                    break;

                case OptionType.Stacked:
                    optionResult = ProcessStacked(shortKeys, value, context);
                    break;

                default:
                    throw new ArgumentOutOfRangeException(nameof(type), $"'{type}' is not a valid option type.");
                }

                switch (optionResult)
                {
                case Continue _:
                    break;

                case End _:
                    return(ProcessResult.End);

                case null:
                default:
                    throw new ProcessException(
                              "Option OnProcess callback returned an unknown result.",
                              ProcessError.InvalidStageResult, context.CommandLevel);
                }
            }

            return(ProcessResult.Continue);
        }