0

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());

If the command line argument is /list=blah it returns true.

If the command line argument is /listary it returns false.

What method would you suggest for quickly altering the command line parameters? considering the above code I am using.

8
  • 1
    In VS2010 you can change the values of whatever you want by hovering over the appropriate variable while you are at a break point, then clicking on the property value you want to modify. Commented Aug 13, 2012 at 9:00
  • @eyossi The code is irrelevant. I just need to change the command line arguments passed to the application while debugging. Commented Aug 13, 2012 at 9:08
  • @veredesmarald Yes, I know that, but I'm not storing the command line arguments in any variable, and the command line arguments section of the project properties is greyed out while debugging. Commented Aug 13, 2012 at 9:09
  • args only exists on the stack in Main once the program has been run. You can't go back to rerun Main with new arguments without restarting the program. Instead you can use either the gui or immediate window to manipulate the values (maybe not stored directly in args?) but you can easily move them over into another local, or class variable right at the beginning of the program and manipulate that to your hearts content Commented Aug 13, 2012 at 9:10
  • @IneedHelp If you aren't storing the command line arguments in a parameter to main then how are you using them for anything? What are you actually trying to accomplish, because it sure sounds like you have the wrong approach... If you add some code to your question we can almost certainly point you in a better direction. Commented Aug 13, 2012 at 9:13

2 Answers 2

2

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
  • this method should execute the code that was previously kept inside Main method
  • your new Main may contains a list of string[], these are test cases to execute
  • you loop over your list of string[] and execute wrapper passing different arguments every time
  • beware however, that code wrapper in PerformMain must be "stateless" so that consecutive executions will work properly
  • 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
 }
Sign up to request clarification or add additional context in comments.

1 Comment

voting helpful, moving your logic into another function is what you need to do just as shown here.. though I'd still consider using the immediate window and a break point at the call to PerformMain so you can just manipulate the values on the fly, point about stateless highly important too
1

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. 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.

2 Comments

Ok, so I understand that the commandline that a process was started with cannot be changed. How would you suggest I implement an easily-accessible "settings" object considering the code I provided in the edited question?
thank you for the update. I have tried saving the arguments list just once in the constructors, but I have no idea how to change the args list elements from the immediate window. I have edited the question with the new code. Am I still doing it wrong?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.