Ejemplo n.º 1
0
        private void InitializeVerbs()
        {
            foreach (var prop in typeof(T).GetTypeInfo().GetProperties()
                     .Where(p => p.GetCustomAttributes <VerbAttribute>().Any()))
            {
                foreach (var attr in prop.GetCustomAttributes <VerbAttribute>())
                {
                    // TODO deduplicate this Verb Name generation logic
                    var verbName = attr.Name ?? prop.Name.ToLowerInvariant();
                    if (verbName.StartsWith(
                            ArgumentPrefix.ToString()))
                    {
                        throw new ArgumentIntegrityException(String.Format(
                                                                 "Verb {0} cannot begin with {1}",
                                                                 verbName, ArgumentPrefix));
                    }
                    if (Verbs.ContainsKey(verbName))
                    {
                        throw new DuplicateArgumentException(verbName);
                    }

                    // TODO allow verb to use other configuration types?
                    var parserConfigType = typeof(AttributeParserConfig <>)
                                           .GetTypeInfo()
                                           .MakeGenericType(new[] { prop.PropertyType });
                    var verbParserConfig = Activator.CreateInstance(
                        type: parserConfigType,
                        args: new object[] { Options, null /* TODO add triggers inside verb configs */, VerbFactory });

                    var verbConfigWrapperType = typeof(VerbParserConfig <>)
                                                .GetTypeInfo()
                                                .MakeGenericType(new[] { prop.PropertyType });
                    var config = (IVerbParserConfig)Activator.CreateInstance(
                        type: verbConfigWrapperType,
                        args: new[] { verbParserConfig, new PropertyValueStore(prop), Options, VerbFactory, new[] { verbName } });

                    config.Name        = verbName;
                    config.Description = attr.Description;
                    if (Options.IncludeHelpTriggerInVerbs)
                    {
                        var helpWrapperType = typeof(AutomaticHelpGenerator <>)
                                              .GetTypeInfo()
                                              .MakeGenericType(new[] { prop.PropertyType });
                        var triggers = new[] { (IHelpGenerator)Activator.CreateInstance(
                                                   type: helpWrapperType,
                                                   args: new object[] { }) };
                        config.AppendTriggers(triggers);
                    }

                    // TODO optimize this a bit
                    foreach (var innerVerb in config.Verbs.Values)
                    {
                        innerVerb.PrecursorVerbs = Enumerable.Repeat(verbName, 1).Concat(innerVerb.PrecursorVerbs).ToArray();
                    }

                    Verbs.Add(verbName, config);
                }
            }
        }
Ejemplo n.º 2
0
        private void InitializeVerbs()
        {
            foreach (var prop in typeof(T).GetProperties()
                     .Where(p => p.GetCustomAttributes <VerbAttribute>().Any()))
            {
                foreach (var attr in prop.GetCustomAttributes <VerbAttribute>())
                {
                    var verbName = attr.Name ?? prop.Name;
                    if (verbName.StartsWith(
                            ArgumentPrefix.ToString(CultureInfo.InvariantCulture)))
                    {
                        throw new ArgumentIntegrityException(String.Format(
                                                                 "Verb {0} cannot begin with {1}",
                                                                 verbName, ArgumentPrefix));
                    }
                    if (Verbs.ContainsKey(verbName))
                    {
                        throw new DuplicateArgumentException(verbName);
                    }

                    var parserConfigType = typeof(AttributeParserConfig <>)
                                           .MakeGenericType(new[] { prop.PropertyType });
                    var parserConfig = Activator.CreateInstance(
                        parserConfigType,
                        new object[] { Options, null /* TODO add triggers inside verb configs */ },
                        null);

                    // Verb's settings object
                    // Object must have default constructor
                    var obj = Activator.CreateInstance(prop.PropertyType);

                    var contextType = typeof(ParsingContext <>)
                                      .MakeGenericType(new[] { prop.PropertyType });
                    var context = (IParsingContext)Activator.CreateInstance(
                        contextType,
                        new [] { obj, parserConfig },
                        null);

                    var verbConfig = new VerbConfig
                    {
                        Context = context,
                        Object  = obj,
                        Store   = new PropertyValueStore(prop)
                    };

                    Verbs.Add(verbName, verbConfig);
                }
            }
        }
