Ejemplo n.º 1
0
        /// <summary>
        /// Print usage.
        /// </summary>
        public static string PrintUsage(
            [NotNull] this CmdLineRules rules,
            [NotNull] PrintUsageSettings settings)
        {
            var sw = new StringWriter();

            PrintUsage(rules, sw, settings);
            return(sw.ToString());
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Print usage.
        /// </summary>
        public static void PrintUsage(
            [NotNull] this CmdLineRules rules,
            [NotNull] TextWriter writer,
            [NotNull] PrintUsageSettings settings)
        {
            if (rules == null)
            {
                throw new ArgumentNullException(nameof(rules));
            }
            if (writer == null)
            {
                throw new ArgumentNullException(nameof(writer));
            }
            if (settings == null)
            {
                throw new ArgumentNullException(nameof(settings));
            }

            UsagePrinter.PrintUsage(rules, writer, settings);
        }
        public static void PrintUsage(CmdLineRules rules, TextWriter writer, PrintUsageSettings settings)
        {
            var titleExists = false;

            if (!settings.ProductNameString.IsNullOrEmpty())
            {
                writer.WriteLine(settings.ProductNameString);
                titleExists = true;
            }

            if (!settings.CopyrightString.IsNullOrEmpty())
            {
                writer.WriteLine(settings.CopyrightString);
                titleExists = true;
            }

            if (titleExists)
            {
                writer.WriteLine();
            }

            writer.Write("Usage: ");
            if (!settings.ProgramFileName.IsNullOrEmpty())
            {
                writer.Write(settings.ProgramFileName);
            }
            else
            {
                var entry = Assembly.GetEntryAssembly();
                if (entry == null)
                {
                    throw new ApplicationException("Could not retrieve program file name. Try to specify it in settings.");
                }
                writer.Write(entry.GetName(false).Name);
            }

            var hasCmds = rules.Commands.Length > 0;

            if (hasCmds)
            {
                writer.Write(" ");
                string prefix, suffix;
                switch (rules.CommandQuantifier)
                {
                case CommandQuantifier.ZeroOrOne:
                    prefix = "[";
                    suffix = "]";
                    break;

                case CommandQuantifier.ZeroOrMultiple:
                    prefix = "[";
                    suffix = "...]";
                    break;

                case CommandQuantifier.One:
                    prefix = "<";
                    suffix = ">";
                    break;

                case CommandQuantifier.OneOrMultiple:
                    prefix = "<";
                    suffix = "...>";
                    break;

                default:
                    throw new ArgumentOutOfRangeException();
                }
                writer.Write(
                    prefix
                    + (settings.RealCommandsInHeadLine
                                                ? rules.Commands.Select(cmd => cmd.Name).Join("|")
                                                : "command")
                    + suffix);
            }

            var    hasOpts = rules.Options.Length > 0;
            string optPrefix;

            switch (settings.OptionPrefix)
            {
            case OptionPrefix.Dash:
                optPrefix = "-";
                break;

            case OptionPrefix.Slash:
                optPrefix = "/";
                break;

            default:
                throw new ArgumentOutOfRangeException();
            }
            if (hasOpts)
            {
                writer.Write(
                    " [{0}...]",
                    settings.RealOptionsInHeadLine
                                                ? rules.Options.Select(opt => GetOptionString(opt, false, optPrefix)).Join("|")
                                                : "options");
            }

            writer.WriteLine();
            writer.WriteLine();

            if (hasCmds)
            {
                writer.WriteLine("                         - COMMANDS -");
                var cmdDescs =
                    rules
                    .Commands
                    .Select(cmd => new ItemDescriptor(cmd.Name, GetCommandDescription(cmd, rules, optPrefix)))
                    .ToList();
                var maxLen = cmdDescs.Max(desc => desc.Name.Length);
                foreach (var desc in cmdDescs)
                {
                    writer.WriteLine(" {0} - {1}", desc.Name.PadRight(maxLen), desc.Description);
                }
            }

            if (hasOpts)
            {
                writer.WriteLine("                         - OPTIONS -");
                var optDescs =
                    rules
                    .Options
                    .Select(opt => new ItemDescriptor(GetOptionString(opt, true, optPrefix), GetOptionDescription(opt)))
                    .ToList();
                var maxLen = optDescs.Max(desc => desc.Name.Length);
                foreach (var desc in optDescs)
                {
                    writer.WriteLine(" {0} - {1}", desc.Name.PadRight(maxLen), desc.Description);
                }
            }
        }