Beispiel #1
0
        private bool SynonymExists(string key, string parent, string synonym)
        {
            bool    output       = true;
            command checksynonym = _commands.FindAll(x => x.key.Equals(key) && x.parent.Equals(parent) && x.synonyms.Exists(y => y.Equals(synonym))).FirstOrDefault();

            if (checksynonym == null)
            {
                output = false;
            }
            return(output);
        }
Beispiel #2
0
        private bool KeyExists(string key, string parent)
        {
            bool    output   = true;
            command checkkey = _commands.FindAll(x => x.key.Equals(key) && x.parent.Equals(parent)).FirstOrDefault();

            if (checkkey == null)
            {
                output = false;
            }
            return(output);
        }
Beispiel #3
0
        public bool AddExample(string key, string parent, string example)
        {
            bool output = false;

            if (this.KeyExists(key, parent)) // && !this.SynonymExists(key, parent, synonym))
            {
                command addexample = _commands.FindAll(x => x.key.Equals(key) && x.parent.Equals(parent)).FirstOrDefault();
                addexample.examples.Add(example);
                output = true;
            }
            return(output);
        }
Beispiel #4
0
        private bool ParentExists(string parent)
        {
            bool output = true;

            if (parent != "")
            {
                command checkparent = _commands.FindAll(x => x.key.Equals(parent)).FirstOrDefault();
                if (checkparent == null)
                {
                    output = false;
                }
            }
            return(output);
        }
Beispiel #5
0
        public bool AddSynonym(string key, string parent, string synonym)
        {
            bool output = false;

            // the only distinction between commands and optionswitches is that commands can have sub-commands
            // find this key - if it does not exist, add it
            // if the parent does not exist, don't add it (unless is root level - blank)
            if (this.ParentExists(parent) && this.KeyExists(key, parent))
            {
                // if this is adding a switch, option, or command, do so
                // note that "" (root) always exists
                command newcommand = new command();
                newcommand.key       = key;
                newcommand.synonym   = synonym;
                newcommand.parent    = parent;
                newcommand.issynonym = true;

                this._commands.Add(newcommand);
                output = true;
            }
            return(output);
        }
Beispiel #6
0
        public string GetHelp(string fullkey)
        {
            // generate help for this
            //string cr = this.CRLF;
            StringBuilder sb = new StringBuilder();
            //command topcommand = _commands.FindAll(x => ((x.parent + ' ' + x.key).TrimEnd(new char[] { ' ' })).Equals(fullkey)).FirstOrDefault();


            IEnumerable <command> commandsearch =
                from x in _commands
                where ((x.parent + ' ' + x.key).Trim()) == fullkey
                select x;
            command topcommand = commandsearch.FirstOrDefault();

            // start by displaying long information
            if (topcommand.longdesc != "")
            {
                sb.AppendLine(topcommand.longdesc);
                sb.AppendLine();
            }


            if (topcommand.usage != "")
            {
                sb.AppendLine("Usage: " + topcommand.usage);
                sb.AppendLine();
            }

            // if there are any commands, put these into a section next
            List <command> allcommands = _commands.FindAll(x => x.parent.Equals(fullkey) && x.iscommand.Equals(true) && x.isroot.Equals(false));

            if (allcommands.Count > 0)
            {
                sb.AppendLine("Commands:");

                // we pad the commands to the width of the widest (including all synonyms
                List <string> commandsyntax = new List <string>();
                List <string> commandinfo   = new List <string>();

                foreach (command thiscommand in allcommands)
                {
                    string thiscommandsyntax = thiscommand.key;
                    foreach (string thissynonym in thiscommand.synonyms)
                    {
                        thiscommandsyntax = thiscommandsyntax + '|' + thissynonym;
                    }
                    //                    thiscommandsyntax = thiscommandsyntax.TrimEnd("|".ToCharArray());
                    commandsyntax.Add(thiscommandsyntax);
                    commandinfo.Add(thiscommand.shortdesc);
                }
                int longestsyntax = commandsyntax.Max(x => x.Length);
                for (int thisline = 0; thisline < commandsyntax.Count; thisline++)
                {
                    sb.AppendLine(this.helpindent + commandsyntax.ElementAt(thisline).PadRight(longestsyntax) + "  " + commandinfo.ElementAt(thisline));
                }
                sb.AppendLine();
            }
            List <command> alloptions = _commands.FindAll(x => x.parent.Equals(fullkey) && (x.isoption.Equals(true) || x.isswitch.Equals(true)) && x.isroot.Equals(false));

            if (alloptions.Count > 0)
            {
                sb.AppendLine("Options:");

                // we pad the commands to the width of the widest (including all synonyms
                List <string> optionsyntax = new List <string>();
                List <string> optioninfo   = new List <string>();

                foreach (command thisoption in alloptions)
                {
                    string thisoptionsyntax = thisoption.key;
                    foreach (string thissynonym in thisoption.synonyms)
                    {
                        thisoptionsyntax = thisoptionsyntax + '|' + thissynonym;
                    }
                    //                    thiscommandsyntax = thiscommandsyntax.TrimEnd("|".ToCharArray());
                    optionsyntax.Add(thisoptionsyntax);
                    optioninfo.Add(thisoption.shortdesc);
                }
                int longestsyntax = optionsyntax.Max(x => x.Length);
                for (int thisline = 0; thisline < optionsyntax.Count; thisline++)
                {
                    sb.AppendLine(this.helpindent + optionsyntax.ElementAt(thisline).PadRight(longestsyntax) + "  " + optioninfo.ElementAt(thisline));
                }
                sb.AppendLine();
            }

            if (topcommand.examples.Count > 0)
            {
                sb.AppendLine("Examples:");

                foreach (string thisexample in topcommand.examples)
                {
                    sb.AppendLine(this.helpindent + thisexample);
                }
                sb.AppendLine();
            }
            return(sb.ToString());


            // the only distinction between commands and optionswitches is that commands can have sub-commands
            // find this key - if it does not exist, add it
            // if the parent does not exist, don't add it (unless is root level - blank)
            // bool output = false;
            //public bool SetParameters(string[] paraarray)
            //{
            //    StringBuilder sb = new StringBuilder();
            //    foreach(string thispara in paraarray)
            //    {
            //        sb.Append(thispara);
            //        sb.Append(' ');
            //    }
            //    return this.SetParameters(sb.ToString().Trim());
            //}
            //public bool SetParameters(string paratext)
            //{
            //    // step through each character - note that once you have a command, you only check this and subcommands
            //    string rootcommand = "";

            //}
        }
