示例#1
0
        public string GetHelp(IParserConfig config)
        {
            var sb = new StringBuilder();

            string descr = GetApplicationDescription();

            if (!string.IsNullOrWhiteSpace(descr))
            {
                sb.AppendLine(descr);
                sb.AppendLine();
            }

            sb.Append(GetUsage(config));
            sb.AppendLine();
            sb.AppendLine();
            sb.AppendLine("commands:");

            foreach (PropertyInfo verb in typeof(T).GetProperties())
            {
                var attrib = GetAttribute <VerbAttribute>(verb);

                if (attrib != null)
                {
                    PropertyInfo[] properties = verb.PropertyType.GetProperties();
                    AppendVerbHelpText(sb, attrib, config.ArgumentPrefix, properties);
                }
            }

            if (!string.IsNullOrWhiteSpace(GenericDescription))
            {
                sb.AppendLine(GenericDescription);
            }

            return(sb.ToString());
        }
示例#2
0
 public RazorTemplatePaser(IParserConfig parseConfig)
 {
     config       = new TemplateServiceConfiguration();
     config.Debug = parseConfig.EnableDebug;
     //config.TemporaryDirectory
     config.Language = Language.CSharp;
     config.DisableTempFileLocking = true;//as well. This will work in any AppDomain (including the default one)
     if (config.Debug)
     {
         config.DisableTempFileLocking = false;
     }
     //config.CachingProvider = new DefaultCachingProvider(t => { });
     config.CachingProvider      = new DefaultCachingProvider();
     config.EncodedStringFactory = new RazorEngine.Text.RawStringFactory(); // Raw string encoding.
     config.ReferenceResolver    = new LocalReferenceResolver();
     //DelegateTemplateManager: (default) Used as the default for historical reasons, easy solution when using dynamic template razor strings.
     //ResolvePathTemplateManager: Used to resolve templates from a given list of directory paths. Doesn't support adding templates dynamically via string. You can use a full path instead of a template name.
     //EmbeddedResourceTemplateManager: Used to resolve templates from assembly embedded resources. Uses Assembly.GetManifestResourceStream(Type, string) to load the template based on the type provided.
     //WatchingResolvePathTemplateManager: Same as ResolvePathTemplateManager but watches the filesystem and invalidates the cache. Note that this introduces a memory leak to your application, so only use this is you have an AppDomain recycle strategy in place or for debugging purposes.
     templateManager        = new DelegateTemplateManager();
     config.TemplateManager = templateManager;
     //config.EncodedStringFactory = new RazorEngine.Text.HtmlEncodedStringFactory(); // Html encoding.
     if (parseConfig.EnableSandbox)
     {
         var service = IsolatedRazorEngineService.Create(new SandBoxConfigCreator(config), SandBox.SandboxCreator);
         Engine.Razor = service;
     }
     else
     {
         var service = RazorEngineService.Create(config);
         Engine.Razor = service;
     }
 }
示例#3
0
        //public static string GetModuleDirectory(GeneraterConfig config)
        //{
        //    string dataFilePath = System.IO.Path.Combine(GetDataDirectory(), config.OutputDirectory);
        //    if (!Directory.Exists(dataFilePath))
        //    {
        //        Directory.CreateDirectory(dataFilePath);
        //    }
        //    return dataFilePath;
        //}
        #endregion

        #region Template Engine

        public static ITemplateParser CreateTemplateEngine(IParserConfig parseConfig = null)
        {
            if (parseConfig == null)
            {
                parseConfig = new DefaultParserConfig();
            }
            return(new RazorTemplatePaser(parseConfig));
        }
示例#4
0
 public LocalGenerator(IDatabase db, IParserConfig parseConfig)
 {
     _parseConfig = parseConfig;
     debugger     = new DefaultDebuger(db);
     parser       = DbLoader.CreateTemplateEngine(parseConfig);// new RazorTemplatePaser(parseConfig);
     // parser = new LiquidTemplatePaser(parseConfig);
     LogReport += (msg) => debugger.WriteLine(msg);
 }
