Esempio n. 1
0
 /// <summary>
 /// Initializes new instance of <see cref="CommandInfo"/>
 /// </summary>
 /// <param name="commandInstance">Instance of the command itself</param>
 /// <param name="commandModelBuilder">Instance of model builder</param>
 public CommandInfo(IConsoleCommand commandInstance, CommandModelBuilder commandModelBuilder)
 {
     this.Command             = commandInstance;
     this.CommandModelBuilder = commandModelBuilder;
 }
Esempio n. 2
0
        /// <summary>
        /// Prints full (?) help about particular command.
        /// </summary>
        /// <param name="cmdModelBuilder"></param>
        public void PrintCommandHelp(CommandModelBuilder cmdModelBuilder)
        {
            // TODO: implement table that allows separate column coloring
            // TODO: or per-cell coloring
            // this would allow for better display.

            this._console.WriteLine(this._helpStyles.CommonStyles.Warning, $"Function '{nameof(this.PrintCommandHelp)}' is not yet fully implemented.");
            this._console.WriteLine(this._helpStyles.CommonStyles.Default, string.Empty);

            this._console.WriteLine(this._helpStyles.HelpDescription, cmdModelBuilder.CommandDescription);
            this._console.WriteLine(this._helpStyles.HelpBody, "Syntax:");
            this._console.WriteText(this._helpStyles.CommonStyles.Default, "  ");

            // TODO: move switch-less to the end and sort!

            var syntax  = cmdModelBuilder.GetSyntax().ToArray();
            var options = string.Join(" ", syntax.OrderBy(s => s.IsMandatory).ThenBy(s => s.OptionType).Select(s => s.GetSyntaxString(this._options)));

            // TODO: use same syntax order like in GetSyntax()

            this._console.WriteLine(this._helpStyles.HelpSyntax, $"{cmdModelBuilder.CommandName} {options}");

            if (syntax.Any())
            {
                this._console.WriteLine(this._helpStyles.CommonStyles.Default, string.Empty);
                this._console.WriteLine(this._helpStyles.HelpBody, "Where:");
                foreach (var syntaxInfo in syntax)
                {
                    var mandatoryIndicator = syntaxInfo.IsMandatory ? "*" : string.Empty;

                    if (syntaxInfo.OptionType != SyntaxOptionType.Switchless)
                    {
                        var literals = string.Join(" ", syntaxInfo.Literals);

                        this._console.WriteText(this._helpStyles.HelpDefinition, $"  {literals}\t{syntaxInfo.OptionName}{mandatoryIndicator}\t");
                        string desc = (syntaxInfo.DefaultValue == null)
                            ? syntaxInfo.Description
                            : $"{syntaxInfo.Description} Defaults to '{syntaxInfo.DefaultValue.ToString()}'";
                        this._console.WriteLine(this._helpStyles.HelpDescription, desc);

                        // TODO: more description for values of switches / values etc or custom description lines - for each property or available value
                    }
                    else
                    {
                        this._console.WriteText(this._helpStyles.HelpDefinition, $"  \"{syntaxInfo.OptionName}\"{mandatoryIndicator}\t");
                        string desc = (syntaxInfo.DefaultValue == null)
                            ? syntaxInfo.Description
                            : $"{syntaxInfo.Description} Defaults to '{syntaxInfo.DefaultValue.ToString()}'";
                        this._console.WriteLine(this._helpStyles.HelpDescription, desc);
                    }
                }

                this._console.WriteLine(this._helpStyles.CommonStyles.Default, string.Empty);
                if (syntax.Any(s => s.IsMandatory))
                {
                    this._console.WriteLine(this._helpStyles.CommonStyles.Default, "Options denoted with \"*\" character are mandatory. In the syntax they are in pointy brackets.");
                }
                this._console.WriteLine(this._helpStyles.CommonStyles.Default, "All option switches are case sensitive until option explicitly states case alternatives.");

                if (this._options.AllowFlagsAsOneArgument && syntax.Count(s => s.OptionType == SyntaxOptionType.Flag) > 1)
                {
                    this._console.WriteText(this._helpStyles.CommonStyles.Default, "All flag-options being turned on can be also specified altogether, i. e. ");
                    string allFlagsText = string.Concat(syntax.Where(s => s.OptionType == SyntaxOptionType.Flag).Select(s => s.Literals.First()));
                    this._console.WriteText(this._helpStyles.HelpDefinition, $"{this._options.FlagCharacters.SelectRandom()}{allFlagsText}");
                    this._console.WriteText(this._helpStyles.CommonStyles.Default, string.Empty);
                }

                if (this._options.SwitchlessOptionsMode == SwitchlessOptionsMode.Mixed && syntax.Count(s => s.OptionType == SyntaxOptionType.Switchless) > 0)
                {
                    this._console.WriteLine(this._helpStyles.CommonStyles.Default, "All options that do not have option selector can be placed anywhere between other options (and their values). Only order is important.");
                    this._console.WriteLine(this._helpStyles.CommonStyles.Default, "Actual order of all OTHER option switches is unimportant.");
                }
                else
                {
                    this._console.WriteLine(this._helpStyles.CommonStyles.Default, "Actual order of all option switches is unimportant.");
                }
            }

            // TODO: larger command description itself - if available

            this._console.WriteLine(this._helpStyles.CommonStyles.Default, string.Empty);
        }