简体   繁体   English

调试时是否可以更改命令行参数?

[英]Is it possible to change command line arguments while debugging?

I have created a method which gives different message box output results depending on the passed command line arguments. 我创建了一个方法,该方法根据传递的命令行参数提供不同的消息框输出结果。

Currently I have to start debugging every time I want to change the command line arguments string. 当前,每次我想更改命令行参数字符串时,都必须开始调试。

Is there a way to change the command line arguments during a debugging session? 有没有办法在调试会话期间更改命令行参数?

EDIT: I've added some sample code 编辑:我添加了一些示例代码

private static class MyParsers
    {

    public static List<string> args;

    static MyParsers()
    {
        args = Environment.GetCommandLineArgs().ToList();
    }

        public static List<string> ParseOptions()
        {
            return ParseOptions(true);
        }

        public static List<string> ParseOptions(bool caseSensitive)
        {
            return caseSensitive
                   ? args
                   : args.MyExtToLower();
        }

        public static bool OptionExists(string option)
        {
            return OptionExists(option, true);
        }

        public static bool OptionExists(string option, bool caseSensitive)
        {
            return caseSensitive
                       ? ParseOptions().Contains(option)
                       : ParseOptions().MyExtToLower().Contains(option);
        }

        public static bool OptionExists(string option, string delimiter)
        {
            return OptionExists(option, false, delimiter);
        }

        public static bool OptionExists(string option, bool caseSensitive, string delimiter)
        {
            var args = ParseOptions(caseSensitive);
            for (var i = 1; i < args.Count; i++)
            {
                if (args[i].Contains(option + delimiter)) return true;
            }
            return false;
        }
}

Then I call MessageBox.Show(MyParsers.OptionExists("/list","=").ToString()); 然后我调用MessageBox.Show(MyParsers.OptionExists("/list","=").ToString());

If the command line argument is /list=blah it returns true. 如果命令行参数为/ list = blah,则返回true。

If the command line argument is /listary it returns false. 如果命令行参数为/ listary,则返回false。

What method would you suggest for quickly altering the command line parameters? 您建议使用哪种方法快速更改命令行参数? considering the above code I am using. 考虑我正在使用上面的代码。

The problem is not about changing command line arguments, but about reexecuting the code that was already executed with different arguments. 问题不在于更改命令行参数,而在于重新执行已使用不同参数执行的代码。 From what I understand you need to test your program with different command line arguments. 据我了解,您需要使用不同的命令行参数来测试您的程序。 Please consider the following solution to achieve your goal: 请考虑以下解决方案以实现您的目标:

  • define some method ( PerformMain ) that will accept string[] args just as a Main method does 定义一些将接受string[] args方法( PerformMain ),就像Main方法一样
  • this method should execute the code that was previously kept inside Main method 该方法应该执行以前保存在Main方法中的代码
  • your new Main may contains a list of string[], these are test cases to execute 您的新Main可能包含string []列表,这些是要执行的测试用例
  • you loop over your list of string[] and execute wrapper passing different arguments every time 您遍历string []列表,并每次执行传递不同参数的包装器
  • beware however, that code wrapper in PerformMain must be "stateless" so that consecutive executions will work properly 但是请注意, PerformMain中的代码包装器必须是“无状态的”,以便连续执行将正常工作
  • this way you can easily test hunderds of cases 这样,您可以轻松测试各种情况

Your code may look like this: 您的代码可能如下所示:

 static void Main(string[] args)
 {
     List<string[]> testCases = new List<string[]>();
     testCases.Add(new string[] { "/test", "xx" });
     testCases.Add(new string[] { "/other" });

     foreach (string[] testCase in testCases)
       Program.PerformMain(testCase);
 }

 static void PerformMain(string[] args)
 {
      // clear state of program
      // execute according to args
 }

The commandline that a process was started with cannot be changed. 启动进程的命令行不能更改。 However, you could copy your arguments into an easily-accessible "settings" object early in your application, and then manipulate that instead. 但是,您可以在应用程序的早期将参数复制到易于访问的“设置”对象中,然后进行操作。

EDIT: Instead of the properties in your object parsing the command line every time you call them, change the properties to regular properties. 编辑:而不是每次调用它们时都会在命令行中解析命令行的对象中的属性,而是将属性更改为常规属性。 Then have an Initialise method (or use the constructor, even) so that you parse the command line just once at startup. 然后使用一个Initialise方法(或什至使用构造函数),以便在启动时仅解析一次命令行。 Then, you can use the immediate window to change the values of your properties at will, because they're not referring back to the command line every time. 然后,您可以使用立即窗口随意更改属性的值,因为它们不会每次都返回到命令行。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM