Ejemplo n.º 1
0
 public bool CanParse(IArgumentPack pack, Type targetType)
 {
     if (targetType != typeof(Guid))
     {
         return(false);
     }
     return(Guid.TryParse(pack.Peek(), out _));
 }
Ejemplo n.º 2
0
        public object Parse(IArgumentPack pack, Type targetType)
        {
            var value = pack.Take();

            if (ulong.TryParse(value, out ulong result))
            {
                return(result);
            }
            return(null);
        }
Ejemplo n.º 3
0
        public static string TakeAll(this IArgumentPack pack)
        {
            List <string> allItems = new List <string>();

            while (pack.CanTake)
            {
                allItems.Add(pack.Take());
            }
            return(string.Join(" ", allItems));
        }
        public object Peek(IArgumentPack p, Type type)
        {
            int    cursor = p.Cursor;
            object output = parsers.Where(x => IsAssignableFrom(type, x.OutputType))
                            .Where(x => x.CanParse(p, type))
                            .OrderByDescending(x => x.Priority)
                            .FirstOrDefault()?
                            .Parse(p, type);

            p.SetCursor(cursor);
            return(output);
        }
Ejemplo n.º 5
0
        public object Parse(IArgumentPack pack, Type targetType)
        {
            var arg = pack.Peek().Unwrap();

            if (!arg.StartsWithAny("\"", "“", "”"))
            {
                return(pack.Take());
            }

            var allItems = new List <string>();

            do
            {
                allItems.Add(pack.Take());
            } while(!allItems[^ 1].EndsWithAny("\"", "“", "”"));
        public object Parse(IArgumentPack pack, Type targetType)
        {
            var item = pack.Take();

            if (bool.TryParse(item, out bool valueBool))
            {
                return(valueBool);
            }

            if (int.TryParse(item, out int valueInt))
            {
                return(valueInt > 0);
            }
            return(null);
        }
        /// <inheritdoc/>
        public bool CanParse(IArgumentPack pack, Type targetType)
        {
            if (targetType != typeof(int))
            {
                return(false);
            }

            var value = pack.Peek();

            if (!value.HasValue)
            {
                return(false);
            }

            var argument = value.Unwrap();

            return(suffixes.ContainsKey(char.ToLowerInvariant(argument.Last())) &&
                   int.TryParse(argument[..^ 1], out _));
Ejemplo n.º 8
0
        public virtual Node FindCommand(IArgumentPack pack)
        {
            if (!pack.CanTake)
            {
                return(null);
            }

            var arg = pack.Peek().Unwrap()
                      .ToLowerInvariant();

            // Take if this module starts.
            if (Metadata.Identifiers?.Any(x => x.ToLowerInvariant() == arg.ToLowerInvariant()) ?? false)
            {
                pack.Take();
            }

            foreach (var c in Children)
            {
                if (c is NodeContainer nc)
                {
                    var foundNode = nc.FindCommand(pack);
                    if (foundNode != null)
                    {
                        return(foundNode);
                    }
                }
                else
                {
                    if (c.Metadata.Identifiers.Any(x => x == arg))
                    {
                        pack.Take();
                        return(c);
                    }
                }
            }
            return(null);
        }
Ejemplo n.º 9
0
        /// <summary>
        /// Query a command recursively
        /// </summary>
        /// <param name="pack">Argument pack to iterate over.</param>
        /// <returns>Nullable node</returns>
        public override Node FindCommand(IArgumentPack pack)
        {
            var arg = pack.Peek().Unwrap()
                      .ToLowerInvariant();

            if (Metadata.Identifiers == null || !Metadata.Identifiers.Any())
            {
                return(null);
            }

            if (Metadata.Identifiers.All(x => x != arg))
            {
                return(null);
            }

            pack.Take();
            var cmd = base.FindCommand(pack);

            if (cmd != null)
            {
                return(cmd);
            }
            return(this);
        }
Ejemplo n.º 10
0
 public object Parse(IArgumentPack pack, Type targetType)
 {
     return(Guid.Parse(pack.Take()));
 }
Ejemplo n.º 11
0
 public T Take <T>(IArgumentPack p)
 => (T)Take(p, typeof(T));
Ejemplo n.º 12
0
 /// <inheritdoc />
 public bool CanParse(IArgumentPack pack, Type targetType)
 {
     return(Enum.TryParse(targetType, pack.Peek(), true, out _));
 }
Ejemplo n.º 13
0
 /// <inheritdoc />
 public object Parse(IArgumentPack pack, Type targetType)
 {
     return(Enum.Parse(targetType, pack.Take(), true));
 }
Ejemplo n.º 14
0
 public T Peek <T>(IArgumentPack p)
 => (T)Peek(p, typeof(T));
Ejemplo n.º 15
0
 public bool CanParse(IArgumentPack pack, Type targetType)
 => true;
Ejemplo n.º 16
0
 public object Take(IArgumentPack p, Type type)
 => parsers.Where(x => IsAssignableFrom(type, x.OutputType))
 .Where(x => x.CanParse(p, type))
 .OrderByDescending(x => x.Priority)
 .FirstOrDefault()?
 .Parse(p, type);
Ejemplo n.º 17
0
 public Node GetCommand(IArgumentPack pack)
 {
     return(Root.FindCommand(pack));
 }
Ejemplo n.º 18
0
 public TypedArgumentPack(IArgumentPack pack, ArgumentParseProvider parseProvider)
 {
     Pack = pack;
     this.parseProvider = parseProvider;
 }