コード例 #1
0
        private bool IsGroupSelected(CommandLineGroup g)
        {
            // return true if the group that this argument belongs to has been selected by the
            // parse command line arguments.
            if (g.DependsOn != null)
            {
                var dependent = (from r in this.ParsedArguments where r.LongName == g.DependsOn.Name select r).FirstOrDefault();
                return(dependent != null && string.Compare(dependent.Value.ToString(), g.DependsOn.Value, StringComparison.OrdinalIgnoreCase) == 0);
            }

            return(false);
        }
コード例 #2
0
        /// <summary>
        /// Add a new command line group or return the existing group if it already exists.
        /// </summary>
        /// <param name="name">The name of the group.</param>
        /// <param name="description">The help text for the group.</param>
        /// <returns>The new command line group.</returns>
        public CommandLineGroup GetOrCreateGroup(string name, string description)
        {
            if (this.Groups.TryGetValue(name, out CommandLineGroup group))
            {
                return(group);
            }

            group = new CommandLineGroup(this, this.LongNames)
            {
                Name = name, Description = description
            };
            this.Groups.Add(name, group);
            this.GroupNames.Add(name);
            return(group);
        }
コード例 #3
0
        /// <summary>
        /// Shows help.
        /// </summary>
        public void PrintHelp(TextWriter output)
        {
            const int ArgHelpLineLength = 100;
            const int ArgHelpIndent     = 30;

            string prefix = string.Format("usage: {0} ", this.AppName);

            output.Write(prefix);
            int indent = prefix.Length;

            var wrapper = new WordWrapper(output, indent, ArgHelpLineLength);

            foreach (var name in this.PositionalNames)
            {
                var    arg  = this.Arguments[name];
                string text = arg.LongSyntax;
                if (arg.DependsOn != null)
                {
                    text = "[" + text + "]";
                }

                wrapper.WriteWord(text);
            }

            foreach (var name in this.LongNames)
            {
                var arg = this.Arguments[name];
                if (arg.IsHidden)
                {
                    continue;
                }

                string text = arg.LongSyntaxAndDataType;

                if (!arg.IsRequired)
                {
                    text = "[" + text + "]";
                }

                wrapper.WriteWord(text);
            }

            output.WriteLine();
            output.WriteLine();
            wrapper = new WordWrapper(output, 0, ArgHelpLineLength);
            wrapper.Write(this.AppDescription);
            output.WriteLine();
            output.WriteLine();
            var visitedOptions = new HashSet <string>();

            foreach (var name in this.GroupNames)
            {
                CommandLineGroup g = this.Groups[name];
                if (g.IsHidden)
                {
                    continue;
                }

                output.WriteLine(g.Description + ":");
                output.WriteLine(new string('-', g.Description.Length + 1));

                foreach (var option in this.PositionalNames.Concat(this.LongNames))
                {
                    var arg = this.Arguments[option];
                    if (arg.IsHidden)
                    {
                        continue;
                    }

                    if (arg.Group == name)
                    {
                        visitedOptions.Add(option);

                        string syntax = "  ";
                        if (!string.IsNullOrEmpty(arg.ShortName))
                        {
                            syntax += string.Format("{0}, ", arg.ShortSyntax);
                        }

                        syntax += string.Format("{0} ", arg.LongSyntaxAndDataType);

                        output.Write(syntax);
                        if (syntax.Length < ArgHelpIndent)
                        {
                            output.Write(new string(' ', ArgHelpIndent - syntax.Length));
                        }
                        else
                        {
                            output.WriteLine();
                            output.Write(new string(' ', ArgHelpIndent));
                        }

                        if (!string.IsNullOrEmpty(arg.Description))
                        {
                            output.Write(": ");
                            wrapper = new WordWrapper(output, ArgHelpIndent + 2, ArgHelpLineLength);
                            wrapper.Write(arg.Description);
                        }

                        output.WriteLine();
                    }
                }

                output.WriteLine();
            }

            bool optionalHeader = false;

            foreach (var option in this.PositionalNames.Concat(this.LongNames))
            {
                var arg = this.Arguments[option];
                if (arg.IsHidden)
                {
                    continue;
                }

                if (!visitedOptions.Contains(arg.LongName))
                {
                    if (!optionalHeader)
                    {
                        optionalHeader = true;
                        const string optionalBanner = "Optional Arguments:";
                        output.WriteLine(optionalBanner);
                        output.WriteLine(new string('-', optionalBanner.Length));
                    }

                    string syntax = "  ";
                    if (!string.IsNullOrEmpty(arg.ShortName))
                    {
                        syntax += string.Format("{0}, ", arg.ShortSyntax);
                    }

                    syntax += string.Format("{0} ", arg.LongSyntax);
                    output.Write(syntax);
                    if (syntax.Length < ArgHelpIndent)
                    {
                        output.Write(new string(' ', ArgHelpIndent - syntax.Length));
                    }
                    else
                    {
                        output.WriteLine();
                        output.Write(new string(' ', ArgHelpIndent));
                    }

                    if (!string.IsNullOrEmpty(arg.Description))
                    {
                        wrapper = new WordWrapper(output, ArgHelpIndent, ArgHelpLineLength);
                        wrapper.Write(arg.Description);
                    }

                    output.WriteLine();
                }
            }
        }