// (c) Copyright Microsoft Corporation. // This source is subject to the Microsoft Public License (Ms-PL). // Please see http://go.microsoft.com/fwlink/?LinkID=131993 for details. // All other rights reserved. using System; namespace Microsoft.Test.CommandLineParsing { /// /// Provides a base class for the functionality that all commands must implement. /// /// /// /// The following example shows parsing of a command-line such as "Test.exe RUN /verbose /runId=10" /// into a strongly-typed Command, that can then be excuted. /// /// using System; /// using System.Linq; /// using Microsoft.Test.CommandLineParsing; /// /// public class RunCommand : Command /// { /// public bool? Verbose { get; set; } /// public int? RunId { get; set; } /// /// public override void Execute() /// { /// Console.WriteLine("RunCommand: Verbose={0} RunId={1}", Verbose, RunId); /// } /// } /// /// public class Program /// { /// public static void Main(string[] args) /// { /// if (String.Compare(args[0], "run", StringComparison.InvariantCultureIgnoreCase) == 0) /// { /// Command c = new RunCommand(); /// c.ParseArguments(args.Skip(1)); // or CommandLineParser.ParseArguments(c, args.Skip(1)) /// c.Execute(); /// } /// } /// } /// /// public abstract class Command { /// /// The name of the command. The base implementation is to strip off the last /// instance of "Command" from the end of the type name. So "DiscoverCommand" /// would become "Discover". If the type name does not have the string "Command" in it, /// then the name of the command is the same as the type name. This behavior can be /// overridden, but most derived classes are going to be of the form [Command Name] + Command. /// public virtual string Name { get { string typeName = this.GetType().Name; if (typeName.Contains("Command")) { return typeName.Remove(typeName.LastIndexOf("Command", StringComparison.Ordinal)); } else { return typeName; } } } /// /// Executes the command. /// public abstract void Execute(); } }