示例#5
0
        public ParsingContext(T obj, IParserConfig config)
        {
            Object = obj;
            Config = config;

            _parsedMutuallyExclusiveGroups = new HashSet <string>();
            _parsedNamedArguments          = new HashSet <string>();
        }
示例#6
0
 public ParserString(IParserConfig parserSettings)
 {
     measurement           = parserSettings.measurement;
     headerRow             = parserSettings.headerRow;
     headerExtra           = parserSettings.headerExtra;
     tagIndexes            = parserSettings.tagIndexes;
     fieldIndexes          = parserSettings.fieldIndexes;
     timeIndex             = parserSettings.timeIndex;
     timeFormat            = parserSettings.timeFormat;
     timeFormatTimescaleDB = parserSettings.timeFormatTimescaleDB;
     columnSeparator       = parserSettings.columnSeparator;
 }
示例#7
0
        private static List <string> GetArgumentNames(INamedArgument attr, IParserConfig config)
        {
            var ret = new List <string>();

            if (attr.ShortName != null)
            {
                ret.Add("" + config.ArgumentPrefix + attr.ShortName);
            }
            if (attr.LongName != null)
            {
                ret.Add("" + config.ArgumentPrefix + config.ArgumentPrefix + attr.LongName);
            }
            return(ret);
        }
示例#8
0
        public VerbParserConfig(IParserConfig internalParserConfig, IValueStoreDefinition store, ParserOptions options)
            : base(options, null)
        {
            InternalParserConfig = internalParserConfig;
            Store = store;

            LongNameArguments   = InternalParserConfig.LongNameArguments;
            ShortNameArguments  = InternalParserConfig.ShortNameArguments;
            PositionalArguments = InternalParserConfig.PositionalArguments;
            Verbs            = InternalParserConfig.Verbs;
            PostParseMethods = InternalParserConfig.PostParseMethods;
            RequiredMutuallyExclusiveArguments = InternalParserConfig.RequiredMutuallyExclusiveArguments;
            RequiredNamedArguments             = InternalParserConfig.RequiredNamedArguments;
        }
示例#9
0
        public string GetHelp(IParserConfig <Verbs> config)
        {
            var sb = new StringBuilder();

            sb.Append(GetUsage());
            sb.AppendLine();
            sb.AppendLine();
            sb.AppendLine("commands:");
            sb.AppendLine(" * ec-dump");
            sb.AppendLine("    Dump all registers that are present in EC memory as hexadecimal table.");
            sb.AppendLine(" * ec-read <register>");
            sb.AppendLine("    Print the value of the given EC register number.");
            sb.AppendLine(" * ec-write <register> <value> [-v|--verbose]");
            sb.AppendLine("    Write the given value to the given EC register number.");
            sb.AppendLine("    If the verbose option is set, read and print the updated value.");
            sb.AppendLine();
            sb.AppendLine("Input values are interpreted as decimal numbers by default.");
            sb.AppendLine("Hexadecimal values may be entered by prefixing them with \"0x\".");

            return(sb.ToString());
        }
示例#10
0
        public string GetHelp(IParserConfig <Verbs> config)
        {
            var sb = new StringBuilder();

            sb.Append(GetUsage());
            sb.AppendLine();
            sb.AppendLine();
            sb.AppendLine("commands:");

            foreach (PropertyInfo verb in typeof(Verbs).GetProperties())
            {
                var attrib = verb.GetCustomAttributes(typeof(VerbAttribute), false)
                             .FirstOrDefault() as VerbAttribute;

                if (attrib != null)
                {
                    PropertyInfo[] properties = verb.PropertyType.GetProperties();
                    AppendVerbHelpText(sb, attrib, config.ArgumentPrefix, properties);
                }
            }

            return(sb.ToString());
        }
示例#11
0
 public string GetHelp(IParserConfig config)
 {
     throw new NotImplementedException();
 }
示例#12
0
        /// <inheritdoc/>
        public string GetHelp(IParserConfig config)
        {
            var lineWidth      = (DisplayWidth ?? DisplayWidth.Default).Width;
            var positionalArgs = config.PositionalArguments.ToList();

            var namedArgs = config.LongNameArguments.Values.Cast <INamedArgument>()
                            .Concat(config.ShortNameArguments.Values.Cast <INamedArgument>())
                            .Distinct()
                            .ToList();

            var positionalDisplay = positionalArgs
                                    .OrderBy(_argumentIndex)
                                    .Select(GetArgumentsForDisplay).ToList();

            var namedDisplay = namedArgs
                               .OrderBy(_argumentDisplayName)
                               .Select(GetArgumentsForDisplay).ToList();

            var tabstop = positionalDisplay
                          .Concat(namedDisplay)
                          .Select(a => a.ArgumentNames.Length)
                          .Max() + Indent.Length + Spacing;

            var helpDataBuilder = new StringBuilder();

            helpDataBuilder.AppendLine(GetUsage(config));
            var metadata = typeof(T).GetTypeInfo().GetCustomAttribute <ApplicationInfoAttribute>();

            if (metadata != null && metadata.Description != null)
            {
                helpDataBuilder.AppendLine();
                foreach (var line in metadata.Description.ReflowWords(lineWidth))
                {
                    helpDataBuilder.Append(Indent);
                    helpDataBuilder.AppendLine(line);
                }
                helpDataBuilder.AppendLine();
            }

            if (positionalArgs.Any())
            {
                helpDataBuilder.AppendLine(PositionalArgumentsTitle + ":");
            }
            foreach (var arg in positionalDisplay)
            {
                helpDataBuilder.Append(Indent);
                helpDataBuilder.Append(arg.ArgumentNames.PadRight(
                                           tabstop - Indent.Length));
                if (tabstop + arg.Description.Length < lineWidth)
                {
                    helpDataBuilder.AppendLine(arg.Description);
                }
                else
                {
                    var iter = arg.Description.ReflowWords(
                        lineWidth - tabstop).GetEnumerator();
                    iter.MoveNext();
                    helpDataBuilder.AppendLine(iter.Current);
                    while (iter.MoveNext())
                    {
                        if (iter.Current == null)
                        {
                            continue;
                        }

                        helpDataBuilder.AppendLine(
                            iter.Current.PadLeft(lineWidth));
                    }
                }
            }

            if (namedArgs.Any())
            {
                if (positionalArgs.Any())
                {
                    helpDataBuilder.AppendLine();
                }
                helpDataBuilder.AppendLine(OptionalArgumentsTitle + ":");
            }
            foreach (var arg in namedDisplay)
            {
                helpDataBuilder.Append(Indent);
                helpDataBuilder.Append(arg.ArgumentNames.PadRight(
                                           tabstop - Indent.Length));
                if (tabstop + arg.Description.Length < lineWidth)
                {
                    helpDataBuilder.AppendLine(arg.Description);
                }
                else
                {
                    var iter = arg.Description.ReflowWords(
                        lineWidth - tabstop).GetEnumerator();
                    iter.MoveNext();
                    helpDataBuilder.AppendLine(iter.Current);
                    while (iter.MoveNext())
                    {
                        helpDataBuilder.Append("".PadRight(tabstop));
                        helpDataBuilder.AppendLine(iter.Current);
                    }
                }
            }

            return(helpDataBuilder.ToString().TrimEnd());
        }
示例#13
0
        private static ArgumentDisplay GetArgumentsForDisplay(INamedArgument attr, IParserConfig config)
        {
            var names = GetArgumentNames(attr, config);

            return(GetArgumentsForDisplay(names, attr));
        }
示例#14
0
 /// <summary>
 /// Action to perform when this trigger is parsed.
 /// </summary>
 /// <param name="config"></param>
 public void OnParse(IParserConfig config)
 {
     (config.Options.OutputWriter
      ?? DefaultHelpWriter).WriteLine(GetHelp(config));
 }
