Exemplo n.º 1
0
        public void BasicStrictCaseTest()
        {
            ICommandLineSettings settings = CommandLineParser <TestClass> .CommandLineSettings();

            settings.CaseMatching = StringComparison.CurrentCulture;

            IArgumentDescriptor[] descriptors = CommandLineParser <TestClass> .GetDescriptors(settings).ToArray();

            Assert.AreEqual(3, descriptors.Length);

            Assert.AreEqual("String", descriptors[0].Name);
            Assert.AreEqual("s", descriptors[0].ShortName);
            Assert.AreEqual(typeof(string), descriptors[0].MemberType);
            Assert.AreEqual("a random string", descriptors[0].Help);

            Assert.AreEqual("Color", descriptors[1].Name);
            Assert.AreEqual("c", descriptors[1].ShortName);
            Assert.AreEqual(typeof(Colors), descriptors[1].MemberType);
            Assert.AreEqual("Red White or Blue", descriptors[1].Help);

            Assert.AreEqual("Verbose", descriptors[2].Name);
            Assert.AreEqual("v", descriptors[2].ShortName);
            Assert.AreEqual(typeof(bool), descriptors[2].MemberType);
            Assert.AreEqual("True for verbose output", descriptors[2].Help);
        }
Exemplo n.º 2
0
        /// <summary>
        /// The primary parsing method - given a <c>string[] args</c> it returns the populated application setting object
        /// </summary>
        /// <param name="args">The command line arguments</param>
        /// <param name="start">The index in args to start parsing at - typically 0 or 1</param>
        /// <param name="settings">The command line settings</param>
        /// <returns>A populated instance of the application command line settings class</returns>
        internal static Context <T> Parse(string[] args, int start, ICommandLineSettings settings)
        {
            Context <T> context = new Context <T> {
                Settings = settings
            };

            Processor <T> processor = new Processor <T>(context);

            processor.Parse(args, start);
            if (context.Exceptions.Count > 0)
            {
                throw new AggregateException("one or more exceptions parsing command line", context.Exceptions);
            }

            return(context);
        }
Exemplo n.º 3
0
        /// <summary>
        /// Returns the arguments descriptors from the application settings class
        /// </summary>
        /// <param name="settings">The command line settings</param>
        /// <returns>An enumerator of all the application settings</returns>
        internal static IEnumerable <ArgumentDescriptor> GetArgumentDescriptors(ICommandLineSettings settings)
        {
            if (settings == null)
            {
                throw new ArgumentNullException(nameof(settings), "Argument cannot be null");
            }

            Context <T> context = new Context <T> {
                Settings = settings
            };
            Processor <T> processor = new Processor <T>(context);

            processor.BuildDescriptors();
            var descriptors = processor.descriptors;

            descriptors.Sort((x, y) => string.Compare(Processor <T> .SortName(x), Processor <T> .SortName(y), StringComparison.CurrentCulture));
            return(descriptors);
        }
Exemplo n.º 4
0
        /// <summary>
        /// Convenience overload when we start parsing at the first element in args[]
        /// </summary>
        /// <param name="args">The command line arguments</param>
        /// <param name="start">The index in args to start parsing at - typically 0 or 1</param>
        /// <param name="settings">The command line settings</param>
        /// <returns>A populated instance of the application command line settings class</returns>
        public static ValueTuple <T, List <string> > Parse(string[] args, int start, ICommandLineSettings settings)
        {
            if (args == null)
            {
                throw new ArgumentNullException(nameof(args), "Argument cannot be null");
            }

            if (settings == null)
            {
                throw new ArgumentNullException(nameof(settings), "Argument cannot be null");
            }

            Context <T> result = Processor <T> .Parse(args, start, settings);

            if (result.Exceptions.Count > 0)
            {
                throw new AggregateException("Failed to parse command line", result.Exceptions);
            }

            return(new ValueTuple <T, List <string> >(result.CommandLineSwitches, result.CommandLineNonSwitches));
        }
Exemplo n.º 5
0
 /// <summary>
 /// get descriptors - allows application to build a help command
 /// </summary>
 /// <param name="settings">The appropriate command line settings</param>
 /// <returns>An enumerator over the command line settings</returns>
 public static IEnumerable <IArgumentDescriptor> GetDescriptors(ICommandLineSettings settings)
 {
     return(Processor <T> .GetArgumentDescriptors(settings));
 }