/// <summary> /// Parses the auto-response file (assumes the "/noautoresponse" switch is not specified on the command line), and combines the /// switches from the auto-response file with the switches passed in. /// Returns true if the response file was found. /// </summary> private static bool GatherAutoResponseFileSwitches(string path, CommandLineSwitches switchesFromAutoResponseFile) { string autoResponseFile = Path.Combine(path, autoResponseFileName); bool found = false; // if the auto-response file does not exist, only use the switches on the command line if (File.Exists(autoResponseFile)) { found = true; GatherResponseFileSwitch("@" + autoResponseFile, switchesFromAutoResponseFile); // if the "/noautoresponse" switch was set in the auto-response file, flag an error if (switchesFromAutoResponseFile[CommandLineSwitches.ParameterlessSwitch.NoAutoResponse]) { switchesFromAutoResponseFile.SetSwitchError("CannotAutoDisableAutoResponseFile", switchesFromAutoResponseFile.GetParameterlessSwitchCommandLineArg(CommandLineSwitches.ParameterlessSwitch.NoAutoResponse)); } if (switchesFromAutoResponseFile.HaveAnySwitchesBeenSet()) { // we picked up some switches from the auto-response file usingSwitchesFromAutoResponseFile = true; } } return found; }
/// <summary> /// Called when a switch that doesn't take parameters is detected on the command line. /// </summary> /// <param name="commandLineSwitches"></param> /// <param name="parameterlessSwitch"></param> /// <param name="switchParameters"></param> /// <param name="duplicateSwitchErrorMessage"></param> /// <param name="unquotedCommandLineArg"></param> private static void GatherParameterlessCommandLineSwitch ( CommandLineSwitches commandLineSwitches, CommandLineSwitches.ParameterlessSwitch parameterlessSwitch, string switchParameters, string duplicateSwitchErrorMessage, string unquotedCommandLineArg ) { // switch should not have any parameters if (switchParameters.Length == 0) { // check if switch is duplicated, and if that's allowed if (!commandLineSwitches.IsParameterlessSwitchSet(parameterlessSwitch) || (duplicateSwitchErrorMessage == null)) { commandLineSwitches.SetParameterlessSwitch(parameterlessSwitch, unquotedCommandLineArg); } else { commandLineSwitches.SetSwitchError(duplicateSwitchErrorMessage, unquotedCommandLineArg); } } else { commandLineSwitches.SetUnexpectedParametersError(unquotedCommandLineArg); } }
/// <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); } }
/// <summary> /// Called when a response file switch is detected on the command line. It loads the specified response file, and parses /// each line in it like a command line. It also prevents multiple (or recursive) inclusions of the same response file. /// </summary> /// <param name="unquotedCommandLineArg"></param> /// <param name="commandLineSwitches"></param> private static void GatherResponseFileSwitch(string unquotedCommandLineArg, CommandLineSwitches commandLineSwitches) { try { string responseFile = unquotedCommandLineArg.Substring(1); if (responseFile.Length == 0) { commandLineSwitches.SetSwitchError("MissingResponseFileError", unquotedCommandLineArg); } else if (!File.Exists(responseFile)) { commandLineSwitches.SetParameterError("ResponseFileNotFoundError", unquotedCommandLineArg); } else { // normalize the response file path to help catch multiple (or recursive) inclusions responseFile = Path.GetFullPath(responseFile); // NOTE: for network paths or mapped paths, normalization is not guaranteed to work bool isRepeatedResponseFile = false; foreach (string includedResponseFile in s_includedResponseFiles) { if (String.Compare(responseFile, includedResponseFile, StringComparison.OrdinalIgnoreCase) == 0) { commandLineSwitches.SetParameterError("RepeatedResponseFileError", unquotedCommandLineArg); isRepeatedResponseFile = true; break; } } if (!isRepeatedResponseFile) { s_includedResponseFiles.Add(responseFile); ArrayList argsFromResponseFile; using (StreamReader responseFileContents = new StreamReader(responseFile, Encoding.Default)) // HIGHCHAR: If response files have no byte-order marks, then assume ANSI rather than ASCII. { argsFromResponseFile = new ArrayList(); while (responseFileContents.Peek() != -1) { // ignore leading whitespace on each line string responseFileLine = responseFileContents.ReadLine().TrimStart(); // skip comment lines beginning with # if (!responseFileLine.StartsWith("#", StringComparison.Ordinal)) { // treat each line of the response file like a command line i.e. args separated by whitespace argsFromResponseFile.AddRange(QuotingUtilities.SplitUnquoted(Environment.ExpandEnvironmentVariables(responseFileLine))); } } } GatherCommandLineSwitches(argsFromResponseFile, commandLineSwitches); } } } catch (NotSupportedException e) { commandLineSwitches.SetParameterError("ReadResponseFileError", unquotedCommandLineArg, e); } catch (SecurityException e) { commandLineSwitches.SetParameterError("ReadResponseFileError", unquotedCommandLineArg, e); } catch (UnauthorizedAccessException e) { commandLineSwitches.SetParameterError("ReadResponseFileError", unquotedCommandLineArg, e); } catch (IOException e) { commandLineSwitches.SetParameterError("ReadResponseFileError", unquotedCommandLineArg, e); } }