示例#15
0
        /// <inheritdoc/>
        public string GetUsage(IParserConfig config)
        {
            var assembly = Assembly.GetEntryAssembly();
            var builder  = new StringBuilder();

            builder.Append(UsageTitle);
            builder.Append(": ");
            builder.Append(Path.GetFileNameWithoutExtension(assembly != null ?
                                                            assembly.Location : typeof(AutomaticHelpGenerator <T>).GetTypeInfo().Assembly.CodeBase));
            builder.Append(" ");

            // TODO list PromptIfValueMissing help message on each argument that can be prompted.
            foreach (var arg in config.LongNameArguments.Values.Cast <INamedArgument>()
                     .Concat(config.ShortNameArguments.Values.Cast <INamedArgument>())
                     .Distinct())
            {
                builder.Append("[ ");
                builder.Append(String.Join("|", GetArgumentNames(arg).ToArray()));
                builder.Append(" ");

                if (arg.Action.ConsumesArgumentValues())
                {
                    if (arg.Constraint == NumArgsConstraint.AtMost ||
                        (arg.Constraint == NumArgsConstraint.AtLeast && arg.NumArgs == 0))
                    {
                        builder.Append("[ ");
                    }

                    for (var i = 0; i < Math.Max(1, arg.NumArgs); i++)
                    {
                        if (arg.MetaVar == null)
                        {
                            continue;
                        }
                        builder.Append(arg.MetaVar.ToUpperInvariant());
                        builder.Append(" ");
                    }

                    if (arg.Constraint == NumArgsConstraint.AtLeast)
                    {
                        builder.Append("... ");
                    }

                    if (arg.Constraint == NumArgsConstraint.AtMost ||
                        (arg.Constraint == NumArgsConstraint.AtLeast && arg.NumArgs == 0))
                    {
                        builder.Append("] ");
                    }
                }

                builder.Append("] ");
            }

            foreach (var arg in config.PositionalArguments.OrderBy(p => p.Index))
            {
                if (arg.Constraint == NumArgsConstraint.AtMost ||
                    (arg.Constraint == NumArgsConstraint.AtLeast && arg.NumArgs == 0))
                {
                    builder.Append("[ ");
                }

                for (var i = 0; i < arg.NumArgs; i++)
                {
                    if (arg.MetaVar == null)
                    {
                        continue;
                    }
                    builder.Append(arg.MetaVar.ToUpperInvariant());
                    builder.Append(" ");
                }

                var store = arg.Store;

                if (EnumUtils.IsEnum(store))
                {
                    var names = EnumUtils.GetEnumValues(store);
                    if (names.Length > 0)
                    {
                        builder.Append("{");
                        builder.Append(String.Join("|", names));
                        builder.Append("}");
                    }
                }

                if (arg.Constraint == NumArgsConstraint.AtLeast)
                {
                    builder.Append("... ");
                }

                if (arg.Constraint == NumArgsConstraint.AtMost ||
                    (arg.Constraint == NumArgsConstraint.AtLeast && arg.NumArgs == 0))
                {
                    builder.Append("] ");
                }
            }

            return(builder.ToString());
        }
示例#16
0
 /// <summary>
 /// Action to perform when trigger is parsed.
 /// </summary>
 /// <param name="config"></param>
 public void OnParse(IParserConfig config)
 {
     Console.Error.WriteLine(GetVersion());
 }
示例#17
0
 public ParserWorker(IParser <T> parser, IParserConfig config)
 {
     Parser = parser;
     Config = config;
 }
