namespace SampleApp
{
    using System;
    using System.Collections.Generic;
    using CommandLine;
    using CommandLine.Text;

    internal sealed class Program
    {
        private static readonly HeadingInfo headingInfo = new HeadingInfo("sampleapp", "1.2.5");

        private sealed class Options
        {
            [Option("r", "read",
                    Required = true,
                    HelpText = "Input file with data to process.")]
            public string InputFile = String.Empty;

            [Option("w", "write",
                    HelpText = "Output file with processed data (otherwise standard output).")]
            public string OutputFile = String.Empty;

            [Option(null, "calculate",
                    HelpText = "Add results in bottom of tabular data.")]
            public bool Calculate = false;

            [Option("v", null,
                    HelpText = "Verbose level. Range: from 0 to 2.")]
            public int VerboseLevel = 0;

            [Option("i", null,
                   HelpText = "If file has errors don't stop processing.")]
            public bool IgnoreErrors = false;

            [Option("j", "jump",
                    HelpText = "Data processing start offset.")]
            public double StartOffset = 0;

            [ValueList(typeof(Liststring))]
            public IList<string> DefinitionFiles = null;

            [HelpOption(
                    HelpText = "Dispaly this help screen.")]
            public string GetUsage()
            {
                HelpText help = new HelpText(Program.headingInfo);
                help.Copyright = new CopyrightInfo("Giacomo Stelluti Scala", 2005, 2007);
                help.AddPreOptionsLine("This is free software. You may redistribute copies of it under the terms of");
                help.AddPreOptionsLine("the MIT License <http://www.opensource.org/licenses/mit-license.php>.");
                help.AddPreOptionsLine("Usage: SampleApp -rMyData.in -wMyData.out --calculate");
                help.AddPreOptionsLine(string.Format("       SampleApp -rMyData.in -i -j{0} file0.def file1.def", 9.7));
                help.AddOptions(this);
                return help;
            }
        }

        private static void Main(string[] args)
        {
            Options options = new Options();
            ICommandLineParser parser = new CommandLineParser();
            if (parser.ParseArguments(args, options, Console.Error))
            {
                Console.WriteLine("Verbose Level: {0}", (options.VerboseLevel < 0 || options.VerboseLevel > 2) ? "#invalid value#" : options.VerboseLevel.ToString());
                Console.WriteLine();
                Console.WriteLine("Reading input file: {0} ...", options.InputFile);
                foreach (string defFile in options.DefinitionFiles)
                    Console.WriteLine("  using definition file: {0}", defFile);
                Console.WriteLine("  start offset: {0}", options.StartOffset);
                Console.WriteLine("  tabular data computation: {0}", options.Calculate.ToString().ToLowerInvariant());
                Console.WriteLine("  on errors: {0}", options.IgnoreErrors ? "continue" : "stop processing");
                Console.WriteLine();
                if (options.OutputFile.Length > 0)
                {
                    headingInfo.WriteMessage(string.Format("Writing elaborated data: {0} ...", options.OutputFile));
                }
                else
                {
                    headingInfo.WriteMessage("Elaborated data:");
                    Console.WriteLine("[...]");
                }
                Environment.Exit(0);
            }
            else
            {
                Environment.Exit(1);
            }
        }
    }
}

Last edited Dec 27, 2008 at 12:04 PM by gsscoder, version 5

Comments

No comments yet.