C# School Assignment? Project Due Tomorrow? Chat LIVE With A Programming Expert!

Welcome to Dream.In.Code
Become a C# Expert!

Join 307,131 C# Programmers for FREE! Get instant access to thousands of C# experts, tutorials, code snippets, and more! There are 2,060 people online right now. Registration is fast and FREE... Join Now!




Command Line Interface

 
Reply to this topicStart new topic

> Command Line Interface, Building A CLI from scratch

Crazy_Learner
Group Icon



post 8 Sep, 2009 - 02:10 PM
Post #1


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
CODE

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:

CODE

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.

CODE

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


I made them empty so that the values would be empty. Duh tongue.gif

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

CODE

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 -------------------------------------------------------------------
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();
    }
}
Go to the top of the page
+Quote Post


Register to Make This Ad Go Away!

MentalFloss
**



post 5 Nov, 2009 - 11:30 PM
Post #2
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.

CODE

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.");
        }
    }
}
Go to the top of the page
+Quote Post


Fast ReplyReply to this topicStart new topic
1 User(s) are reading this topic (1 Guests and 0 Anonymous Users)
0 Members:

 


Lo-Fi Version Time is now: 11/21/09 02:43PM

Live C# Help!

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter Fan Us On Facebook

C# Tutorials

Reference Sheets

C# Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month