示例#18
0
        /// <inheritdoc/>
        public string GetHelp(IParserConfig config)
        {
            var lineWidth      = (DisplayWidth ?? DisplayWidth.Default).Width;
            var positionalArgs = config.PositionalArguments.ToList();

            var namedArgs = config.LongNameArguments.Values.Cast <INamedArgument>()
                            .Concat(config.ShortNameArguments.Values.Cast <INamedArgument>())
                            .Distinct()
                            .ToList();

            var namedRequiredArgs = namedArgs.Where(a => a.Required).ToList();
            var namedOptionalArgs = namedArgs.Where(a => !a.Required).ToList();

            var positionalDisplay = positionalArgs
                                    .OrderBy(_argumentIndex)
                                    .Select(GetArgumentsForDisplay).ToList();

            var namedRequiredDisplay = namedRequiredArgs
                                       .OrderBy(_argumentDisplayName)
                                       .Select(r => GetArgumentsForDisplay(r, config)).ToList();

            var namedOptionalDisplay = namedOptionalArgs
                                       .OrderBy(_argumentDisplayName)
                                       .Select(o => GetArgumentsForDisplay(o, config)).ToList();

            var tabstop = positionalDisplay
                          .Concat(namedRequiredDisplay)
                          .Concat(namedOptionalDisplay)
                          .Select(a => a.ArgumentNames.Length)
                          .Max() + Indent.Length + Spacing;

            var helpDataBuilder = new StringBuilder();

            helpDataBuilder.AppendLine(GetUsage(config));
            var metadata = typeof(T).GetTypeInfo().GetCustomAttribute <ApplicationInfoAttribute>();

            if (metadata != null && metadata.Description != null)
            {
                helpDataBuilder.AppendLine();
                foreach (var line in metadata.Description.ReflowWords(lineWidth))
                {
                    helpDataBuilder.Append(Indent);
                    helpDataBuilder.AppendLine(line);
                }
                helpDataBuilder.AppendLine();
            }

            if (positionalArgs.Any())
            {
                helpDataBuilder.AppendLine(PositionalArgumentsTitle + ":");
            }
            foreach (var arg in positionalDisplay)
            {
                helpDataBuilder.Append(Indent);
                // No padding needed if there is no description
                if (String.IsNullOrEmpty(arg.Description))
                {
                    helpDataBuilder.AppendLine(arg.ArgumentNames);
                    continue;
                }
                helpDataBuilder.Append(arg.ArgumentNames.PadRight(
                                           tabstop - Indent.Length));
                if (tabstop + arg.Description.Length < lineWidth)
                {
                    helpDataBuilder.AppendLine(arg.Description);
                }
                else
                {
                    var iter = arg.Description.ReflowWords(
                        lineWidth - tabstop).GetEnumerator();
                    iter.MoveNext();
                    helpDataBuilder.AppendLine(iter.Current);
                    while (iter.MoveNext())
                    {
                        if (iter.Current == null)
                        {
                            continue;
                        }

                        helpDataBuilder.AppendLine(
                            iter.Current.PadLeft(lineWidth));
                    }
                }
            }

            var prevSection = positionalArgs.Any();

            AppendNamedArgumentList(helpDataBuilder, RequiredNamedArgumentsTitle, namedRequiredDisplay, tabstop, lineWidth, prevSection);
            prevSection |= namedRequiredDisplay.Any();
            AppendNamedArgumentList(helpDataBuilder, OptionalNamedArgumentsTitle, namedOptionalDisplay, tabstop, lineWidth, prevSection);

            if (config.Verbs.Any())
            {
                helpDataBuilder.AppendLine();
                helpDataBuilder.Append(CommandsTitle);
                helpDataBuilder.AppendLine(":");
            }
            foreach (var verb in config.Verbs.Values.OrderBy(v => v.Name))
            {
                helpDataBuilder.Append(Indent);
                // No padding needed if there is no description
                if (String.IsNullOrEmpty(verb.Description))
                {
                    helpDataBuilder.AppendLine(verb.Name);
                    continue;
                }
                helpDataBuilder.Append(verb.Name.PadRight(
                                           tabstop - Indent.Length));
                if (tabstop + verb.Description.Length < lineWidth)
                {
                    helpDataBuilder.AppendLine(verb.Description);
                }
                else
                {
                    var iter = verb.Description.ReflowWords(
                        lineWidth - tabstop).GetEnumerator();
                    iter.MoveNext();
                    helpDataBuilder.AppendLine(iter.Current);
                    while (iter.MoveNext())
                    {
                        if (iter.Current == null)
                        {
                            continue;
                        }

                        helpDataBuilder.AppendLine(
                            iter.Current.PadLeft(lineWidth));
                    }
                }
            }

            return(helpDataBuilder.ToString().TrimEnd());
        }
