0

This is my current code;

        static void Adonis()
        {
            Console.Write("adonis@" + prefix + "# ");
            string adonisInput = Console.ReadLine();
            CmdCheck(adonisInput);
        }

        static void CmdCheck(string arg)
        {
            if (arg.Contains("use"))
            {
                string str = arg;
                string[] tokens = str.Split(' ');
                string chosenExploit = tokens[1];
                if (chosenExploit == "pscan")
                {
                    p(pscan.print());
                    Adonis();
                }
                if (chosenExploit == "ping")
                {
                    //code
                }
                if (!(chosenExploit is module)) // this line keeps getting the error
                {
                    p("Invalid Syntax: '" + arg + "', 'use <arg>'; missing argument.");
                    Adonis();
                }
            }
            Console.WriteLine("Unknown command, '" + arg + "', type 'help' for more commands.");
            Adonis();
        }

Let me put some background knowledge into my initiative.

I have two objects named "pscan", and "ping". If they want to use pscan or ping, they should be able to type, "use pscan", or "use ping". I do this by finding the split in between "use" and the input, and I read the following text. Which could possibly be "pscan, or "ping".

I wanted to make some error handling, so I was trying to see like, what if someone entered something like "use", "use ", or even mispell such as "use pscna". I wanted it to read "pscna" or the null ("") and return it and tell the user that they are missing an argument and that it's an invalid syntax.

The error I keep getting is "Index was outside the bounds of the array".

Akuhyo
  • 23
  • 6
  • Look at https://stackoverflow.com/questions/491595/best-way-to-parse-command-line-arguments-in-c – Daveo Nov 17 '19 at 06:19
  • The line `if (!(chosenExploit is module))` shouldn't produce this specific error. This error might be raised from this line: `string chosenExploit = tokens[1];` - specifically, if the user entered a value that has no spaces in it. – Zohar Peled Nov 17 '19 at 06:21

1 Answers1

0
string chosenExploit = tokens[1];

This part would only works if the user did typed in something after "use", else your array would just be a single word of "use", so I'll suggest you start by check if the length of array is 1 to see if there's even an argument.

Xascoria Dung
  • 75
  • 1
  • 9