/// <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; }
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)); }
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)); }
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)); }
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)); }
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)); }
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)); }
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)); }
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)); }
public void NoneParsed() { const string Name1 = "arg1"; const string Name2 = "arg2"; const string Name3 = "arg3"; const string Name4 = "arg4"; const string Name5 = "arg5"; string[] arguments = new string[] { Switch.GetPrefixedName(Name1), Switch.GetPrefixedName(Name2) }; string[] switchNames1 = new string[] { Name1 }; string[] switchNames2 = new string[] { Name1, Name2 }; string[] switchNames3 = new string[] { Name1, Name2, Name3 }; string[] switchNames4 = new string[] { Name2, Name3, Name4 }; string[] switchNames5 = new string[] { Name4, Name5 }; SwitchCollection switches = new SwitchCollection(); switches.Add(switchNames2); ArgumentParser parser = new ArgumentParser(arguments, switches); Assert.IsTrue(parser.NoneParsed(switchNames1)); Assert.IsTrue(parser.NoneParsed(switchNames2)); Assert.IsTrue(parser.NoneParsed(switchNames3)); Assert.IsTrue(parser.NoneParsed(switchNames4)); Assert.IsTrue(parser.NoneParsed(switchNames5)); Assert.IsTrue(m_emptyParser.NoneParsed(switchNames1)); Assert.IsTrue(m_emptyParser.NoneParsed(switchNames2)); Assert.IsTrue(m_emptyParser.NoneParsed(switchNames3)); parser.Parse(); Assert.IsFalse(parser.NoneParsed(switchNames1)); Assert.IsFalse(parser.NoneParsed(switchNames2)); Assert.IsFalse(parser.NoneParsed(switchNames3)); Assert.IsFalse(parser.NoneParsed(switchNames4)); Assert.IsTrue(parser.NoneParsed(switchNames5)); }
public void GetValues() { const string Name1 = "name1"; const string Name2 = "name2"; const int NumberArguments = 2; const string Value1 = "value1"; const string Value2 = "value2"; const string Value3 = "value3"; const string Value4 = "value4"; string[] arguments = new string[] { Switch.GetPrefixedName(Name1), Value1, Value2, Switch.GetPrefixedName(Name2), Value3, Value4 }; SwitchCollection switches = new SwitchCollection(); switches.Add(Name1, NoLongName, NoDescription, NumberArguments, IsRequired); switches.Add(Name2, NoLongName, NoDescription, NumberArguments, IsRequired); ArgumentParser parser = new ArgumentParser(arguments, switches); Assert.IsNull(parser.GetValues(EmptyName)); Assert.IsNull(parser.GetValues(Name1)); Assert.IsNull(parser.GetValues(Name2)); Assert.IsNull(parser.GetValues(NoName)); Assert.IsNull(m_emptyParser.GetValues(EmptyName)); Assert.IsNull(m_emptyParser.GetValues(Name1)); Assert.IsNull(m_emptyParser.GetValues(Name2)); Assert.IsNull(m_emptyParser.GetValues(NoName)); parser.Parse(); Assert.AreEqual(new string[] { Value1, Value2 }, parser.GetValues(Name1)); Assert.AreEqual(new string[] { Value3, Value4 }, parser.GetValues(Name2)); Assert.IsNull(parser.GetValues(EmptyName)); Assert.IsNull(parser.GetValues(NoName)); }
public void GetValueWithInvalidArgumentNumber() { const int Argument0 = 0; const int Argument1 = 1; const int Argument2 = 2; const string ArgumentName = "arg1"; const string Name = "name1"; const string Value = "value1"; string[] arguments = new string[] { Switch.GetPrefixedName(Name), Value }; SwitchCollection switches = new SwitchCollection(); switches.Add(Name, NoLongName, NoDescription, IsRequired, ArgumentName); ArgumentParser parser = new ArgumentParser(arguments, switches); Assert.IsNull(parser.GetValue(EmptyName, Argument0)); Assert.IsNull(parser.GetValue(EmptyName, Argument1)); Assert.IsNull(parser.GetValue(EmptyName, Argument2)); Assert.IsNull(parser.GetValue(Name, Argument0)); Assert.IsNull(parser.GetValue(Name, Argument1)); Assert.IsNull(parser.GetValue(Name, Argument2)); Assert.IsNull(parser.GetValue(NoName, Argument0)); Assert.IsNull(parser.GetValue(NoName, Argument1)); Assert.IsNull(parser.GetValue(NoName, Argument2)); Assert.IsNull(m_emptyParser.GetValue(EmptyName, Argument0)); Assert.IsNull(m_emptyParser.GetValue(EmptyName, Argument1)); Assert.IsNull(m_emptyParser.GetValue(EmptyName, Argument2)); Assert.IsNull(m_emptyParser.GetValue(NoName, Argument0)); Assert.IsNull(m_emptyParser.GetValue(NoName, Argument1)); Assert.IsNull(m_emptyParser.GetValue(NoName, Argument2)); parser.Parse(); Assert.AreEqual(Value, parser.GetValue(Name, Argument1)); Assert.IsNull(parser.GetValue(EmptyName, Argument0)); Assert.IsNull(parser.GetValue(EmptyName, Argument1)); Assert.IsNull(parser.GetValue(EmptyName, Argument2)); Assert.IsNull(parser.GetValue(Name, Argument0)); Assert.IsNull(parser.GetValue(Name, Argument2)); Assert.IsNull(parser.GetValue(NoName, Argument0)); Assert.IsNull(parser.GetValue(NoName, Argument1)); Assert.IsNull(parser.GetValue(NoName, Argument2)); }
public void GetValue() { const string ArgumentName1 = "arg1"; const string ArgumentName2 = "arg2"; const string Name1 = "name1"; const string Name2 = "name2"; const string Value1 = "value1"; const string Value2 = "value2"; string[] arguments = new string[] { Switch.GetPrefixedName(Name1), Value1, Switch.GetPrefixedName(Name2), Value2 }; SwitchCollection switches = new SwitchCollection(); switches.Add(Name1, NoLongName, NoDescription, IsRequired, ArgumentName1); switches.Add(Name2, NoLongName, NoDescription, IsRequired, ArgumentName2); ArgumentParser parser = new ArgumentParser(arguments, switches); Assert.IsNull(parser.GetValue(EmptyName)); Assert.IsNull(parser.GetValue(Name1)); Assert.IsNull(parser.GetValue(Name2)); Assert.IsNull(parser.GetValue(NoName)); Assert.IsNull(m_emptyParser.GetValue(EmptyName)); Assert.IsNull(m_emptyParser.GetValue(NoName)); parser.Parse(); Assert.AreEqual(Value1, parser.GetValue(Name1)); Assert.AreEqual(Value2, parser.GetValue(Name2)); Assert.IsNull(parser.GetValue(EmptyName)); Assert.IsNull(parser.GetValue(NoName)); }
public void Initialize() { m_emptyParser = new ArgumentParser(new string[NoElements], new SwitchCollection()); m_emptyParser.Parse(); }