Command line argument

With the first introduction post of C# you can understand the return value of the Main() method, let’s examine the incoming array of string data. Assume that you now want to update your application to process any possible command-line parameters. One way to do so is using a C# for loop. We will see for loop in C# in later article.

Accessing command line arguments with for loop

Program


using System;

public class CLA
{
    public static void Main(string[] args)
    {
        //this loop prints any number of given command line arguments
        for(int i=0; i<args.Length; i++)
        {
            Console.WriteLine("arg-" + i + " = " + args[i]);
        }
    }
}

Output


F:\Code\Algorithm>csc CLA.cs

F:\Code\Algorithm>CLA tangled java elaborately
arg-0 = tangled
arg-1 = java
arg-2 = elaborately
  

Explanation

Here, you are checking to see whether the array of strings contains some number of items using the Length property of System.Array. As you’ll see in later article, all C# arrays actually alias the System.Array class and, therefore, share a common set of members. As you loop over each item in the array, its value is printed to the console window. Supplying the arguments at the command line is equally simple, as shown in output above.

Accessing command line arguments with foreach loop

As an alternative to the standard for loop, you may iterate over an incoming string array using the C# foreach keyword. Here is sample usage :

Program


using System;

public class CLA
{
    public static void Main(string[] args)
    {
        //you don't need to specify length property of array when using foreach loop
        foreach (string arg in args)
        {
            Console.WriteLine("Arg: {0}",arg);
        }
    }
}
  

Output


F:\Code\Algorithm>csc CLA.cs

F:\Code\Algorithm>CLA tangled java elaborately
Arg: tangled
Arg: java
Arg: elaborately
  

Accessing command line arguments with GetCommandLineArgs()

You are also able to access command-line arguments using the static GetCommandLineArgs() method of the System.Environment type. The return value of this method is an array of strings. The first index identifies the name of the application itself, while the remaining elements in the array contain the individual command-line arguments. Note that when using this approach, it is no longer necessary to define Main() as taking a string array as the input parameter, although there is no harm in doing so.

Program


using System;

public class CLA
{
    public static void Main()
    {
        //command line arguments are stored in string typed array args
        string[] args = Environment.GetCommandLineArgs();
        //remember when using this method in output first argument is always filename
        foreach(string arg in args)
        {
            Console.WriteLine("Args: {0}",arg);
        }
    }
}
  

Output


F:\Code\Algorithm>csc CLA.cs

F:\Code\Algorithm>CLA tangled java elaborately
Args: CLA
Args: tangled
Args: java
Args: elaborately
  

Explanation

It is up to you to determine which command-line arguments your program will respond to (if any) and how they must be formatted.

Specifying Command-Line Arguments with Visual Studio

During the development cycle, you might want to specify possible command-line flags for testing purposes. To do so with Visual Studio, double-click the Properties icon in Solution Explorer and select the Debug tab on the left side. From there, specify values using the command-line arguments text box and save your changes.

Program


using System;

public class ConsoleApp
{  
    public static void Main(string[] args)
    {
        for(int i=0; i<args.Length; i++)
        {
            Console.WriteLine("Arg-{0} = {1}",i,args[i]);
        }
        Console.ReadLine();
    }
}
  

Output


Arg-0 = tangled
Arg-1 = java
Arg-2 = elaborately
  

Comments

Popular posts from this blog

System.Environment

System.Console

Datatype and keyword