Пример #1
0
        private string getArgsUsage(ScriptContext context, int width, CommandLineParameters items)
        {
            List <Var> sb = new List <Var>();

            foreach (CommandLineParameter p in items)
            {
                string text     = p.GetTransformedValue(context);
                string vardescr = p.GetDescription(context);

                string name;
                bool   emptyName = (p.Switch ?? string.Empty).Trim().Length == 0;
                if (emptyName && string.IsNullOrEmpty(p.Var))
                {
                    name = p.Switch ?? string.Empty;
                }
                else
                {
                    if (string.IsNullOrEmpty(text))
                    {
                        continue;
                    }
                    if (emptyName)
                    {
                        name = " ";
                    }
                    else
                    {
                        var pp = items.GetSwitchPrefixesArray();
                        name = "  " + ((pp != null && pp.Length > 0) ? pp[0] : string.Empty) + p.Switch;
                    }
                }

                if (p.Count != CommandLineValueCount.None)
                {
                    if (p.Unspecified == null || emptyName)
                    {
                        if (!string.IsNullOrEmpty(vardescr))
                        {
                            name += " " + vardescr;
                        }
                    }
                    else
                    {
                        name += " [" + vardescr + "]";
                    }
                }

                StringBuilder vb = new StringBuilder();
                vb.Append(text);
                if ((Options & UsageOptions.AutoSuffix) != 0)
                {
                    if (p.Required)
                    {
                        vb.Append(" (required)");
                    }
                    else if (p.Default != null && p.Count != CommandLineValueCount.None)
                    {
                        string def = Utils.To <string>(context.Transform(p.Default, p.Transform));
                        vb.Append(" (default: " + def + ")");
                    }
                }
                Var v = new Var(name, vb.ToString());
                sb.Add(v);
            }

            StringWriter sw = new StringWriter();

            Utils.WrapTwoColumns(sw, sb, 30, width);
            return(sw.ToString());
        }
Пример #2
0
        /// <summary>
        /// Generate usage text for the script with given ID and descriptions
        /// </summary>
        /// <param name="context">Script context</param>
        /// <param name="description">Script description</param>
        /// <param name="id">Script ID</param>
        /// <param name="width">Console width</param>
        /// <param name="items">Command line parameters</param>
        /// <returns></returns>
        public string GetUsage(ScriptContext context, string description, string id, int width, CommandLineParameters items)
        {
            width = CorrectWidth(width);

            StringWriter sw = new StringWriter();

            // Write description
            if (!string.IsNullOrEmpty(description))
            {
                Utils.Wrap(sw, description, width, string.Empty);
                sw.WriteLine();
            }

            if ((Options & UsageOptions.UsageLine) != 0)
            {
                string prefix = LinePrefix + id;

                StringBuilder line     = new StringBuilder(prefix);
                bool          optional = false;
                foreach (CommandLineParameter p in items)
                {
                    string text     = p.GetTransformedValue(context);
                    string vardescr = p.GetDescription(context);
                    if (p.Required)
                    {
                        if (!string.IsNullOrEmpty(p.Switch))
                        {
                            line.Append(" " + items.GetSwitchPrefixesArray()[0] + p.Switch);
                        }
                        if (p.Count != CommandLineValueCount.None)
                        {
                            line.Append(" " + p.GetDescription(context));
                        }
                    }
                    else if (!string.IsNullOrEmpty(text) && !string.IsNullOrEmpty(p.Switch))
                    {
                        optional = true;
                    }
                    else if (p.Count != CommandLineValueCount.None && string.IsNullOrEmpty(p.Switch) && !string.IsNullOrEmpty(vardescr))
                    {
                        line.Append(" [" + vardescr + "]");
                    }
                }

                if (optional && LineSuffix == null)
                {
                    line.Append(" [parameters]");
                }
                line.Append(LineSuffix);
                Utils.Wrap(sw, line.ToString(), width, new string(' ', prefix.Length + 1));
            }

            foreach (CommandLineParameter p in items)
            {
                string text = p.GetTransformedValue(context);
                if (!string.IsNullOrEmpty(text) &&
                    ((p.Switch ?? string.Empty).Trim().Length != 0 ||
                     (!string.IsNullOrEmpty(p.Var) && !string.IsNullOrEmpty(p.Value))))
                {
                    if ((Options & UsageOptions.UsageLine) != 0)
                    {
                        sw.WriteLine();
                    }
                    break;
                }
            }
            sw.Write(getArgsUsage(context, width, items));
            return(sw.ToString());
        }