Beispiel #1
0
        /// <summary>
        /// Parses the Command-Line Args or Shows the Help, whichever is appropriate.
        /// Appropriateness is determined by whether there are Remaining Args, or
        /// whether the Help option was specified.
        /// </summary>
        /// <param name="writer"></param>
        /// <param name="args"></param>
        /// <returns>true when parsing was successful and no help was requested.</returns>
        /// <remarks>Which, by simplifying the model in SOLID-, DRY-style, the need
        /// for many in the way of helpers vanishes altogether.</remarks>
        public bool TryParseOrShowHelp(TextWriter writer, params string[] args)
        {
            var remaining = _optionSet.Parse(args);

            //Not-parsed determined here.
            var parsed = !(remaining.Any() ||
                           _optionSet.GetMissingVariables().Any() ||
                           _helpInfo.Help.Enabled);

            //Show-error when any-remaining or missing-variables.
            if (remaining.Any() || _optionSet.GetMissingVariables().Any())
            {
                writer.WriteLine("{0}: error parsing arguments:", ConsoleName);
            }
            else if (!parsed)
            {
                writer.WriteLine("{0} options:", ConsoleName);
            }

            //Show-help when not-parsed.
            if (!parsed)
            {
                _optionSet.WriteOptionDescriptions(writer);
            }

            return(parsed);
        }
        public void Should_Detect_Required_VariableLists()
        {
            var optionSet = new RequiredValuesOptionSet();
            var n         = optionSet.AddRequiredVariableList <string>("n", "");
            var a         = optionSet.AddRequiredVariableList <int>("a", "");
            var m         = optionSet.AddRequiredVariableList <string>("m", "");

            //TODO: Screaming for an NUnit-test-case-coverage.
            var args = "-n FindThisString -n:Findit2 -n:Findit3 -a2 -a3 -a5565 -a:23".Split(' ');

            optionSet.Parse(args);

            Action <IEnumerable <string> > verifyN = x =>
            {
                // ReSharper disable PossibleMultipleEnumeration
                Assert.AreEqual(3, x.Count());
                Assert.IsTrue(x.Contains("FindThisString"));
                Assert.IsTrue(x.Contains("Findit2"));
                Assert.IsTrue(x.Contains("Findit3"));
                // ReSharper restore PossibleMultipleEnumeration
            };

            Action <IEnumerable <int> > verifyA = x =>
            {
                // ReSharper disable PossibleMultipleEnumeration
                Assert.AreEqual(4, x.Count());
                Assert.IsTrue(x.Contains(2));
                Assert.IsTrue(x.Contains(3));
                Assert.IsTrue(x.Contains(5565));
                Assert.IsTrue(x.Contains(23));
                // ReSharper restore PossibleMultipleEnumeration
            };

            Action <IEnumerable <string> > verifyM = x =>
            {
                // ReSharper disable PossibleMultipleEnumeration
                Assert.AreEqual(0, x.Count());
                Assert.AreEqual(1, optionSet.GetMissingVariables().Count());
                // ReSharper restore PossibleMultipleEnumeration
            };

            verifyA(a);
            verifyA(a.Values);

            verifyN(n);
            verifyN(n.Values);

            verifyM(m);
            verifyM(m.Values);
        }
        public void Should_Detect_Required_Variables()
        {
            var optionSet = new RequiredValuesOptionSet();
            var name      = optionSet.AddRequiredVariable <string>("n", "");
            var age       = optionSet.AddRequiredVariable <int>("a", "");
            var age2      = optionSet.AddRequiredVariable <int>("b", "");
            var age3      = optionSet.AddRequiredVariable <int>("c", "");

            //TODO: Screaming for NUnit-test-case-coverage.
            var args = "-n FindThisString".Split(' ');

            optionSet.Parse(args);

            /* TODO: Might could (should) also verify that each of the missing ones,
             * as well as found ones, are either there are not there. */
            Assert.AreEqual(3, optionSet.GetMissingVariables().Count());

            Assert.AreEqual("FindThisString", name);
        }