Beispiel #7
0
        public bool AnalyseParts(List <lookup> rawparts)
        {
            // step through each part and check it against the parameter rules
            // get all syn
            string currentparent = "";
            string currentstyle  = "";

            foreach (lookup thispart in rawparts)
            {
                // check this in the synonyms
                command match = this._commands.FindAll(x => x.synonym.Equals(thispart.value) && x.parent.Equals(currentparent)).FirstOrDefault();
                if (match == null)
                {
                    // is a "data" item: set this in the type property of the parent
                    thispart.parameterstyle = "Data";
                    thispart.fullkey        = currentparent;

                    // now put this into the "value" field of the parent
                    if (currentstyle == "Option" || currentstyle == "Command")
                    {
                        // find the parent and insert this value into the
                        if (thispart.value != "")
                        {
                            lookup valuematch = this._values.FindAll(x => x.fullkey.Equals(thispart.fullkey)).FirstOrDefault();
                            if (valuematch.value == "")
                            {
                                // put this in
                                valuematch.value = thispart.value;
                            }
                            else if (this.takefirstvalueonly == false)
                            {
                                valuematch.value = valuematch.value + ' ' + thispart.value;
                            }
                        }
                    }
                }
                else
                {
                    // this is a match: find the key and retrieve this from
                    command keymatch;
                    if (match.issynonym == true)
                    {
                        keymatch = this._commands.FindAll(x => x.key.Equals(match.key) && x.parent.Equals(currentparent)).FirstOrDefault();
                    }
                    else
                    {
                        keymatch = match;
                    }
                    if (keymatch != null)
                    {
                        // check the type: if a switch, we set it to true and do nothing else
                        if (keymatch.isswitch == true)
                        {
                            thispart.parameterstyle = "Switch";
                            thispart.fullkey        = (currentparent + ' ' + keymatch.key).Trim();
                            thispart.value          = "true";
                        }
                        else if (keymatch.isoption == true)
                        {
                            // the NEXT data item is the value: note that only the first of these is added
                            currentstyle  = "Option";
                            currentparent = (currentparent + ' ' + keymatch.key).Trim();

                            thispart.parameterstyle = "Option";
                            thispart.fullkey        = currentparent;
                            thispart.value          = "";
                        }
                        else if (keymatch.iscommand == true)
                        {
                            // the NEXT data item is the value: note that only the first of these is added
                            currentstyle  = "Command";
                            currentparent = (currentparent + ' ' + keymatch.key).Trim();

                            thispart.parameterstyle = "Command";
                            thispart.fullkey        = currentparent;
                            thispart.value          = "";
                        }
                    }
                }
            }

            return(true);
        }