示例#19
0
        /// <summary>
        /// Get all help information.
        /// </summary>
        /// <param name="config"></param>
        /// <returns></returns>
        public string GetHelp(IParserConfig <T> config)
        {
            var positionalArgs = typeof(T).GetProperties()
                                 .Where(p => p.GetCustomAttribute <PositionalArgumentAttribute>() != null)
                                 .Select(p => p.GetCustomAttribute <PositionalArgumentAttribute>() as IPositionalArgument)
                                 .ToList();

            var namedArgs = typeof(T).GetProperties()
                            .Where(p => p.GetCustomAttribute <NamedArgumentAttribute>() != null)
                            .Select(p => p.GetCustomAttribute <NamedArgumentAttribute>() as INamedArgumentBase)
                            .ToList();

            foreach (var trigger in config.Triggers)
            {
                var names = new List <string>();
                if (trigger.ShortName.HasValue)
                {
                    names.Add("-" + trigger.ShortName);
                }
                if (trigger.LongName != null)
                {
                    names.Add("--" + trigger.LongName);
                }
                if (names.Any())
                {
                    namedArgs.Add(trigger);
                }
            }

            var positionalDisplay = positionalArgs
                                    .OrderBy(_argumentIndex)
                                    .Select(GetArgumentsForDisplay).ToList();

            var namedDisplay = namedArgs
                               .OrderBy(_argumentDisplayName)
                               .Select(GetArgumentsForDisplay).ToList();

            var tabstop = positionalDisplay
                          .Concat(namedDisplay)
                          .Select(a => a.ArgumentNames.Length)
                          .Max() + Indent.Length + Spacing;

            var helpDataBuilder = new StringBuilder();

            helpDataBuilder.AppendLine(GetUsage());

            var metadata = typeof(T).GetCustomAttribute <ApplicationInfoAttribute>();

            if (metadata != null && metadata.Description != null)
            {
                helpDataBuilder.AppendLine();
                foreach (var line in metadata.Description.ReflowWords(LineWidth))
                {
                    helpDataBuilder.Append(Indent);
                    helpDataBuilder.AppendLine(line);
                }
                helpDataBuilder.AppendLine();
            }

            if (positionalArgs.Any())
            {
                helpDataBuilder.AppendLine(PositionalArgumentsTitle + ":");
            }
            foreach (var arg in positionalDisplay)
            {
                helpDataBuilder.Append(Indent);
                helpDataBuilder.Append(arg.ArgumentNames.PadRight(
                                           tabstop - Indent.Length));
                if (tabstop + arg.Description.Length < LineWidth)
                {
                    helpDataBuilder.AppendLine(arg.Description);
                }
                else
                {
                    var iter = arg.Description.ReflowWords(
                        LineWidth - tabstop).GetEnumerator();
                    iter.MoveNext();
                    helpDataBuilder.AppendLine(iter.Current);
                    while (iter.MoveNext())
                    {
                        if (iter.Current == null)
                        {
                            continue;
                        }

                        helpDataBuilder.AppendLine(
                            iter.Current.PadLeft(LineWidth));
                    }
                }
            }

            if (namedArgs.Any())
            {
                if (positionalArgs.Any())
                {
                    helpDataBuilder.AppendLine();
                }
                helpDataBuilder.AppendLine(OptionalArgumentsTitle + ":");
            }
            foreach (var arg in namedDisplay)
            {
                helpDataBuilder.Append(Indent);
                helpDataBuilder.Append(arg.ArgumentNames.PadRight(
                                           tabstop - Indent.Length));
                if (tabstop + arg.Description.Length < LineWidth)
                {
                    helpDataBuilder.AppendLine(arg.Description);
                }
                else
                {
                    var iter = arg.Description.ReflowWords(
                        LineWidth - tabstop).GetEnumerator();
                    iter.MoveNext();
                    helpDataBuilder.AppendLine(iter.Current);
                    while (iter.MoveNext())
                    {
                        helpDataBuilder.Append("".PadRight(tabstop));
                        helpDataBuilder.AppendLine(iter.Current);
                    }
                }
            }

            return(helpDataBuilder.ToString().TrimEnd());
        }
示例#20
0
 public void OnParse(IParserConfig config)
 {
     throw new NotImplementedException();
 }
示例#21
0
 /// <summary>
 /// Action to perform when this trigger is parsed.
 /// </summary>
 /// <param name="config"></param>
 public void OnParse(IParserConfig <T> config)
 {
     Console.Error.WriteLine(GetHelp(config));
 }
