Click here to Skip to main content
15,893,588 members
Articles / Programming Languages / C# 4.0

Implementing Command Execution in a Console Application

Rate me:
Please Sign up or sign in to vote.
4.83/5 (6 votes)
17 Jan 2013CPOL5 min read 29.3K   349   11  
Describes a simple yet nice approach to make an interactive console application
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;

namespace PossibleSolution
{


    class Program
    {
        static void printCounters1()
        {
            Console.WriteLine("Printing first counters");
        }

        static void printCounters2()
        {
            Console.WriteLine("Printing second counters");
        }

        static void printHelp()
        {
            Console.WriteLine("Supported: stop, print first counters (pfc), print second counters (psc)");
        }

        static void Main(string[] args)
        {
            Dictionary<string, string> domain = new Dictionary<string, string> { { "first", "value 1" }, { "second", "value 2" } };
            bool shouldfinish = false;
            ConsoleCommandHolder holder = new ConsoleCommandHolder();
            holder.AddRange( new ConsoleCommand[]{ new SimpleCommand("Stop application", new string[]{"stop"}, 
                (x)=>
                    {
                        Console.WriteLine("Do you really want to stop? (type 'yes' to stop)");
                        string acceptString = Console.ReadLine();
                        if (acceptString == "yes")
                        {
                            shouldfinish = true;
                        }

                    }), 
                    new SimpleCommand("Print first command", new string[]{"print first command", "pfc"},(x)=>printCounters1() ),
                    new SimpleCommand("Print second command", new string[]{"print second command", "psc"},(x)=>printCounters2() ), 
                    new SimpleCommand("Print dictionary", new string[]{"print dictionary", "pd"},
                        (x)=>
                            {
                                foreach (var next in domain)
                                    Console.WriteLine("{0} -> {1}", next.Key, next.Value);
                            }), 
                    new AddCommand(domain), 
            });
            while (shouldfinish == false)
            {
                string command = Console.ReadLine();
                ConsoleCommand toExecute = holder.Find(command);
                if(toExecute != null)
                    toExecute.Do(command);
                else Console.WriteLine(holder);
            }
            Console.WriteLine("Disposing domain and stopping");
            Console.ReadKey();
        }
    }
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer Crypton-M
Ukraine Ukraine
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions