Click here to Skip to main content
15,887,596 members
Articles / Programming Languages / XML

Adaptive Console Framework - Build Your Console Application on the Fly

Rate me:
Please Sign up or sign in to vote.
3.00/5 (1 vote)
18 Sep 2008CDDL9 min read 31K   286   15  
Introduces the goal and use of the Adaptive Console Framework.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using AdaptiveConsole;

namespace Catool.Contracts
{
    [
        OptionContract(
            Type=ContractType.Patternized,
            Description="Performs arithmetic calculation",
            Parameters=2)
    ]
    public class CalculationContract : OptionContractBase
    {
        [
            Option(
                Name="/method;/m",
                Type=OptionType.SingleValue,
                Description="Specifies the calculation method\n"+
                            "\t\tadd: Performs an addition calculation\n"+
                            "\t\tsub: Performs a subtraction calculation\n"+
                            "\t\tmul: Performs a multiplication calculation\n"+
                            "\t\tdiv: Performs a division calculation",
                Required=true)
        ]
        public string Method { get; set; }

        [
            Option(
                Name="/nologo",
                Type=OptionType.Switch,
                Description="Suppress the display of banner information")
        ]
        public bool NoLogo { get; set; }

        public override void Execute(
            ConsoleApplicationBase consoleApplication, 
            IList<ArgumentInfo> args)
        {
            var integers = from arg in args
                           where arg.Category == ArgumentCategory.Parameter
                           select arg.Argument;
            int num1, num2, result;
            try
            {
                num1 = int.Parse(integers.ElementAt(0));
                num2 = int.Parse(integers.ElementAt(1));
            }
            catch
            {
                consoleApplication.PrintHelpMessage();
                return;
            }
            if (!this.NoLogo)
                consoleApplication.PrintBanner();
            try
            {
                switch (this.Method.ToUpper())
                {
                    case "ADD":
                        result = num1 + num2;
                        break;
                    case "SUB":
                        result = num1 - num2;
                        break;
                    case "MUL":
                        result = num1 * num2;
                        break;
                    case "DIV":
                        result = num1 / num2;
                        break;
                    default:
                        consoleApplication.PrintHelpMessage();
                        return;
                }
                Console.WriteLine(result);
            }
            catch (DivideByZeroException)
            {
                Console.WriteLine("Divided by zero error caught.");
                return;
            }
        }
    }
}

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 Common Development and Distribution License (CDDL)


Written By
Architect Prosource Development
China China
I have more than 13 years' experience in software development and more than 5 years' working experience in software industry. I'm the National Certified System Analyst and now I'm the consultant of China System Analyst Institution. I also got the MCP/MCAD certificate on .NET technology in the year 2004.
I'm very interested in system architect and analysis, and also interested in .NET technologies. For my blog please refer to http://www.sunnychen.org.

Comments and Discussions