示例#1
0
        // http://www.codeproject.com/Articles/816301/Csharp-Building-a-Useful-Extensible-NET-Console-Ap

        public static void Main(string[] args)
        {
            try
            {
                string   username = string.Empty;
                string   password = string.Empty;
                string   baseURL  = string.Empty;
                string[] commandList;

                // Three Arguments are required: username, password and base url. If not there, ask for them
                if (args.Count() != 3)
                {
                    Console.Write("Usage: EnomCLI [username] [password] [baseurl]\n(Example BaseURL: 'https://resellertest.enom.com/interface.asp?command=' )\n");

                    username = EnomUtilities.GetValueFromUser("Username");
                    password = EnomUtilities.GetValueFromUser("Password");
                    baseURL  = EnomUtilities.GetValueFromUser("BaseURL");
                }
                else
                {
                    username = args[0];
                    password = args[1];
                    baseURL  = args[2];
                }

                // Set up SSL Support
                ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12;

                // Load up valid enom commands.
                if (System.IO.File.Exists("CommandList.txt"))
                {
                    commandList = System.IO.File.ReadAllLines("CommandList.txt");
                }
                else
                {
                    commandList = EnomUtilities.GetInternalCommandList();
                }

                CommandProcessor processor = new CommandProcessor();
                processor.Run(baseURL, username, password, commandList, "xml");
            }
            catch (System.Exception ex)
            {
                Console.WriteLine("\nError encountered: " + ex.Message);
            }
        }
        public void Run(string baseURL, string username, string password, string[] commandList, string responseType)
        {
            string input = string.Empty;

            ShowHelp();

            while (true)
            {
                // Create the prompt
                Console.Write("enom>");
                input = Console.ReadLine().ToLower();

                // If the user types exit, close the command line
                if (input == "exit")
                {
                    return;
                }
                else if (input == "cls")
                {
                    Console.Clear();
                }
                else if (input == "help")
                {
                    ShowHelp();
                }
                else if (input.StartsWith("responsetype "))
                {
                    // User is changing the response type
                    string[] commands = input.Split(' ');
                    responseType = commands[1];
                }
                else
                {
                    // If we have a valid command line, process
                    if (!string.IsNullOrWhiteSpace(input))
                    {
                        // Verify this is a valid command
                        string commandName = input.Split(' ')[0];
                        if (!commandList.Contains(commandName))
                        {
                            Console.Write("Invalid command.\n");
                        }
                        else
                        {
                            try
                            {
                                // Build the URL
                                string url = EnomUtilities.BuildURL(input, username, password, baseURL, responseType);

                                // Load the result
                                string response = EnomUtilities.GetResponse(url);

                                if (responseType.ToLower() == "xml")
                                {
                                    // Display result to the screen with formatting
                                    Console.WriteLine(EnomUtilities.PrettyXml(response + Environment.NewLine));
                                }
                                else
                                {
                                    // Display result to the screen without formatting
                                    Console.WriteLine(response + Environment.NewLine);
                                }
                            }
                            catch (System.ApplicationException ae)
                            {
                                Console.WriteLine(ae.Message);
                            }
                            catch (System.Exception ex)
                            {
                                Console.WriteLine("Error: " + ex.Message);
                            }
                        }
                    }
                }
            }
        }