Reputation: 1758
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.
Upvotes: 0
Views: 4277
Reputation: 34200
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.
Upvotes: 1
Reputation: 6221
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:
PerformMain
) that will accept string[] args
just as a Main
method doesMain
methodMain
may contains a list of string[], these are test cases to executePerformMain
must be "stateless" so that consecutive executions will work properlyYour 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
}
Upvotes: 2