Ejemplo n.º 3
0
        private void InitializeVerbs()
        {
            foreach (var prop in typeof(T).GetTypeInfo().GetProperties()
                     .Where(p => p.GetCustomAttributes <VerbAttribute>().Any()))
            {
                foreach (var attr in prop.GetCustomAttributes <VerbAttribute>())
                {
                    // TODO deduplicate this Verb Name generation logic
                    var verbName = attr.Name ?? prop.Name.ToLowerInvariant();
                    if (verbName.StartsWith(
                            ArgumentPrefix.ToString()))
                    {
                        throw new ArgumentIntegrityException(String.Format(
                                                                 "Verb {0} cannot begin with {1}",
                                                                 verbName, ArgumentPrefix));
                    }
                    if (Verbs.ContainsKey(verbName))
                    {
                        throw new DuplicateArgumentException(verbName);
                    }

                    // TODO allow verb to use other configuration types?
                    var parserConfigType = typeof(AttributeParserConfig <>)
                                           .GetTypeInfo()
                                           .MakeGenericType(new[] { prop.PropertyType });
                    var verbParserConfig = Activator.CreateInstance(
                        type: parserConfigType,
                        args: new object[] { Options, null /* TODO add triggers inside verb configs */, VerbFactory });

                    var verbConfigWrapperType = typeof(VerbParserConfig <>)
                                                .GetTypeInfo()
                                                .MakeGenericType(new[] { prop.PropertyType });
                    var config = (IVerbParserConfig)Activator.CreateInstance(
                        type: verbConfigWrapperType,
                        args: new[] { verbParserConfig, new PropertyValueStore(prop), Options, VerbFactory });

                    Verbs.Add(verbName, config);
                }
            }
        }
Ejemplo n.º 4
0
 /// <summary>
 /// Initializes a new instance of the <see cref="IntentArgument"/> class.
 /// </summary>
 /// <param name="m_prefix">The m_prefix.</param>
 /// <param name="m_args">The m_args.</param>
 private IntentArgument(ArgumentPrefix m_prefix, params object[] m_args)
 {
     this.Prefix = m_prefix;
     this.Args   = m_args;
 }
Ejemplo n.º 5
0
 /// <summary>
 /// Gets the intent argument.
 /// </summary>
 /// <param name="m_prefix">The argument prefix.</param>
 /// <param name="m_args">The argument arg(s).</param>
 /// <returns>A new instance of this class.</returns>
 internal static IntentArgument GetIntentArgument(ArgumentPrefix m_prefix, params object[] m_args)
 {
     return(new IntentArgument(m_prefix, m_args));
 }
Ejemplo n.º 6
0
 /// <summary>
 /// Gets the intent argument.
 /// </summary>
 /// <param name="m_prefix">The argument prefix.</param>
 /// <param name="m_args">The argument arg(s).</param>
 /// <returns>A new instance of this class.</returns>
 internal static IntentArgument GetIntentArgument(ArgumentPrefix m_prefix, params object[] m_args)
 {
     return new IntentArgument(m_prefix, m_args);
 }
Ejemplo n.º 7
0
 /// <summary>
 /// Initializes a new instance of the <see cref="IntentArgument"/> class.
 /// </summary>
 /// <param name="m_prefix">The m_prefix.</param>
 /// <param name="m_args">The m_args.</param>
 private IntentArgument(ArgumentPrefix m_prefix, params object[] m_args)
 {
     this.Prefix = m_prefix;
     this.Args = m_args;
 }