/// <summary> /// Registers an array of synonymous switches (aliases) with the /// command line parser /// </summary> /// <remarks> /// Switches named here should not include a switch prefix character /// such as a slash or minus. This is handled by the parser in /// <see cref="CommandLineSwitchRecord"/>. /// <para> /// The first switch in the array is considered to be the actual /// name of the switch while those that follow create the aliases /// for the switch /// </para> /// </remarks> /// <example>For example: /// <code> /// parser = new CommandLineParser(System.Environment.CommandLine); /// parser.AddSwitch(new string[] { "help", @"usage" }, "show help"); //Add a switch with an alias /// parser.Parse(); /// </code> /// </example> /// <param name="names">Name and aliases of the switch.</param> /// <param name="description">Usage description for the switch.</param> public void AddSwitch( string[] names, string description ) { if ( _switches == null ) _switches = new System.Collections.ArrayList(); CommandLineSwitchRecord rec = new CommandLineSwitchRecord( names[0], description ); for ( int s=1; s<names.Length; s++ ) rec.AddAlias( names[s] ); _switches.Add( rec ); }
/// <summary> /// Initializes a new instance of the CommandLineParser class /// </summary> /// <example>For example: /// <code> /// class Class1 /// { /// private bool _bool; /// /// [CommandLineSwitch("bool","Provide boolean value")] /// [CommandLineAlias(new string [] {"boolean","b"})] /// public bool Bool /// { /// get { return _bool; } /// set { this._bool = value; } /// } /// /// [STAThread] /// static void Main(string[] args) /// { /// Class1 c = new Class1(); /// /// CommandLineParser clp = new CommandLineParser(System.Environment.CommandLine,c); /// clp.Parse(); /// /// Console.WriteLine("bool={0}",c.Bool); /// } /// } /// </code> /// </example> /// <param name="commandLine">The command line to parse when <see cref="Parse"/> is called.</param> /// <param name="classForAutoAttributes">The class that contains attributes from which switches are created.</param> public CommandLineParser( string commandLine, object classForAutoAttributes ) { _commandLine = commandLine; Type type = classForAutoAttributes.GetType(); System.Reflection.MemberInfo[] members = type.GetMembers(); for(int i=0; i<members.Length; i++) { object[] attributes = members[i].GetCustomAttributes(false); if(attributes.Length > 0) { CommandLineSwitchRecord rec = null; foreach ( Attribute attribute in attributes ) { if ( attribute is CommandLineSwitchAttribute ) { CommandLineSwitchAttribute switchAttrib = (CommandLineSwitchAttribute) attribute; // Get the property information. We're only handling // properties at the moment! if ( members[i] is System.Reflection.PropertyInfo ) { System.Reflection.PropertyInfo pi = (System.Reflection.PropertyInfo) members[i]; rec = new CommandLineSwitchRecord( switchAttrib.Name, switchAttrib.Description, pi.PropertyType ); // Map in the Get/Set methods. rec.SetMethod = pi.GetSetMethod(); rec.GetMethod = pi.GetGetMethod(); rec.PropertyOwner = classForAutoAttributes; // Can only handle a single switch for each property // (otherwise the parsing of aliases gets silly...) break; } } } // See if any aliases are required. We can only do this after // a switch has been registered and the framework doesn't make // any guarantees about the order of attributes, so we have to // walk the collection a second time. if ( rec != null ) { foreach ( Attribute attribute in attributes ) { if ( attribute is CommandLineAliasAttribute ) { CommandLineAliasAttribute aliasAttrib = (CommandLineAliasAttribute) attribute; foreach(string a in aliasAttrib.Alias) rec.AddAlias(a); } } } // Assuming we have a switch record (that may or may not have // aliases), add it to the collection of switches. if ( rec != null ) { if ( _switches == null ) _switches = new System.Collections.ArrayList(); _switches.Add( rec ); } } } }
/// <summary> /// Registers a switch with the command line parser /// </summary> /// <remarks> /// Switches named here should not include a switch prefix character /// such as a slash or minus. This is handled by the parser in /// <see cref="CommandLineSwitchRecord"/>. /// </remarks> /// <example>For example: /// <code> /// parser = new CommandLineParser(System.Environment.CommandLine); /// parser.AddSwitch(@"\?" , "show help"); /// parser.Parse(); /// </code> /// </example> /// <param name="name">The name of the switch.</param> /// <param name="description">Usage description for the switch.</param> public void AddSwitch( string name, string description ) { if ( _switches == null ) _switches = new System.Collections.ArrayList(); CommandLineSwitchRecord rec = new CommandLineSwitchRecord( name, description ); _switches.Add( rec ); }