Example #1
0
        protected virtual void WriteSyntax(TextWriter writer, IEnumerable<ParameterSet> parameterSets, HelpLevel level)
        {
            const string indent = "   ";
            writer.WriteLine("SYNTAX");
            var entryAsm = Assembly.GetEntryAssembly();
            string fileName = "";
            if (entryAsm != null)
                fileName = Path.GetFileName(Assembly.GetEntryAssembly().Location);

            this.WriteSyntax(writer,
                             parameterSets,
                             indent,
                             fileName,
                             true,
                             p => true,
                             p =>
                             {
                                 if (p.Property.PropertyType == typeof(Switch))
                                     return "";

                                 if (p.Property.PropertyType == typeof(Switch))
                                     return "";

                                 if (level.HasFlag(HelpLevel.Detailed))
                                     return string.Format("<{0}>", p.Property.PropertyType.FullName);

                                 return string.Format("<{0}>", this.GetHumanReadableTypeName(p.Property.PropertyType));
                             });
        }
Example #2
0
        /// <summary>
        /// Writes a centered banner that separates groups of options.
        /// </summary>
        public void WriteBanner(string banner, HelpLevel level = HelpLevel.Standard)
        {
            Contract.Requires(banner != null);

            if (m_requestedLevel < level)
            {
                return;
            }

            m_writer.WriteLine();

            string fullBanner = "- " + banner + " -";
            int    avail      = m_characterWidth;

            if (avail > fullBanner.Length)
            {
                m_builder.Length = 0;
                m_builder.Append(' ', (avail - fullBanner.Length) / 2);
                m_builder.Append(fullBanner);
                m_writer.WriteLine(m_builder.ToString());
                m_builder.Length = 0;
            }
            else
            {
                m_writer.WriteLine(fullBanner);
            }

            m_writer.WriteLine();
        }
Example #3
0
        public virtual string GetUsage(HelpLevel level)
        {
            var sb = new StringBuilder();

            switch (level)
            {
            case HelpLevel.Summary:
                sb.Append($"{CommandName} - {Description}");
                break;

            case HelpLevel.Medium:
                sb.Append($"{CommandName} - ");
                ExpectedParameters.ForEach(p => sb.Append($"{p.Name} "));
                break;

            case HelpLevel.Full:
                sb.AppendLine($"{CommandName} - {Description}");
                sb.AppendLine("Parameters:");
                ExpectedParameters.ForEach(p => sb.AppendLine($"\t{p.Name} - {p.Description}"));
                break;

            default:
                throw new Exception($"Unhandled level {level}");
            }

            return(sb.ToString());
        }
Example #4
0
        /// <summary>
        /// Creates an instance of this object that outputs to the specified <see cref="TextWriter" />.
        /// </summary>
        /// <param name="writer">Where to send the output to.</param>
        /// <param name="characterWidth">The number of characters after which to perform word wrapping.</param>
        /// <param name="requestedLevel">The level of help being requested. Defaults to Standard</param>
        public HelpWriter(TextWriter writer, int characterWidth, HelpLevel requestedLevel = HelpLevel.Standard)
        {
            Contract.Requires(writer != null);

            m_writer         = writer;
            m_characterWidth = characterWidth;
            m_requestedLevel = requestedLevel;
        }
Example #5
0
        /// <summary>
        /// Writes a blank line to the console.
        /// </summary>
        public void WriteLine(HelpLevel level = HelpLevel.Standard)
        {
            if (m_requestedLevel < level)
            {
                return;
            }

            m_writer.WriteLine();
        }
Example #6
0
        /// <summary>
        /// Writes a word-wrapped line to the console.
        /// </summary>
        public void WriteLine(string line, HelpLevel level = HelpLevel.Standard)
        {
            Contract.Requires(line != null);

            if (m_requestedLevel < level)
            {
                return;
            }

            m_builder.Length = 0;
            WriteLine(line, 0);
        }
