示例#1
0
        /// <summary>
        /// Formats the given parameters as so:
        ///     -Param1     Param1 Description
        ///
        ///     -Param2      Param2 Description, this description is
        ///                  longer and splits onto a separate line.
        ///
        ///     -Param3      Param3 Description continues as before.
        /// </summary>
        /// <param name="Params">List of parameters arranged as "-ParamName Param Description"</param>
        /// <param name="Indent">Indent from the left hand side</param>
        /// <param name="DefaultRightPadding">The minimum padding from the start of the param name to the start of the description (resizes with larger param names)</param>
        /// <returns></returns>
        public static IEnumerable <string> FormatParams(IEnumerable <string> Params, int Indent, int DefaultRightPadding)
        {
            Dictionary <string, string> ParamDict = new Dictionary <string, string>(StringComparer.InvariantCultureIgnoreCase);

            // Extract Params/Descriptions into Key/Value pairs
            foreach (var Param in Params)
            {
                // Find the first space (should be following the param name)
                if (!String.IsNullOrWhiteSpace(Param))
                {
                    var ParamName = String.Empty;
                    var ParamDesc = String.Empty;

                    var SplitPoint = Param.IndexOf(' ');
                    if (SplitPoint > 0)
                    {
                        // Extract the name and description seperately
                        ParamName = Param.Substring(0, SplitPoint);
                        ParamDesc = Param.Substring(SplitPoint + 1, Param.Length - (SplitPoint + 1));
                    }
                    else
                    {
                        ParamName = Param;
                    }

                    // build dictionary using Name and Desc as Key and Value
                    if (!ParamDict.ContainsKey(ParamName))
                    {
                        ParamDict.Add(ParamName, ParamDesc);
                    }
                    else
                    {
                        LogWarning("Duplicated help parameter \"{0}\"", ParamName);
                    }
                }
            }

            // string used to intent the param
            string IndentString = string.Empty.PadRight(Indent);

            // default the padding value
            int RightPadding = DefaultRightPadding;

            // If (Padding < longest param name) padding = longest param name + 1
            foreach (var ParamName in ParamDict.Keys)
            {
                if (ParamName.Length + 1 > RightPadding)
                {
                    RightPadding = ParamName.Length + 1;
                }
            }

            // Get the window width, using a default value if there's no console attached to this process.
            int WindowWidth;

            try
            {
                WindowWidth = Console.WindowWidth;
            }
            catch
            {
                WindowWidth = 240;
            }

            // Build the formatted params
            foreach (var ParamName in ParamDict.Keys)
            {
                // build the param first, including intend and padding on the rights size
                string ParamString = IndentString + ParamName.PadRight(RightPadding);

                // Build the description line by line, adding the same amount of intending each time.
                foreach (var DescriptionLine in WordWrap(ParamDict[ParamName], WindowWidth - ParamString.Length))
                {
                    // Formatting as following:
                    // <Indent>-param<Right Padding>Description<New line>
                    yield return(ParamString + DescriptionLine);

                    // we replace the param string on subsequent lines with white space of the same length
                    ParamString = string.Empty.PadRight(IndentString.Length + RightPadding);
                }
            }
        }