示例#22
0
 public HtmlLoader(IParserConfig config)
 {
     client = new HttpClient();
     url    = $"{config.Url}/{config.Prefix}";
 }
示例#23
0
        public string GetUsage(IParserConfig config)
        {
            var attrib = GetAttribute <ApplicationInfoAttribute>(typeof(T));

            return(string.Format("usage: {0} [--version] [--help] <command> [<args>]", attrib.Name));
        }
示例#24
0
 /// <summary>
 /// Action to perform when trigger is parsed.
 /// </summary>
 /// <param name="config"></param>
 public void OnParse(IParserConfig config)
 {
     (config.Options.OutputWriter
      ?? DefaultWriter).WriteLine(GetVersion());
 }
示例#25
0
        /// <inheritdoc/>
        public string GetUsage(IParserConfig config)
        {
            var assembly = Assembly.GetEntryAssembly();
            var builder  = new StringBuilder();

            builder.Append(UsageTitle);
            builder.Append(": ");
            builder.Append(Path.GetFileNameWithoutExtension(assembly != null ?
                                                            assembly.Location : Assembly.GetExecutingAssembly().CodeBase));
            builder.Append(" ");

            foreach (var arg in config.LongNameArguments.Values.Cast <INamedArgument>()
                     .Concat(config.ShortNameArguments.Values.Cast <INamedArgument>())
                     .Distinct())
            {
                builder.Append("[ ");
                builder.Append(String.Join("|", GetArgumentNames(arg).ToArray()));
                builder.Append(" ");

                if (arg.Action.ConsumesArgumentValues())
                {
                    if (arg.Constraint == NumArgsConstraint.AtMost)
                    {
                        builder.Append("[ ");
                    }

                    for (var i = 0; i < arg.NumArgs; i++)
                    {
                        if (arg.MetaVar == null)
                        {
                            continue;
                        }
                        builder.Append(arg.MetaVar.ToUpper(CultureInfo.CurrentCulture));
                        builder.Append(" ");
                    }

                    if (arg.Constraint == NumArgsConstraint.AtLeast)
                    {
                        builder.Append("... ");
                    }

                    if (arg.Constraint == NumArgsConstraint.AtMost)
                    {
                        builder.Append("] ");
                    }
                }

                builder.Append("] ");
            }

            foreach (var arg in config.PositionalArguments.OrderBy(p => p.Index))
            {
                if (arg.Constraint == NumArgsConstraint.AtMost)
                {
                    builder.Append("[ ");
                }

                for (var i = 0; i < arg.NumArgs; i++)
                {
                    if (arg.MetaVar == null)
                    {
                        continue;
                    }
                    builder.Append(arg.MetaVar.ToUpperInvariant());
                    builder.Append(" ");
                }

                var store = arg.Store;

                if (store.Type.IsSubclassOf(typeof(Enum)))
                {
                    AddEnumFormat(builder, store);
                }
                if (store.Type.GetCustomAttribute <StaticEnumerationAttribute>() != null)
                {
                    AddStaticEnumFormat(builder, store);
                }

                var staticEnum = (store.GetCustomAttribute <StaticEnumerationAttribute>() ??
                                  store.Type.GetCustomAttribute <StaticEnumerationAttribute>()) != null;
                if (staticEnum)
                {
                    AddStaticEnumFormat(builder, store);
                }

                if (arg.Constraint == NumArgsConstraint.AtLeast)
                {
                    builder.Append("... ");
                }

                if (arg.Constraint == NumArgsConstraint.AtMost)
                {
                    builder.Append("] ");
                }
            }

            return(builder.ToString());
        }
