Page 1 of 1

Command Line Interface Building A CLI from scratch

#1 Crazy_Learner  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 10
  • View blog
  • Posts: 143
  • Joined: 25-July 09

Post icon  Posted 08 September 2009 - 03:10 PM

I know many of you have used or considered using NConsole, which by all standards is a very good program, but lets say that you want to build your own interface that accepts flags with arguments. For instance if i were to use a statement like -csv "pathtofile.csv" \write the the -cvs and \write are flags of the program. I am going to show you an easy way to create this type of interface.

PS. I know the code is not optimized, it is made in a way to increase the understanding of it

Step 1: Determine Flags
My suggestion is that you take some time and write down all the flags that you want to use and what they will all do, even if you have got to the point of actually making the functions (Look At #7 Here). After detriming flags youll want to decide if those flags have something attached or not, line for instance the \write flag could have a path added to it, or you could make it default it to some location and thus wont need one. its all up to you.

Step 2: Setting Up The Interface
This CLI (Command Line Interface) is built in the main function, but you could make it anywhere you want, you would just have to pass the array of arguments on to the method

Step 2.1 : Setting Up The Main Method
public static void Main(string[] args)
{

}



That is pretty simple, its the main entry point of the program

Step 2.2 : Creating Booleans
Now this is a major, but small step. This whole design is based of one thing "booleans AKA bool " for this you wll want to create a boolean for every flag that you wanted to use. For example if i am using the flags "/help" "/write" and "-csv" then my code would look like this:

bool help_flag = false; // the "/help" flag
bool write_flag = false; // the "/write" flag
bool csv_flag = false; // the "-csv" flag

bool csv_superflag = false; //since this is the main flag the program looks for it will set this
// then set the value of csv_flag to false so that the arguments can continue without any
// csv flag misleads



Step 2.3 : Creating The Strings
This is and isnt important dependent on your flags, for this example it is. I have decided in notepad where i listed all my flags that the -csv flag and the \write flag will have parameters attached to them so we need to store those parameters in a string.

string csv_parameter = string.Empty;
string write_parameter = string.Empty;



I made them empty so that the values would be empty. Duh :P

Step 2.4 : Creating The ForEach Loop
This is where all the work by the interface is really done. This loop is going to take each argument one by one. Now each argument is considered to 1 word or until a space, unless its in quotes. so this is where the booleans determine if a flag is being used

foreach(string argument in args)
{
	if(argument == "-csv")
	{
		csv_flag = true;
		continue;
	}
	if(csv_flag == true)
	{
		csv_flag = false;
		csv_parameter = argument;
		csv_superflag = true;
		continue;
	}
	if(argument == "/write")
	{
		write_flag = true;
		continue;
	}
	if(write_flage == true)
	{
		write_flag = false;
		write_parameter = argument;
	}
}

if(csv_superflag == true)
{
	RunMyCSVProgram(parameter_csv,parameter_write);
}



Now if look at the code youll notice that everytime the flag was used it shoot up a boolean to true, then when the next argument came (the parameter) it would set it as false and set the argument equal to the string parameter_(theflag). Simple but effective. Thats pretty much the whole concept behind a "SIMPLE" CLI.

--------------------------------------------------- Beyond this line is the full source Code -------------------------------------------------------------------
using System;

public static void Main(string[] args)
{
	bool help_flag = false; // the "/help" flag
	bool write_flag = false; // the "/write" flag
	bool csv_flag = false; // the "-csv" flag
	
	bool csv_superflag = false; //since this is the main flag the program looks for it will set this
	// then set the value of csv_flag to false so that the arguments can continue without any
	// csv flag misleads
	
	string csv_parameter = string.Empty;
	string write_parameter = string.Empty;

	foreach(string argument in args)
	{
		if(argument == "-csv")
		{
			csv_flag = true;
			continue;
		}
		if(csv_flag == true)
		{
			csv_flag = false;
			csv_parameter = argument;
			csv_superflag = true;
			continue;
		}
		if(argument == "/write")
		{
			write_flag = true;
			continue;
		}
		if(write_flage == true)
		{
			write_flag = false;
			write_parameter = argument;
		}
	}

	if(csv_superflag == true)
	{
		RunMyCSVProgram();
	}
}



Is This A Good Question/Topic? 0
  • +

Replies To: Command Line Interface

#2 MentalFloss  Icon User is offline

  • "ADDICTED"[2:5]
  • member icon

Reputation: 525
  • View blog
  • Posts: 1,397
  • Joined: 02-September 09

Posted 06 November 2009 - 12:30 AM

I read your tutorial and didn't really like your usage of the "flags", so I modified it a bit.
It's just another way to do it. Maybe someone will enjoy it.

Oh and, I don't know why you bothered setting the flags to false again on conditions, but I kept it around.

using System;

class Program {

	// Implement the desired behavior based on which flags are set.
	// These are the possible flags for the program.
	[Flags]
	public enum behaviorFlags {
		None = 0,  // 0000
		Help = 1,  // 0001
		Write = 2, // 0010
		CSV = 4,   // 0100
		Super = 8  // 1000
	}

	public static void Main(string[] args) {

		behaviorFlags behavior = behaviorFlags.None;


		string csv_parameter = string.Empty;
		string write_parameter = string.Empty;

		foreach (string argument in args) {
			if (argument == "-csv") {
				// Set the CSV flag...
				behavior |= behaviorFlags.CSV;
				continue;
			}
			// Check if the flag is set for CSV.
			if ((behavior & behaviorFlags.CSV) == behaviorFlags.CSV) {
				// I guess clear it for some reason by anding the inverse.
				behavior &= ~behaviorFlags.CSV;
				csv_parameter = argument;
				// Set the superflag
				behavior |= behaviorFlags.Super;
				continue;
			}
			if (argument == "/write") {
				// Set the write flag
				behavior |= behaviorFlags.Write;
				continue;
			}

			// Check if write flag is set..
			if ((behavior & behaviorFlags.Write) == behaviorFlags.Write) {
				// If it is, unset it.. I don't know why.
				behavior &= ~behaviorFlags.Write;
				write_parameter = argument;
			}
		}

		// Check if super flag is set. If so, we can run the program.
		if ((behavior & behaviorFlags.Super) == behaviorFlags.Super) {
			//RunMyCSVProgram();
			Console.WriteLine("Program should launch now.");
		}
	}
}


Was This Post Helpful? 1
  • +
  • -

#3 Mazola  Icon User is offline

  • New D.I.C Head

Reputation: -1
  • View blog
  • Posts: 1
  • Joined: 10-December 09

Posted 27 December 2009 - 09:19 PM

Hello staff would like to know why I downloaded the "SerialPortCommunication" and has an unknown file of 102KB that it will be to fasendo wrong.
Sorry my English I am Brazilian and it is google
Thank you!
Was This Post Helpful? -1
  • +
  • -

Page 1 of 1