Example #7
0
        /// <summary>
        /// Writes an option and its description.
        /// </summary>
        public void WriteOption(string name, string description, HelpLevel level = HelpLevel.Standard, string shortName = null)
        {
            Contract.Requires(name != null);
            Contract.Requires(description != null);

            if (m_requestedLevel < level)
            {
                return;
            }

            if (shortName != null)
            {
                var shortForm = string.Format(CultureInfo.InvariantCulture, "(Short form: /{0})", shortName);
                if (string.IsNullOrEmpty(description))
                {
                    description = shortForm;
                }
                else
                {
                    description += " " + shortForm;
                }
            }

            if (string.IsNullOrEmpty(description))
            {
                m_writer.WriteLine(name);
                return;
            }

            m_builder.Length = 0;
            if (name.Length >= DescriptionColumn)
            {
                m_writer.WriteLine(name);
            }
            else
            {
                m_builder.Append(name);
            }

            WriteLine(description, DescriptionColumn);
        }
Example #8
0
        protected virtual void WriteExample(TextWriter writer, ParameterSetCollection parameterSets, HelpLevel level)
        {
            const string indent = "   ";
            writer.WriteLine("EXAMPLES");

            var entryAsm = Assembly.GetEntryAssembly();
            string fileName = "";
            if (entryAsm != null)
                fileName = Path.GetFileName(Assembly.GetEntryAssembly().Location);

            foreach (var set in parameterSets)
            {
                var examples =
                    set.SelectMany(p => p.GetAttributes<ExampleValueAttribute>()
                                         .Select(ev => new Tuple<Parameter, ExampleValueAttribute>(p, ev)))
                       .GroupBy(t => t.Item2.Set,
                                StringComparer.InvariantCultureIgnoreCase);

                examples = examples.Where(g => g.Any(t => t.Item1.Property.DeclaringType == set.UnderlyingType));

                foreach (IGrouping<string, Tuple<Parameter, ExampleValueAttribute>> group in examples)
                {
                    var attrDict = group.ToDictionary(t => t.Item1, t => t.Item2);
                    this.WriteSyntax(writer,
                                     new[] { set },
                                     indent,
                                     fileName,
                                     false,
                                     p =>
                                     {
                                         ExampleValueAttribute attr;
                                         if (attrDict.TryGetValue(p, out attr))
                                             return true;
                                         return p.GetAttribute<RequiredAttribute>() != null;
                                     },
                                     p =>
                                     {
                                         ExampleValueAttribute attr;
                                         if (attrDict.TryGetValue(p, out attr))
                                             return attr.Value;

                                         if (p.Property.PropertyType == typeof(Switch))
                                             return "";

                                         if (level.HasFlag(HelpLevel.Detailed))
                                             return string.Format("<{0}>", p.Property.PropertyType.FullName);

                                         return string.Format("<{0}>", this.GetHumanReadableTypeName(p.Property.PropertyType));
                                     });
                }
            }
        }
Example #9
0
        private void WriteParameters(TextWriter writer, ParameterSetCollection sets, HelpLevel level)
        {
            // TODO grouping by "name" alone is not good enough
            var parameters = sets.SelectMany(set => set).GroupBy(p => p.Name)
                                 .OrderBy(g => g.Key, StringComparer.InvariantCultureIgnoreCase)
                                 .Select(g => g.First());

            foreach (var parameter in parameters)
            {
                writer.WriteLine();
                this.WriteParameter(writer, sets, parameter);
            }
        }
Example #10
0
        public virtual void Write(TextWriter writer, ParameterSetCollection sets, HelpLevel level)
        {
            if (level.HasFlag(HelpLevel.Syntax))
            {
                this.WriteSyntax(writer, sets, level);
            }

            if (level.HasFlag(HelpLevel.Examples))
            {
                this.WriteExample(writer, sets, level);
            }

            if (level.HasFlag(HelpLevel.Parameters))
            {
                this.WriteParameters(writer, sets, level);
            }
        }
Example #11
0
 public virtual void Write(TextWriter writer, ParameterSet set, HelpLevel level)
 {
     var tmp = new ParameterSetCollection(new[] { set });
     this.Write(writer, tmp, level);
 }
Example #12
0
 /// <summary>
 /// Creates an instance of this object that outputs to the console.
 /// </summary>
 public HelpWriter(HelpLevel requestedLevel = HelpLevel.Standard)
 {
     m_requestedLevel = requestedLevel;
 }