This is the sample application that you can find in version 1.8 stable.

#region Using Directives
using System;
using System.Collections.Generic;
using System.Text;
using CommandLine;
using CommandLine.Text;
#endregion

namespace SampleApp
{
    sealed class Program
    {
        private static readonly HeadingInfo _headingInfo = new HeadingInfo("sampleapp", "1.8");

        private enum OptimizeFor
        {
            Unspecified,
            Speed,
            Accuracy
        }

        private sealed class Options
        {
            #region Standard Option Attribute
            [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 = null;

            [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;

            [Option(null, "optimize",
                HelpText = "Optimize for Speed|Accuracy.")]
            public OptimizeFor Optimization = OptimizeFor.Unspecified;
            #endregion

            #region Specialized Option Attribute
            [ValueList(typeof(List<string>))]
            public IList<string> DefinitionFiles = null;

            [OptionList("o", "operators", Separator = ';',
                HelpText = "Operators included in processing (+;-;...)." +
                " Separate each operator with a semicolon." +
                " Do not include spaces between operators and separator.")]
            public IList<string> AllowedOperators = null;
            
            [HelpOption(
                    HelpText = "Dispaly this help screen.")]
            public string GetUsage()
            {
                var help = new HelpText(Program._headingInfo);
                help.AdditionalNewLineAfterOption = true;
                help.Copyright = new CopyrightInfo("Giacomo Stelluti Scala", 2005, 2009);
                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.AddPreOptionsLine("       SampleApp -rMath.xml -wReport.bin -o *;/;+;-");
                help.AddOptions(this);

                return help;
            }
            #endregion
        }

        /// <summary>
        /// Application's Entry Point.
        /// </summary>
        /// <param name="args">Command Line Arguments splitted by the System.</param>
        private static void Main(string[] args)
        {
            var options = new Options();
            ICommandLineParser parser = new CommandLineParser(new CommandLineParserSettings(Console.Error));
            if (!parser.ParseArguments(args, options))
                Environment.Exit(1);

            DoCoreTask(options);

            Environment.Exit(0);
        }

        private static void DoCoreTask(Options options)
        {
            if (options.VerboseLevel == null)
                Console.Write("verbose [off]");
            else
                Console.WriteLine("verbose [on]: {0}", (options.VerboseLevel < 0 || options.VerboseLevel > 2) ? "#invalid value#" : options.VerboseLevel.ToString());
            Console.WriteLine();
            Console.WriteLine("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("  optimize for: {0}", options.Optimization.ToString().ToLowerInvariant());
            if (options.AllowedOperators != null)
            {
                var builder = new StringBuilder();
                builder.Append("  allowed operators: ");
                foreach (string op in options.AllowedOperators)
                {
                    builder.Append(op);
                    builder.Append(", ");
                }
                Console.WriteLine(builder.Remove(builder.Length - 2, 2).ToString());
            }
            Console.WriteLine();
            if (options.OutputFile.Length > 0)
                _headingInfo.WriteMessage(string.Format("writing elaborated data: {0} ...", options.OutputFile));
            else
            {
                _headingInfo.WriteMessage("elaborated data:");
                Console.WriteLine("[...]");
            }
        }
    }
}

OptionList attribute

In this sample the OptionList attribute uses a semicolon (;) as separator and it suggests the user to specify characters of mathematical operators. The semicolon and multiplication (*) operator can be pre-parsed from a *nix command line like bash (and the same could apply to Win32 PowerShell). So if you use something similar in your CLI-based application, you should warn the user to enclose this option within double quotes. I suggest you to write it in application documentation, help file and --help option text message.

Last edited Mar 9, 2010 at 6:42 AM by gsscoder, version 18

Comments

jcollum Mar 10, 2010 at 12:25 AM 
It would be helpful if you had sample command lines for SampleApp in here.

gsscoder Jan 8, 2009 at 7:53 PM 
OK, I will update this sample on each release. For any question about the use of the API, don't esitate to email me.

PatricioVidal May 13, 2008 at 12:40 AM 
Yes!

gsscoder Apr 28, 2008 at 8:25 AM 
Project Author >> Publish the sample application here was of any help?