Example #1
0
        private bool TryParseHelpVerb(string[] args, object options, Pair <MethodInfo, HelpVerbOptionAttribute> helpInfo,
                                      OptionMap optionMap)
        {
            TextWriter helpWriter = _settings.HelpWriter;

            if (helpInfo != null && helpWriter != null)
            {
                if (string.Compare(args[0], helpInfo.Right.LongName, GetStringComparison(_settings)) == 0)
                {
                    // User explicitly requested help
                    string verb = args.FirstOrDefault();
                    if (verb != null)
                    {
                        OptionInfo verbOption = optionMap[verb];
                        if (verbOption != null)
                        {
                            if (verbOption.GetValue(options) is null)
                            {
                                // We need to create an instance also to render help
                                verbOption.CreateInstance(options);
                            }
                        }
                    }

                    DisplayHelpVerbText(options, helpInfo, verb);
                    return(true);
                }
            }

            return(false);
        }
Example #2
0
        private bool DoParseArgumentsVerbs(string[] args, object options, ref object verbInstance)
        {
            IList <Pair <PropertyInfo, VerbOptionAttribute> > verbs =
                ReflectionHelper.RetrievePropertyList <VerbOptionAttribute>(options);
            Pair <MethodInfo, HelpVerbOptionAttribute> helpInfo =
                ReflectionHelper.RetrieveMethod <HelpVerbOptionAttribute>(options);

            if (args.Length == 0)
            {
                if (helpInfo != null && _settings.HelpWriter != null)
                {
                    DisplayHelpVerbText(options, helpInfo, null);
                }

                return(false);
            }

            OptionMap optionMap = OptionMap.Create(options, verbs, _settings);

            if (TryParseHelpVerb(args, options, helpInfo, optionMap))
            {
                return(false);
            }

            OptionInfo verbOption = optionMap[args.First()];

            // User invoked a bad verb name
            if (verbOption is null)
            {
                if (helpInfo != null)
                {
                    DisplayHelpVerbText(options, helpInfo, null);
                }

                return(false);
            }

            verbInstance = verbOption.GetValue(options);
            if (verbInstance is null)
            {
                // Developer has not provided a default value and did not assign an instance
                verbInstance = verbOption.CreateInstance(options);
            }

            bool verbResult = DoParseArgumentsCore(args.Skip(1).ToArray(), verbInstance);

            if (!verbResult && helpInfo != null)
            {
                // Particular verb parsing failed, we try to print its help
                DisplayHelpVerbText(options, helpInfo, args.First());
            }

            return(verbResult);
        }