/// <summary> /// Called when a switch that takes parameters is detected on the command line. This method flags errors and stores the /// switch parameters. /// </summary> /// <param name="commandLineSwitches"></param> /// <param name="parameterizedSwitch"></param> /// <param name="switchParameters"></param> /// <param name="duplicateSwitchErrorMessage"></param> /// <param name="multipleParametersAllowed"></param> /// <param name="missingParametersErrorMessage"></param> /// <param name="unquoteParameters"></param> /// <param name="unquotedCommandLineArg"></param> private static void GatherParameterizedCommandLineSwitch ( CommandLineSwitches commandLineSwitches, CommandLineSwitches.ParameterizedSwitch parameterizedSwitch, string switchParameters, string duplicateSwitchErrorMessage, bool multipleParametersAllowed, string missingParametersErrorMessage, bool unquoteParameters, string unquotedCommandLineArg ) { if (// switch must have parameters (switchParameters.Length > 1) || // unless the parameters are optional (missingParametersErrorMessage == null)) { // check if switch is duplicated, and if that's allowed if (!commandLineSwitches.IsParameterizedSwitchSet(parameterizedSwitch) || (duplicateSwitchErrorMessage == null)) { // skip the parameter indicator (if any) if (switchParameters.Length > 0) { switchParameters = switchParameters.Substring(1); } // save the parameters after unquoting and splitting them if necessary if (!commandLineSwitches.SetParameterizedSwitch(parameterizedSwitch, unquotedCommandLineArg, switchParameters, multipleParametersAllowed, unquoteParameters)) { // if parsing revealed there were no real parameters, flag an error, unless the parameters are optional if (missingParametersErrorMessage != null) { commandLineSwitches.SetSwitchError(missingParametersErrorMessage, unquotedCommandLineArg); } } } else { commandLineSwitches.SetSwitchError(duplicateSwitchErrorMessage, unquotedCommandLineArg); } } else { commandLineSwitches.SetSwitchError(missingParametersErrorMessage, unquotedCommandLineArg); } }
public void SetParameterizedSwitchTests3() { CommandLineSwitches switches = new CommandLineSwitches(); // we haven't set this switch yet Assert.Null(switches.GetParameterizedSwitchCommandLineArg(CommandLineSwitches.ParameterizedSwitch.Logger)); Assert.False(switches.IsParameterizedSwitchSet(CommandLineSwitches.ParameterizedSwitch.Logger)); string[] parameters = switches[CommandLineSwitches.ParameterizedSwitch.Logger]; Assert.NotNull(parameters); Assert.Equal(0, parameters.Length); // don't unquote fake/missing parameters Assert.True(switches.SetParameterizedSwitch(CommandLineSwitches.ParameterizedSwitch.Logger, "/l:\"", "\"", false, false)); Assert.Equal("/l:\"", switches.GetParameterizedSwitchCommandLineArg(CommandLineSwitches.ParameterizedSwitch.Logger)); Assert.True(switches.IsParameterizedSwitchSet(CommandLineSwitches.ParameterizedSwitch.Logger)); parameters = switches[CommandLineSwitches.ParameterizedSwitch.Logger]; Assert.NotNull(parameters); Assert.Equal(1, parameters.Length); Assert.Equal("\"", parameters[0]); // don't unquote multiple fake/missing parameters -- this is bogus because the /logger switch does not take multiple // parameters, but for testing purposes this is fine Assert.True(switches.SetParameterizedSwitch(CommandLineSwitches.ParameterizedSwitch.Logger, "/LOGGER:\"\",asm;\"p,a;r\"", "\"\",asm;\"p,a;r\"", true, false)); Assert.Equal("/l:\" /LOGGER:\"\",asm;\"p,a;r\"", switches.GetParameterizedSwitchCommandLineArg(CommandLineSwitches.ParameterizedSwitch.Logger)); Assert.True(switches.IsParameterizedSwitchSet(CommandLineSwitches.ParameterizedSwitch.Logger)); parameters = switches[CommandLineSwitches.ParameterizedSwitch.Logger]; Assert.NotNull(parameters); Assert.Equal(4, parameters.Length); Assert.Equal("\"", parameters[0]); Assert.Equal("\"\"", parameters[1]); Assert.Equal("asm", parameters[2]); Assert.Equal("\"p,a;r\"", parameters[3]); }
public void AppendParameterizedSwitchesTests1() { CommandLineSwitches switchesLeft = new CommandLineSwitches(); switchesLeft.SetParameterizedSwitch(CommandLineSwitches.ParameterizedSwitch.Project, "tempproject.proj", "tempproject.proj", false, true); Assert.True(switchesLeft.IsParameterizedSwitchSet(CommandLineSwitches.ParameterizedSwitch.Project)); Assert.False(switchesLeft.IsParameterizedSwitchSet(CommandLineSwitches.ParameterizedSwitch.Target)); CommandLineSwitches switchesRight = new CommandLineSwitches(); switchesRight.SetParameterizedSwitch(CommandLineSwitches.ParameterizedSwitch.Target, "/t:build", "build", true, true); Assert.False(switchesRight.IsParameterizedSwitchSet(CommandLineSwitches.ParameterizedSwitch.Project)); Assert.True(switchesRight.IsParameterizedSwitchSet(CommandLineSwitches.ParameterizedSwitch.Target)); switchesLeft.Append(switchesRight); Assert.Equal("tempproject.proj", switchesLeft.GetParameterizedSwitchCommandLineArg(CommandLineSwitches.ParameterizedSwitch.Project)); Assert.True(switchesLeft.IsParameterizedSwitchSet(CommandLineSwitches.ParameterizedSwitch.Project)); string[] parameters = switchesLeft[CommandLineSwitches.ParameterizedSwitch.Project]; Assert.NotNull(parameters); Assert.Equal(1, parameters.Length); Assert.Equal("tempproject.proj", parameters[0]); Assert.Equal("/t:build", switchesLeft.GetParameterizedSwitchCommandLineArg(CommandLineSwitches.ParameterizedSwitch.Target)); Assert.True(switchesLeft.IsParameterizedSwitchSet(CommandLineSwitches.ParameterizedSwitch.Target)); parameters = switchesLeft[CommandLineSwitches.ParameterizedSwitch.Target]; Assert.NotNull(parameters); Assert.Equal(1, parameters.Length); Assert.Equal("build", parameters[0]); }
public void SetParameterizedSwitchTests2() { CommandLineSwitches switches = new CommandLineSwitches(); // we haven't set this switch yet Assert.Null(switches.GetParameterizedSwitchCommandLineArg(CommandLineSwitches.ParameterizedSwitch.Target)); Assert.False(switches.IsParameterizedSwitchSet(CommandLineSwitches.ParameterizedSwitch.Target)); string[] parameters = switches[CommandLineSwitches.ParameterizedSwitch.Target]; Assert.NotNull(parameters); Assert.Equal(0, parameters.Length); // fake/missing parameters -- this is bogus because the /target switch allows multiple parameters but we're turning // that off here just for testing purposes Assert.False(switches.SetParameterizedSwitch(CommandLineSwitches.ParameterizedSwitch.Target, "/t:\"", "\"", false, true)); // switch has been set Assert.Equal("/t:\"", switches.GetParameterizedSwitchCommandLineArg(CommandLineSwitches.ParameterizedSwitch.Target)); Assert.True(switches.IsParameterizedSwitchSet(CommandLineSwitches.ParameterizedSwitch.Target)); parameters = switches[CommandLineSwitches.ParameterizedSwitch.Target]; // but no parameters Assert.NotNull(parameters); Assert.Equal(0, parameters.Length); // more fake/missing parameters Assert.False(switches.SetParameterizedSwitch(CommandLineSwitches.ParameterizedSwitch.Target, "/t:A,\"\";B", "A,\"\";B", true, true)); Assert.Equal("/t:\" /t:A,\"\";B", switches.GetParameterizedSwitchCommandLineArg(CommandLineSwitches.ParameterizedSwitch.Target)); Assert.True(switches.IsParameterizedSwitchSet(CommandLineSwitches.ParameterizedSwitch.Target)); parameters = switches[CommandLineSwitches.ParameterizedSwitch.Target]; // now we have some parameters Assert.NotNull(parameters); Assert.Equal(2, parameters.Length); Assert.Equal("A", parameters[0]); Assert.Equal("B", parameters[1]); }
public void SetParameterizedSwitchTests1() { CommandLineSwitches switches = new CommandLineSwitches(); Assert.True(switches.SetParameterizedSwitch(CommandLineSwitches.ParameterizedSwitch.Verbosity, "/v:q", "q", false, true)); Assert.Equal("/v:q", switches.GetParameterizedSwitchCommandLineArg(CommandLineSwitches.ParameterizedSwitch.Verbosity)); Assert.True(switches.IsParameterizedSwitchSet(CommandLineSwitches.ParameterizedSwitch.Verbosity)); string[] parameters = switches[CommandLineSwitches.ParameterizedSwitch.Verbosity]; Assert.NotNull(parameters); Assert.Equal(1, parameters.Length); Assert.Equal("q", parameters[0]); // set it again -- this is bogus, because the /verbosity switch doesn't allow multiple parameters, but for the // purposes of testing the SetParameterizedSwitch() method, it doesn't matter Assert.True(switches.SetParameterizedSwitch(CommandLineSwitches.ParameterizedSwitch.Verbosity, "/verbosity:\"diag\";minimal", "\"diag\";minimal", true, true)); Assert.Equal("/v:q /verbosity:\"diag\";minimal", switches.GetParameterizedSwitchCommandLineArg(CommandLineSwitches.ParameterizedSwitch.Verbosity)); Assert.True(switches.IsParameterizedSwitchSet(CommandLineSwitches.ParameterizedSwitch.Verbosity)); parameters = switches[CommandLineSwitches.ParameterizedSwitch.Verbosity]; Assert.NotNull(parameters); Assert.Equal(3, parameters.Length); Assert.Equal("q", parameters[0]); Assert.Equal("diag", parameters[1]); Assert.Equal("minimal", parameters[2]); }
public void TestHaveAnySwitchesBeenSet() { // Check if method works with parameterized switch CommandLineSwitches switches = new CommandLineSwitches(); Assert.False(switches.HaveAnySwitchesBeenSet()); switches.SetParameterizedSwitch(CommandLineSwitches.ParameterizedSwitch.Verbosity, "/v:q", "q", false, true); Assert.True(switches.HaveAnySwitchesBeenSet()); // Check if method works with parameterless switches switches = new CommandLineSwitches(); Assert.False(switches.HaveAnySwitchesBeenSet()); switches.SetParameterlessSwitch(CommandLineSwitches.ParameterlessSwitch.Help, "/?"); Assert.True(switches.HaveAnySwitchesBeenSet()); }
public void AppendParameterizedSwitchesTests3() { CommandLineSwitches switchesLeft = new CommandLineSwitches(); switchesLeft.SetParameterizedSwitch(CommandLineSwitches.ParameterizedSwitch.Project, "tempproject.proj", "tempproject.proj", false, true); Assert.True(switchesLeft.IsParameterizedSwitchSet(CommandLineSwitches.ParameterizedSwitch.Project)); CommandLineSwitches switchesRight = new CommandLineSwitches(); switchesRight.SetParameterizedSwitch(CommandLineSwitches.ParameterizedSwitch.Project, "Rhubarb.proj", "Rhubarb.proj", false, true); Assert.True(switchesRight.IsParameterizedSwitchSet(CommandLineSwitches.ParameterizedSwitch.Project)); switchesLeft.Append(switchesRight); Assert.Equal("tempproject.proj", switchesLeft.GetParameterizedSwitchCommandLineArg(CommandLineSwitches.ParameterizedSwitch.Project)); Assert.True(switchesLeft.IsParameterizedSwitchSet(CommandLineSwitches.ParameterizedSwitch.Project)); string[] parameters = switchesLeft[CommandLineSwitches.ParameterizedSwitch.Project]; Assert.NotNull(parameters); Assert.Equal(1, parameters.Length); Assert.Equal("tempproject.proj", parameters[0]); Assert.True(switchesLeft.HaveErrors()); VerifySwitchError(switchesLeft, "Rhubarb.proj"); }
public void AppendParameterizedSwitchesTests2() { CommandLineSwitches switchesLeft = new CommandLineSwitches(); switchesLeft.SetParameterizedSwitch(CommandLineSwitches.ParameterizedSwitch.Target, "/target:Clean", "Clean", true, true); Assert.True(switchesLeft.IsParameterizedSwitchSet(CommandLineSwitches.ParameterizedSwitch.Target)); CommandLineSwitches switchesRight = new CommandLineSwitches(); switchesRight.SetParameterizedSwitch(CommandLineSwitches.ParameterizedSwitch.Target, "/t:\"RESOURCES\";build", "\"RESOURCES\";build", true, true); Assert.True(switchesRight.IsParameterizedSwitchSet(CommandLineSwitches.ParameterizedSwitch.Target)); switchesLeft.Append(switchesRight); Assert.Equal("/t:\"RESOURCES\";build", switchesLeft.GetParameterizedSwitchCommandLineArg(CommandLineSwitches.ParameterizedSwitch.Target)); Assert.True(switchesLeft.IsParameterizedSwitchSet(CommandLineSwitches.ParameterizedSwitch.Target)); string[] parameters = switchesLeft[CommandLineSwitches.ParameterizedSwitch.Target]; Assert.NotNull(parameters); Assert.Equal(3, parameters.Length); Assert.Equal("Clean", parameters[0]); Assert.Equal("RESOURCES", parameters[1]); Assert.Equal("build", parameters[2]); }