Exemplo n.º 1
0
        internal Verb(string?shortName, string longName, string?parentShortAndLongName, CliParserConfig config)
        {
            ShortName            = shortName;
            LongName             = longName;
            ShortAndLongName     = ArgUtils.ShortAndLongName(shortName, longName);
            FullShortAndLongName = parentShortAndLongName == null?ArgUtils.ShortAndLongName(shortName, longName) : string.Concat(parentShortAndLongName, ' ', ArgUtils.ShortAndLongName(shortName, longName));

            Invoke      = () => throw new CliParserBuilderException(string.Concat("Invoke for verb ", FullShortAndLongName, " has not been configured"));
            InvokeAsync = () => throw new CliParserBuilderException(string.Concat("InvokeAsync for verb ", FullShortAndLongName, " has not been configured"));
            this.config = config;
            HelpText    = "No help available.";
            allVerbs    = new();
            verbsByName = new(config.StringComparer);
        }
Exemplo n.º 2
0
        internal Verb(string?shortName, string longName, string?parentShortAndLongName, CliParserConfig config)
        {
            ShortName            = shortName;
            LongName             = longName;
            ShortAndLongName     = ArgUtils.ShortAndLongName(shortName, longName);
            FullShortAndLongName = parentShortAndLongName == null?ArgUtils.ShortAndLongName(shortName, longName) : string.Concat(parentShortAndLongName, ' ', ArgUtils.ShortAndLongName(shortName, longName));

            Invoke            = x => throw new CliParserBuilderException(string.Concat("Invoke for verb ", FullShortAndLongName, " has not been configured"));
            InvokeAsync       = x => throw new CliParserBuilderException(string.Concat("InvokeAsync for verb ", FullShortAndLongName, " has not been configured"));
            this.config       = config;
            verbsByName       = new(config.StringComparer);
            allVerbs          = new();
            allValues         = new List <IValue <TClass> >();
            allSwitches       = new List <ISwitch <TClass> >();
            allOptions        = new List <IOption <TClass> >();
            allSwitchesByName = new Dictionary <string, ISwitch <TClass> >(config.StringComparer);
            allOptionsByName  = new Dictionary <string, IOption <TClass> >(config.StringComparer);
            HelpText          = "No help available.";
        }
        /// <summary>
        /// Writes the overall help.
        /// Lists all verbs, their aliases, and their help text.
        /// </summary>
        /// <param name="console">The console help is written to.</param>
        public void WriteOverallHelp(IConsole console, IReadOnlyCollection <IVerb> verbs, CliParserConfig config)
        {
            ConsoleColor original = console.ForegroundColor;
            int          width    = console.CurrentWidth;

            console.WriteLine("Verbs...");

            // This is the number of characters we're reserving for the key text
            // A few more spaces just so it's easier to read

            WritePaddedKeywordDescriptions(console,
                                           KeywordColor,
                                           verbs.Select(verb => new KeywordAndDescription(verb.ShortAndLongName, verb.HelpText)));

            console.Write("For detailed help, use: ");
            console.ForegroundColor = KeywordColor;
            console.WriteLine($"verbname " + ArgUtils.ShortAndLongName(config.ShortHelpSwitch, config.LongHelpSwitch));
            console.WriteLine();
            console.ForegroundColor = original;
        }
        /// <summary>
        /// Writes specific help for <paramref name="verb"/> to <paramref name="console"/>. Shows all of the possible arguments and their help text.
        /// Any arguments that aren't required are shown in [brackets].
        /// </summary>
        /// <typeparam name="TClass">The class that this verb parses input into.</typeparam>
        /// <param name="console">The console help is written to.</param>
        /// <param name="verb">The verb to write help for.</param>
        public void WriteSpecificHelp <TClass>(IConsole console, Verb <TClass> verb) where TClass : class, new()
        {
            ConsoleColor original = console.ForegroundColor;

            console.WriteLine(verb.HelpText);
            console.Write(verb.FullShortAndLongName + ' ');

            foreach (IOption opt in verb.AllOptions)
            {
                console.Write(ArgUtils.ShortAndLongName(opt.ShortName, opt.LongName, opt.DescriptiveName, opt.ArgumentRequired != ArgumentRequired.Required));
                console.Write(' ');
            }
            foreach (ISwitch sw in verb.AllSwitches)
            {
                console.Write(ArgUtils.ShortAndLongName(sw.ShortName, sw.LongName, sw.ArgumentRequired != ArgumentRequired.Required));
                console.Write(' ');
            }
            int i = 1;

            foreach (IValue val in verb.AllValues)
            {
                console.Write(val.ArgumentRequired == ArgumentRequired.Required ? val.DescriptiveName : string.Concat('[', val.DescriptiveName, ']'));
                console.Write(' ');
                i++;
            }
            if (verb.MultiValue != null)
            {
                console.Write(verb.MultiValue.ArgumentRequired == ArgumentRequired.Required
                                        ? verb.MultiValue.DescriptiveName
                                        : string.Concat('[', verb.MultiValue.DescriptiveName, ']'));
            }
            console.WriteLine();
            console.WriteLine();

            List <KeywordAndDescription> stuffToWrite = new();

            if (verb.AllVerbs.Count != 0)
            {
                console.WriteLine("Verbs: ");
                foreach (IVerb subVerb in verb.AllVerbs)
                {
                    stuffToWrite.Add(new(subVerb.ShortAndLongName, subVerb.HelpText));
                }
                WritePaddedKeywordDescriptions(console, KeywordColor, stuffToWrite);
                stuffToWrite.Clear();
            }
            if (verb.AllOptions.Count != 0)
            {
                console.WriteLine("Options: ");
                foreach (IOption opt in verb.AllOptions)
                {
                    stuffToWrite.Add(new KeywordAndDescription(opt.ShortAndLongName(), GetRequiredness(opt.ArgumentRequired) + opt.HelpText));
                }
                WritePaddedKeywordDescriptions(console, KeywordColor, stuffToWrite);
                stuffToWrite.Clear();
            }
            if (verb.AllSwitches.Count != 0)
            {
                console.WriteLine("Switches: ");
                foreach (ISwitch sw in verb.AllSwitches)
                {
                    stuffToWrite.Add(new KeywordAndDescription(sw.ShortAndLongName(), GetRequiredness(sw.ArgumentRequired) + sw.HelpText));
                }
                WritePaddedKeywordDescriptions(console, KeywordColor, stuffToWrite);
                stuffToWrite.Clear();
            }
            if (verb.AllValues.Count != 0)
            {
                console.WriteLine("Values: ");
                foreach (IValue val in verb.AllValues)
                {
                    stuffToWrite.Add(new KeywordAndDescription(val.DescriptiveName, GetRequiredness(val.ArgumentRequired) + val.HelpText));
                }
                WritePaddedKeywordDescriptions(console, KeywordColor, stuffToWrite);
                stuffToWrite.Clear();
            }
            if (verb.MultiValue != null)
            {
                console.WriteLine("Multi-Value: ");
                stuffToWrite.Add(new KeywordAndDescription(verb.MultiValue.DescriptiveName, GetRequiredness(verb.MultiValue.ArgumentRequired) + verb.MultiValue.HelpText));
                WritePaddedKeywordDescriptions(console, KeywordColor, stuffToWrite);
                stuffToWrite.Clear();
            }
            console.ForegroundColor = original;
        }