예제 #1
0
        public YaclopsDumpTreeCommand(CommandNode start)
        {
            _start = start;

            _entries.Add(new TypeEntry { Type = typeof(CommandRoot), Code = "R", HasChildren = true });
            _entries.Add(new TypeEntry { Type = typeof(CommandGroup), Code = "G", HasChildren = true });
            _entries.Add(new TypeEntry { Type = typeof(Command), Code = "C", HasChildren = false });
        }
예제 #2
0
        private void DumpNode(CommandNode node, int indent)
        {
            TypeEntry entry = _entries.FirstOrDefault(x => x.Type.IsInstanceOfType(node));
            if (entry == null)
            {
                throw new ArgumentException("Node is of an unknown type: " + node.GetType().Name, "node");
            }

            var shortNames = node.NamedParameters.SelectMany(x => x.ShortNames, (x, l) => l).OrderBy(x => x).ToList();
            var longNames = node.NamedParameters.SelectMany(x => x.LongNames, (x, l) => l).OrderBy(x => x).ToList();

            var chars = new string('.', indent);
            var spaces = new string(' ', indent + 7);
            Console.WriteLine("{0}[{1}]: {2}", chars, entry.Code, node.Verb);

            if (shortNames.Any())
            {
                Console.WriteLine("{0}SN: {1}", spaces, string.Join("|", shortNames));
            }

            if (longNames.Any())
            {
                Console.WriteLine("{0}LN: {1}", spaces, string.Join("|", longNames));
            }

            if (node.PositionalParameters.Any())
            {
                Console.WriteLine("{0}PP: {1}", spaces, string.Join("|", node.PositionalParameters.Select(x => x.PropertyName + (x.IsRequired ? "(r)" : string.Empty))));
            }

            if (entry.HasChildren)
            {
                var group = (CommandGroup) node;
                foreach (var child in group.Nodes.OrderBy(x => x.Verb))
                {
                    DumpNode(child, indent + 2);
                }
            }
        }
예제 #3
0
        public ParserState(CommandNode startNode, Lexer lexer, IEnumerable<string> helpFlags, string helpVerb)
        {
            NamedParameters = new List<ParserNamedParameterResult>();
            PositionalParameters = new List<ParserPositionalParameterResult>();
            CurrentNode = startNode;
            ExtractParameters(CurrentNode);

            foreach (var flag in helpFlags)
            {
                if (flag.StartsWith("--"))
                {
                    _longHelpFlags.Add(flag.Substring(2));
                }
                else if (flag.StartsWith("-"))
                {
                    _shortHelpFlags.Add(flag.Substring(1));
                }
            }

            _lexer = lexer;
            _helpVerb = helpVerb;
            CurrentToken = _lexer.Pop();
        }
예제 #4
0
 public InternalCommand(CommandNode parent, string verb, Action<CommandRoot, CommandNode> worker)
     : base(parent, verb)
 {
     Worker = worker;
 }
예제 #5
0
 protected CommandNode(CommandNode parent, string verb)
 {
     Parent = parent;
     Verb = verb;
 }
예제 #6
0
 protected ExternalCommand(CommandNode parent, string verb)
     : base(parent, verb)
 {
 }
예제 #7
0
        private void ExtractParameters(CommandNode node)
        {
            foreach (var p in node.NamedParameters)
            {
                foreach (var name in p.LongNames)
                {
                    _longNames[name] = p;
                }

                foreach (var name in p.ShortNames)
                {
                    _shortNames[name] = p;
                }
            }

            foreach (var p in node.PositionalParameters)
            {
                _positionalParameters.Enqueue(p);

                if (p.IsRequired)
                {
                    _pendingRequired.Add(p.PropertyName);
                }
            }
        }
예제 #8
0
 public static HelpCommand Make(CommandNode start, GlobalParserSettings settings)
 {
     return new HelpCommand(start, settings);
 }
예제 #9
0
 public CommandChildEntry(CommandNode node, int depth)
 {
     Node = node;
     Depth = depth;
 }
예제 #10
0
        // TODO - include options of ancestor groups?
        private string VerbPath(CommandNode node, int maxDepth = int.MaxValue)
        {
            StringBuilder builder = new StringBuilder();

            if ((node.Parent != null) && (maxDepth > 0))
            {
                builder.Append(VerbPath(node.Parent, maxDepth - 1));
            }

            if (builder.Length > 0)
            {
                builder.Append(" ");
            }

            builder.Append(node.HelpVerb);

            return builder.ToString();
        }
예제 #11
0
 public HelpCommand(CommandNode start, GlobalParserSettings settings)
 {
     _start = start;
     _settings = settings;
 }
예제 #12
0
        private int CountChildCommands(CommandNode node, bool includeHidden)
        {
            int count = 0;

            Command command = node as Command;
            if (command != null)
            {
                count += 1;
            }
            else
            {
                CommandGroup group = node as CommandGroup;
                if ((group != null) && (includeHidden || !group.Hidden))
                {
                    count += group.Nodes.Sum(child => CountChildCommands(child, includeHidden));
                }
            }

            return count;
        }