I'm trying to read user arguments in a C# application. I know how to read them based on position with
string[] args = Environment.GetCommandLineArgs();
but I'd like to read them from switches such as
app.exe /f /d:foo
I'm really struggling to find any information on doing this...
Use a switch once by adding it to the Run commandIn the Run dialog box, type a quotation mark, enter the full path for the app's .exe file, and then type another quotation mark. Alternatively, click Browse to locate and select the file. In this case, the quotation marks are supplied automatically.
A command line switch (also known as an option, a parameter, or a flag) acts as a modifier to the command you are issuing in the Command Prompt window, in a batch file, or in other scripts. Usually, a switch is a single letter preceded by a forward slash.
The special character $# stores the total number of arguments. We also have $@ and $* as wildcard characters which are used to denote all the arguments. We use $$ to find the process ID of the current shell script, while $? can be used to print the exit code for our script.
To pass command line arguments, we typically define main() with two arguments : first argument is the number of command line arguments and second is list of command-line arguments. The value of argc should be non negative. argv(ARGument Vector) is array of character pointers listing all the arguments.
Why don't you just parse the array of arguments passed and act based on them, like this
foreach (string arg in args)
{
switch (arg.Substring(0, 2).ToUpper())
{
case "/F":
// process argument...
break;
case "/Z":
// process arg...
break;
case "/D":
paramD = arg.Substring(3);
break;
default:
// do other stuff...
break;
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With