Exemple #1
0
		public void NumberSwitchesParsedProperty()
		{
			const string Name1 = "arg1";
			const string Name2 = "arg2";

			string[] arguments = new string[]
			{
				Switch.GetPrefixedName(Name1),
				Switch.GetPrefixedName(Name2)
			};

			SwitchCollection switches = new SwitchCollection();

			switches.Add(Name1);
			switches.Add(Name2);

			ArgumentParser parser = new ArgumentParser(arguments, switches);

			Assert.AreEqual(NoSwitches, parser.NumberSwitchesParsed);
			Assert.AreEqual(NoSwitches, m_emptyParser.NumberSwitchesParsed);

			parser.Parse();

			Assert.AreEqual(switches.Count, parser.NumberSwitchesParsed);

			Assert.IsTrue(parser.IsParsed(Name1));
			Assert.IsTrue(parser.IsParsed(Name2));
		}
Exemple #2
0
        /// <summary>
        /// Initialize command-line argument parser and parse given
        /// command-line arguments.
        /// </summary>
        /// <param name="arguments">
        /// Array of strings representing command-line arguments to parse.
        /// </param>
        /// <returns>
        /// True if help should be printed, false otherwise.
        /// </returns>
        static bool ParseArguments(string[] arguments)
        {
            DefineSwitches();

            s_parser = new ArgumentParser(arguments, Switches);

            s_parser.Parse();

            if (s_parser.IsParsed(SwitchNames.Help) ||
                s_parser.NoneParsed(SwitchNames.ProjectFiles,
                SwitchNames.SolutionFiles, SwitchNames.SourceFiles))
            {
                return true;
            }

            return false;
        }
Exemple #3
0
		public void ParseTwice()
		{
			const string Name1 = "arg1";
			const string Name2 = "arg2";

			string[] arguments = new string[]
			{
				Switch.GetPrefixedName(Name1),
				Switch.GetPrefixedName(Name2)
			};

			SwitchCollection switches = new SwitchCollection();

			switches.Add(Name1);
			switches.Add(Name2);

			ArgumentParser parser = new ArgumentParser(arguments, switches);

			Assert.IsFalse(parser.IsParsed(Name1));
			Assert.IsFalse(parser.IsParsed(Name2));

			parser.Parse();

			// No exceptions should be thrown on second successive call.
			Assert.DoesNotThrow(delegate { parser.Parse(); });

			Assert.IsTrue(parser.IsParsed(Name1));
			Assert.IsTrue(parser.IsParsed(Name2));
		}
Exemple #4
0
		public void ParseUndefinedSwitch()
		{
			const string Name1 = "arg1";
			const string Name2 = "arg2";
			const string UndefinedName = "arg3";

			string[] arguments = new string[]
			{
				Switch.GetPrefixedName(Name1),
				Switch.GetPrefixedName(UndefinedName),
				Switch.GetPrefixedName(Name2)
			};

			SwitchCollection switches = new SwitchCollection();

			switches.Add(Name1);
			switches.Add(Name2);

			ArgumentParser parser = new ArgumentParser(arguments, switches);

			Assert.IsFalse(parser.IsParsed(Name1));
			Assert.IsFalse(parser.IsParsed(Name2));
			Assert.IsFalse(parser.IsParsed(UndefinedName));

			Assert.Throws<ParsingException>(delegate { parser.Parse(); });

			Assert.IsFalse(parser.IsParsed(Name2));
			Assert.IsFalse(parser.IsParsed(UndefinedName));

			Assert.IsTrue(parser.IsParsed(Name1));
		}
Exemple #5
0
		public void ParseSwitchWithMissingArguments()
		{
			const string Name1 = "arg1";
			const string Name2 = "arg2";

			string[] arguments = new string[]
			{
				Switch.GetPrefixedName(Name1),
				Switch.GetPrefixedName(Name2)
			};

			SwitchCollection switches = new SwitchCollection();

			switches.Add(Name1);
			switches.Add(Name2, NoLongName, NoDescription, HasArguments, !IsRequired);

			ArgumentParser parser = new ArgumentParser(arguments, switches);

			Assert.IsFalse(parser.IsParsed(Name1));
			Assert.IsFalse(parser.IsParsed(Name2));

			Assert.Throws<ParsingException>(delegate { parser.Parse(); });

			Assert.IsTrue(parser.IsParsed(Name1));
			Assert.IsTrue(parser.IsParsed(Name2));
		}
Exemple #6
0
		public void ParseSwitchWithExcessArguments()
		{
			const string Name1 = "name1";
			const string Name2 = "name2";
			const string Value1 = "value1";
			const string Value2 = "value2";
			const string Value3 = "value3";

			string[] arguments = new string[]
			{
				Switch.GetPrefixedName(Name1),
				Value1,
				Value2,
				Switch.GetPrefixedName(Name2)
			};

			string[] argumentNames = new string[] { "arg1", "arg2" };

			string[] enoughValues = new string[] { Value1, Value2 };

			string[] excessValues = new string[] { Value1, Value2, Value3 };

			SwitchCollection switches = new SwitchCollection();

			switches.Add(Name1, NoLongName, NoDescription, IsRequired, argumentNames);
			switches.Add(Name2);

			ArgumentParser parser = new ArgumentParser(arguments, switches);

			Assert.IsFalse(parser.IsParsed(Name1));
			Assert.IsFalse(parser.IsParsed(Name2));

			// No exceptions should be thrown. Excess arguments should be ignored.
			Assert.DoesNotThrow(delegate { parser.Parse(); });

			Assert.AreEqual(enoughValues, parser.GetValues(Name1));

			Assert.AreNotEqual(excessValues, parser.GetValues(Name1));

			Assert.IsTrue(parser.IsParsed(Name1));
			Assert.IsTrue(parser.IsParsed(Name2));
		}
Exemple #7
0
		public void ParseSwitchWithArguments()
		{
			const string Name1 = "name1";
			const string Name2 = "name2";
			const string Value1 = "value1";
			const string Value2 = "value2";

			string[] arguments = new string[]
			{
				Switch.GetPrefixedName(Name1),
				Value1,
				Value2,
				Switch.GetPrefixedName(Name2)
			};

			string[] argumentNames = new string[] { "arg1", "arg2" };

			string[] values = new string[] { Value1, Value2 };

			SwitchCollection switches = new SwitchCollection();

			switches.Add(Name1, NoLongName, NoDescription, IsRequired, argumentNames);
			switches.Add(Name2);

			ArgumentParser parser = new ArgumentParser(arguments, switches);

			Assert.IsFalse(parser.IsParsed(Name1));
			Assert.IsFalse(parser.IsParsed(Name2));

			parser.Parse();

			Assert.AreEqual(values, parser.GetValues(Name1));

			Assert.IsTrue(parser.IsParsed(Name1));
			Assert.IsTrue(parser.IsParsed(Name2));
		}
Exemple #8
0
		public void ParseNoSwitches()
		{
			const string ArgumentName1 = "arg1";
			const string ArgumentName2 = "arg2";
			const string Name1 = "name1";
			const string Name2 = "name2";

			string[] arguments = new string[]
			{
				ArgumentName1,
				ArgumentName2
			};

			SwitchCollection switches = new SwitchCollection();

			switches.Add(Name1);
			switches.Add(Name2);

			ArgumentParser parser = new ArgumentParser(arguments, switches);

			Assert.IsFalse(parser.IsParsed(ArgumentName1));
			Assert.IsFalse(parser.IsParsed(ArgumentName2));
			Assert.IsFalse(parser.IsParsed(Name1));
			Assert.IsFalse(parser.IsParsed(Name2));

			parser.Parse();

			Assert.IsFalse(parser.IsParsed(ArgumentName1));
			Assert.IsFalse(parser.IsParsed(ArgumentName2));
			Assert.IsFalse(parser.IsParsed(Name1));
			Assert.IsFalse(parser.IsParsed(Name2));
		}
Exemple #9
0
		public void Parse()
		{
			const string Name1 = "arg1";
			const string Name2 = "arg2";

			string[] arguments = new string[]
			{
				Switch.GetPrefixedName(Name1),
				Switch.GetPrefixedName(Name2)
			};

			SwitchCollection switches = new SwitchCollection();

			switches.Add(Name1);
			switches.Add(Name2);

			ArgumentParser parser = new ArgumentParser(arguments, switches);

			Assert.IsFalse(parser.IsParsed(EmptyName));
			Assert.IsFalse(parser.IsParsed(Name1));
			Assert.IsFalse(parser.IsParsed(Name2));
			Assert.IsFalse(parser.IsParsed(NoName));
			Assert.IsFalse(m_emptyParser.IsParsed(EmptyName));
			Assert.IsFalse(m_emptyParser.IsParsed(Name1));
			Assert.IsFalse(m_emptyParser.IsParsed(Name2));
			Assert.IsFalse(m_emptyParser.IsParsed(NoName));

			parser.Parse();

			Assert.IsFalse(parser.IsParsed(EmptyName));
			Assert.IsFalse(parser.IsParsed(NoName));

			Assert.IsTrue(parser.IsParsed(Name1));
			Assert.IsTrue(parser.IsParsed(Name2));
		}