The Console Project template is often overlooked and rarely loved but, the power it possesses is amazing. If you don’t mind not working without a user interface, that is. Not all processes need human interaction, though. As a professional software developer, I have encountered many different scenarios where that is the case. I actually enjoy these projects because they have an entire level of sophistication removed from them: the user interface. What is left is just me and the code. Keep in mind, that in this post, we are only dealing with console applications.
What Are Command Line Arguments
Command line arguments are passed to a console application that can then be parsed by the application and, if the argument is both expected and within a known range of options, ask the application to execute in specific ways. The signature for the Main() method is the entry point of the application and the default signature allows these arguments.
Figure 1.1 – The default main() method signature
static void Main(string[] args) { }
As long as you can pass your argument as a string, the application can handle it.
Command Line Argument Basics
If you right-click a console application project in Solution Explorer and click the Properties menu option, you will see the tabbed Project Properties options.
Figure 1.2 – Project Properties
Click the Debug tab and you will see the Start Options section. This is where you can pass in command line arguments to your applications for debugging purposes. Now, in the Main() method, if we add a simple loop to go through the arguments and print out each argument, what will see based on the Hello World arguments as above?
Figure 1.3 – Main() method to print out command line arguments
static void Main(string[] args) { foreach(string arg in args) { Console.WriteLine(arg); } Console.ReadLine(); }
Figure 1.4 – Hello World output
Hello
World
If the arguments are entered as separate words, they are considered separate arguments. You can get around this by wrapping the text in double quotes: “Hello World”. This would print out a single argument.
Moving Beyond The Basics
If your console application accepts command line arguments and especially if there is more than one possible argument, you will soon find that you can’t just accept them as they come. You will likely need named arguments. You see this sometimes in other console applications. For example, perhaps a possible argument is -t or -f or -file with the very next argument as the file name. As a basic exercise, I am going to create a simple program that can handle five different arguments. What these arguments are doesn’t really matter as the basic concepts will be the same regardless of what they are or what they do.
First, I am going to check for one of two possible arguments: /help or /?. It can be assumed that if either of these commands appears, the user is looking for help. This will cause the console to display pre-formatted help text. If either of the help arguments is present, all others will be ignored. Also, I am going to test for exit argument. Again, if this argument is present, we don’t have to worry about checking for any others. The other arguments, as you will see below, are -f or /file to specify a file name and -t. If the -t argument is present, the program will check to ensure that the file exists. If the -t argument is not present, that check will be skipped. These are just possible paths to show how certain things can be done. There are limitless possibilities really.
Listing 1.5 – Command Line Argument Parser
public class CmdLineArgHandler { private List<string> arguments = null; private bool switchToggled = false; private bool fileExists = false; private string fileName = string.Empty; public CmdLineArgHandler(string[] args) { arguments = args.ToList().Select(x => x.ToLower()).ToList(); } public void Start() { if(arguments.Contains("/help") || arguments.Contains("/?")) { DisplayHelp(); } else if(arguments.Contains("/exit")) { return; } else { if(arguments.Contains("-t")) { switchToggled = true; } if(arguments.Contains("-f") || arguments.Contains("/file")) { int index = arguments.IndexOf("/file"); fileName = arguments[index + 1]; if(switchToggled) { fileExists = new System.IO.FileInfo(fileName).Exists; } } if(switchToggled) { Console.WriteLine("Switch Toggled."); if(fileExists) { Console.WriteLine("File exists"); } else { Console.WriteLine("File does not exist"); } } } } private void DisplayHelp() { Console.WriteLine("Sample Help Text"); Console.WriteLine(""); Console.WriteLine("/help or /?\t\tDisplay this help text"); Console.WriteLine(""); Console.WriteLine("-t\t\t\tTurns on a toggle that does something amazing"); Console.WriteLine(""); Console.WriteLine("-f or /file\t\tPath to a file"); Console.WriteLine(""); Console.WriteLine("/exit\t\t\tExit the program"); Console.WriteLine(""); Console.ReadLine(); } }
Conclusion
This isn’t a complicated topic, honestly. It is one, however, that requires a systematic approach. The power of console applications will not be questioned by those that have used them before and using the tactics touched on in this article, the sky is the limit.