コード例 #1
0
        public static Verb <TClass> Standalone <TClass>(CliParserConfig config, Action <Verb <TClass> > verbConfig) where TClass : class, new()
        {
            Verb <TClass> v = new(null, string.Empty, null, config);

            verbConfig(v);
            return(v);
        }
コード例 #2
0
 /// <summary>
 /// Returns the configured things. If any of these are unconfigured, it will return the defaults.
 /// </summary>
 /// <param name="config">The config, or the defaults if none was passed.</param>
 /// <param name="console">The console. Default is <see cref="StandardConsole"/>.</param>
 /// <param name="tokenizer">The tokenizer. Default is <see cref="QuotedStringTokenizer"/>.</param>
 /// <param name="msgFormatter">The message formatter. Defaultis <see cref="StandardMessageFormatter"/>.</param>
 public CliParserBuilder GetConfigured(out CliParserConfig config, out IConsole console, out ITokenizer tokenizer, out IMessageFormatter msgFormatter)
 {
     config       = this.config;
     console      = (this.console ??= new StandardConsole());
     tokenizer    = (this.tokenizer ??= new QuotedStringTokenizer());
     msgFormatter = (this.msgFormatter ??= new StandardMessageFormatter(ConsoleColor.Cyan));
     return(this);
 }
コード例 #3
0
 /// <summary>
 /// Create this class using a CliParserBuilder.
 /// </summary>
 internal CliParser(IConsole console, ITokenizer tokenizer, IMessageFormatter msgFormatter, Dictionary <string, IVerb> verbsByName, List <IVerb> verbs, CliParserConfig config)
 {
     Console          = console;
     Tokenizer        = tokenizer;
     MsgFormatter     = msgFormatter;
     this.verbsByName = verbsByName;
     Verbs            = verbs;
     Config           = config;
 }
コード例 #4
0
 /// <summary>
 /// Creates a new instance.
 /// </summary>
 public CliParserBuilder(CliParserConfig config, IConsole?console = null, ITokenizer?tokenizer = null, IMessageFormatter?msgFormatter = null)
 {
     this.config       = config ?? new CliParserConfig();
     verbsByName       = new Dictionary <string, IVerb>(this.config.StringComparer);
     verbs             = new List <IVerb>();
     this.console      = console;
     this.tokenizer    = tokenizer;
     this.msgFormatter = msgFormatter;
 }
コード例 #5
0
 /// <summary>
 /// Creates a new instance, using a default <see cref="CliParserConfig"/>.
 /// </summary>
 public CliParserBuilder()
 {
     config       = new CliParserConfig();
     verbsByName  = new Dictionary <string, IVerb>(config.StringComparer);
     verbs        = new List <IVerb>();
     console      = null;
     tokenizer    = null;
     msgFormatter = null;
 }
コード例 #6
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);
        }
コード例 #7
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.";
        }
コード例 #8
0
 internal static void Validate(Dictionary <string, IVerb> verbsByName, string shortName, string longName, CliParserConfig config)
 {
     if (config == null)
     {
         throw new ArgumentNullException(nameof(config), "You cannot pass a null configuration action");
     }
     if (string.IsNullOrEmpty(longName))
     {
         throw new ArgumentNullException(nameof(longName), "Verb Long Name cannot be null or an empty string");
     }
     if (string.IsNullOrEmpty(shortName))
     {
         throw new ArgumentNullException(nameof(longName), "Verb Short Name cannot be null or an empty string");
     }
     if (config.StringComparer.Equals(longName, config.ShortHelpSwitch) || config.StringComparer.Equals(longName, config.LongHelpSwitch))
     {
         throw new CliParserBuilderException($"Short name for {longName} is already used by a help switch ({config.ShortHelpSwitch} or {config.LongHelpSwitch})");
     }
     if (config.StringComparer.Equals(shortName, config.ShortHelpSwitch) || config.StringComparer.Equals(shortName, config.LongHelpSwitch))
     {
         throw new CliParserBuilderException($"Long name for {shortName} is already used by a help switch ({config.ShortHelpSwitch} or {config.LongHelpSwitch})");
     }
     if (verbsByName.ContainsKey(longName))
     {
         throw new CliParserBuilderException(string.Concat("The verb name ", longName, " has already been used, you may only use unique verb names"));
     }
     if (verbsByName.ContainsKey(shortName))
     {
         throw new CliParserBuilderException(string.Concat("The verb name ", shortName, " has already been used, you may only use unique verb names"));
     }
 }
コード例 #9
0
        /// <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;
        }