示例#26
0
        /// <inheritdoc/>
        public string GetUsage(IParserConfig config)
        {
            var assembly = Assembly.GetEntryAssembly();
            var builder  = new StringBuilder();

            builder.Append(UsageTitle);
            builder.Append(": ");
            builder.Append(Path.GetFileNameWithoutExtension(assembly != null ?
                                                            assembly.Location : typeof(AutomaticHelpGenerator <T>).GetTypeInfo().Assembly.CodeBase));

            // Append the verbs in case
            var verbConfig = config as IVerbParserConfig;

            if (verbConfig != null && verbConfig.PrecursorVerbs != null)
            {
                foreach (var pre in verbConfig.PrecursorVerbs)
                {
                    builder.Append(" ");
                    builder.Append(pre);
                }
            }

            builder.Append(" ");

            // TODO list PromptIfValueMissing help message on each argument that can be prompted.
            var allNamedArgs = config.LongNameArguments.Values.Cast <INamedArgument>()
                               .Concat(config.ShortNameArguments.Values.Cast <INamedArgument>())
                               .Distinct().ToList();

            for (var i = 0; i < allNamedArgs.Count; i++)
            {
                var arg = allNamedArgs[i];
                if (i > 0)
                {
                    builder.Append(" ");
                }
                if (!arg.Required)
                {
                    builder.Append("[ ");
                }
                builder.Append(String.Join("|", GetArgumentNames(arg, config).ToArray()));

                if (arg.Action.ConsumesArgumentValues())
                {
                    if (arg.Constraint == NumArgsConstraint.AtMost ||
                        (arg.Constraint == NumArgsConstraint.AtLeast && arg.NumArgs == 0))
                    {
                        builder.Append(" [ ");
                    }

                    for (var j = Math.Max(1, (int)arg.NumArgs) - 1; j >= 0; j--)
                    {
                        if (arg.MetaVar == null)
                        {
                            continue;
                        }
                        builder.Append(" ");
                        builder.Append(arg.MetaVar.ToUpperInvariant());
                    }

                    if (arg.Constraint == NumArgsConstraint.AtLeast)
                    {
                        builder.Append(" ...");
                    }

                    if (arg.Constraint == NumArgsConstraint.AtMost ||
                        (arg.Constraint == NumArgsConstraint.AtLeast && arg.NumArgs == 0))
                    {
                        builder.Append(" ]");
                    }
                }
                if (!arg.Required)
                {
                    builder.Append(" ]");
                }
            }

            if (config.PositionalArguments.Any())
            {
                builder.Append(" ");
            }
            var allPositionalArgs = config.PositionalArguments.OrderBy(p => p.Index).ToList();

            for (var i = 0; i < allPositionalArgs.Count; i++)
            {
                var arg = allPositionalArgs[i];
                if (arg.Constraint == NumArgsConstraint.AtMost ||
                    (arg.Constraint == NumArgsConstraint.AtLeast && arg.NumArgs == 0))
                {
                    builder.Append("[ ");
                }

                for (var j = 0; j < arg.NumArgs; j++)
                {
                    if (arg.MetaVar == null)
                    {
                        continue;
                    }
                    builder.Append(arg.MetaVar.ToUpperInvariant());
                    if (i < allPositionalArgs.Count - 1)
                    {
                        builder.Append(" ");
                    }
                }

                var store = arg.Store;

                if (EnumUtils.IsEnum(store))
                {
                    var names = EnumUtils.GetEnumValues(store);
                    if (names.Length > 0)
                    {
                        builder.Append("{");
                        builder.Append(String.Join("|", names));
                        builder.Append("}");
                    }
                }

                if (arg.Constraint == NumArgsConstraint.AtLeast)
                {
                    builder.Append("... ");
                }

                if (arg.Constraint == NumArgsConstraint.AtMost ||
                    (arg.Constraint == NumArgsConstraint.AtLeast && arg.NumArgs == 0))
                {
                    builder.Append("]");
                    if (i < allPositionalArgs.Count - 1)
                    {
                        builder.Append(" ");
                    }
                }
            }

            if (config.Verbs.Any())
            {
                builder.Append(" <");
                builder.Append(CommandsUsagePlaceholder);
                builder.Append(">");
            }

            return(builder.ToString());
        }
示例#27
0
 public void OnParse(IParserConfig config)
 {
     Console.WriteLine(GetHelp(config));
 }
示例#28
0
 /// <summary>
 /// Action to perform when trigger is parsed.
 /// </summary>
 /// <param name="config"></param>
 public void OnParse(IParserConfig <T> config)
 {
     Console.Error.WriteLine(GetVersion());
 }