public bool AssignValue(object destination, MultiMap <Argument, object> argumentValues, CommandLineErrorReporter reporter) { IList <object> values = argumentValues[this]; if (IsCollection) { Array array = Array.CreateInstance(valueType, values.Count); for (int i = 0; i < values.Count; i++) { array.SetValue(values[i], i); } field.SetValue(destination, array); } else if (values.Count != 0) { field.SetValue(destination, values[0]); } else if (IsRequired) { if (IsDefault) { reporter(Resources.CommandLineArgumentParser_Argument_MissingRequiredDefaultArgument); } else { reporter(string.Format(Resources.CommandLineArgumentParser_Argument_MissingRequiredArgument, LongName)); } return(true); } return(false); }
public bool AddValue(string value, MultiMap <Argument, object> argumentValues, CommandLineErrorReporter reporter) { if (!AllowMultiple && argumentValues.ContainsKey(this)) { reporter(string.Format(Resources.CommandLineArgumentParser_Argument_DuplicateArgument, LongName)); return(false); } object newValue; if (!ParseValue(ValueType, value, out newValue)) { reporter(string.Format(Resources.CommandLineArgumentParser_Argument_InvalidArgumentValue, LongName, value)); return(false); } if (Unique && argumentValues.Contains(this, newValue)) { reporter(string.Format(Resources.CommandLineArgumentParser_Argument_DuplicateArgumentValueExpectedUnique, LongName, value)); return(false); } argumentValues.Add(this, newValue); return(true); }
/// <inheritdoc /> public override bool ValidateArguments(Arguments arguments, CommandLineErrorReporter errorReporter) { if (!arguments.Install && !arguments.Uninstall || arguments.Install && arguments.Uninstall) { errorReporter("Exactly one of the options /install or /uninstall must be specified."); return(false); } return(true); }
/// <summary> /// Parses an argument list. /// </summary> /// <param name="args">The arguments to parse.</param> /// <param name="destination">The destination of the parsed arguments.</param> /// <param name="reporter">The error reporter.</param> /// <returns>True if no parse errors were encountered.</returns> public bool Parse(string[] args, object destination, CommandLineErrorReporter reporter) { if (args == null || Array.IndexOf(args, null) >= 0) { throw new ArgumentNullException(@"args"); } if (destination == null) { throw new ArgumentNullException(@"destination"); } if (!argumentSpecification.IsInstanceOfType(destination)) { throw new ArgumentException(Resources.CommandLineArgumentParser_ArgumentObjectIsOfIncorrectType, @"destination"); } if (reporter == null) { throw new ArgumentNullException(@"reporter"); } var argumentValues = new MultiMap <Argument, object>(); try { bool hadError = ParseArgumentList(args, argumentValues, reporter); // Finished assigning values. foreach (Argument arg in arguments) { hadError |= arg.AssignValue(destination, argumentValues, reporter); } if (defaultArgument != null) { hadError |= defaultArgument.AssignValue(destination, argumentValues, reporter); } return(!hadError); } catch (Exception ex) { reporter(string.Format(Resources.CommandLineArgumentParser_ExceptionWhileParsing, ex.Message)); return(false); } }
private string GetResponseFileContents(string fileName, CommandLineErrorReporter reporter) { string args = null; try { args = responseFileReader(fileName); } catch (FileNotFoundException) { reporter(string.Format(Resources.CommandLineArgumentParser_ResponseFileDoesNotExist, fileName)); } catch (Exception e) { reporter(string.Format(Resources.CommandLineArgumentParser_ErrorOpeningResponseFile, fileName, e.Message)); } return(args); }
bool IUtilityCommand.ValidateArguments(object arguments, CommandLineErrorReporter errorReporter) { return(ValidateArguments((TArguments)arguments, errorReporter)); }
/// <summary> /// Validates the command arguments. /// </summary> /// <param name="arguments">The arguments object, not null.</param> /// <param name="errorReporter">The error reporter, not null.</param> /// <returns>True if the arguments are valid.</returns> public virtual bool ValidateArguments(TArguments arguments, CommandLineErrorReporter errorReporter) { return(true); }
private static void ReportUnrecognizedArgument(CommandLineErrorReporter reporter, string argument) { reporter(string.Format(Resources.CommandLineArgumentParser_UnrecognizedArgument, argument)); }
private string GetResponseFileContents(string fileName, CommandLineErrorReporter reporter) { string args = null; try { args = responseFileReader(fileName); } catch (FileNotFoundException) { reporter(string.Format(Resources.CommandLineArgumentParser_ResponseFileDoesNotExist, fileName)); } catch (Exception e) { reporter(string.Format(Resources.CommandLineArgumentParser_ErrorOpeningResponseFile, fileName, e.Message)); } return args; }
private bool LexFileArguments(string fileName, CommandLineErrorReporter reporter, out string[] nestedArguments) { nestedArguments = null; string args = GetResponseFileContents(fileName, reporter); if (args == null) { return(true); } bool hadError = false; List <string> argArray = new List <string>(); StringBuilder currentArg = new StringBuilder(); bool inQuotes = false; int index = 0; try { for (; ;) { // skip whitespace while (char.IsWhiteSpace(args[index])) { index += 1; } // # - comment to end of line if (args[index] == '#') { index += 1; while (args[index] != '\n') { index += 1; } continue; } // do one argument do { if (args[index] == '\\') { int cSlashes = 1; index += 1; while (index == args.Length && args[index] == '\\') { cSlashes += 1; } if (index == args.Length || args[index] != '"') { currentArg.Append('\\', cSlashes); } else { currentArg.Append('\\', (cSlashes >> 1)); if (0 != (cSlashes & 1)) { currentArg.Append('"'); } else { inQuotes = !inQuotes; } } } else if (args[index] == '"') { inQuotes = !inQuotes; index += 1; } else { currentArg.Append(args[index]); index += 1; } } while (!char.IsWhiteSpace(args[index]) || inQuotes); argArray.Add(currentArg.ToString()); currentArg.Length = 0; } } catch (IndexOutOfRangeException) { // got EOF if (inQuotes) { reporter(string.Format(Resources.CommandLineArgumentParser_MismatchedQuotedInResponseFile, fileName)); hadError = true; } else if (currentArg.Length > 0) { // valid argument can be terminated by EOF argArray.Add(currentArg.ToString()); } } nestedArguments = argArray.ToArray(); return(hadError); }
private bool ParseArgumentList(IEnumerable <string> args, MultiMap <Argument, object> argumentValues, CommandLineErrorReporter reporter) { bool hadError = false; foreach (string argument in args) { if (argument.Length == 0) { continue; } switch (argument[0]) { case '-': case '/': int endIndex = argument.IndexOfAny(new char[] { ':', '+' }, 1); string option = argument.Substring(1, endIndex == -1 ? argument.Length - 1 : endIndex - 1); string optionArgument; if (option.Length + 1 == argument.Length) { optionArgument = null; } else if (argument.Length > 1 + option.Length && argument[1 + option.Length] == ':') { optionArgument = argument.Substring(option.Length + 2); } else { optionArgument = argument.Substring(option.Length + 1); } Argument arg; if (argumentMap.TryGetValue(option, out arg)) { hadError |= !arg.AddValue(optionArgument, argumentValues, reporter); } else if (defaultArgumentConsumesUnrecognizedSwitches) { hadError |= !defaultArgument.AddValue(argument, argumentValues, reporter); } else { ReportUnrecognizedArgument(reporter, argument); hadError = true; } break; case '@': if (SupportsResponseFiles) { string[] nestedArguments; hadError |= LexFileArguments(argument.Substring(1), reporter, out nestedArguments); if (nestedArguments != null) { hadError |= ParseArgumentList(nestedArguments, argumentValues, reporter); } } else if (defaultArgument != null) { hadError |= !defaultArgument.AddValue(argument, argumentValues, reporter); } else { ReportUnrecognizedArgument(reporter, argument); hadError = true; } break; default: if (defaultArgument != null) { hadError |= !defaultArgument.AddValue(argument, argumentValues, reporter); } else { ReportUnrecognizedArgument(reporter, argument); hadError = true; } break; } } return(hadError); }
private static void ReportUnrecognizedArgument(CommandLineErrorReporter reporter, string argument) { reporter(string.Format(Resources.CommandLineArgumentParser_UnrecognizedArgument, argument)); }
/// <summary> /// Parses an argument list. /// </summary> /// <param name="args">The arguments to parse.</param> /// <param name="destination">The destination of the parsed arguments.</param> /// <param name="reporter">The error reporter.</param> /// <returns>True if no parse errors were encountered.</returns> public bool Parse(string[] args, object destination, CommandLineErrorReporter reporter) { if (args == null || Array.IndexOf(args, null) >= 0) throw new ArgumentNullException(@"args"); if (destination == null) throw new ArgumentNullException(@"destination"); if (!argumentSpecification.IsInstanceOfType(destination)) throw new ArgumentException(Resources.CommandLineArgumentParser_ArgumentObjectIsOfIncorrectType, @"destination"); if (reporter == null) throw new ArgumentNullException(@"reporter"); var argumentValues = new MultiMap<Argument, object>(); try { bool hadError = ParseArgumentList(args, argumentValues, reporter); // Finished assigning values. foreach (Argument arg in arguments) hadError |= arg.AssignValue(destination, argumentValues, reporter); if (defaultArgument != null) hadError |= defaultArgument.AssignValue(destination, argumentValues, reporter); return !hadError; } catch (Exception ex) { reporter(string.Format(Resources.CommandLineArgumentParser_ExceptionWhileParsing, ex.Message)); return false; } }
public bool AssignValue(object destination, MultiMap<Argument, object> argumentValues, CommandLineErrorReporter reporter) { IList<object> values = argumentValues[this]; if (IsCollection) { Array array = Array.CreateInstance(valueType, values.Count); for (int i = 0; i < values.Count; i++) array.SetValue(values[i], i); field.SetValue(destination, array); } else if (values.Count != 0) { field.SetValue(destination, values[0]); } else if (IsRequired) { if (IsDefault) reporter(Resources.CommandLineArgumentParser_Argument_MissingRequiredDefaultArgument); else reporter(string.Format(Resources.CommandLineArgumentParser_Argument_MissingRequiredArgument, LongName)); return true; } return false; }
public bool AddValue(string value, MultiMap<Argument, object> argumentValues, CommandLineErrorReporter reporter) { if (!AllowMultiple && argumentValues.ContainsKey(this)) { reporter(string.Format(Resources.CommandLineArgumentParser_Argument_DuplicateArgument, LongName)); return false; } object newValue; if (!ParseValue(ValueType, value, out newValue)) { reporter(string.Format(Resources.CommandLineArgumentParser_Argument_InvalidArgumentValue, LongName, value)); return false; } if (Unique && argumentValues.Contains(this, newValue)) { reporter(string.Format(Resources.CommandLineArgumentParser_Argument_DuplicateArgumentValueExpectedUnique, LongName, value)); return false; } argumentValues.Add(this, newValue); return true; }
private bool ParseArgumentList(IEnumerable<string> args, MultiMap<Argument, object> argumentValues, CommandLineErrorReporter reporter) { bool hadError = false; foreach (string argument in args) { if (argument.Length == 0) continue; switch (argument[0]) { case '-': case '/': int endIndex = argument.IndexOfAny(new char[] { ':', '+' }, 1); string option = argument.Substring(1, endIndex == -1 ? argument.Length - 1 : endIndex - 1); string optionArgument; if (option.Length + 1 == argument.Length) optionArgument = null; else if (argument.Length > 1 + option.Length && argument[1 + option.Length] == ':') optionArgument = argument.Substring(option.Length + 2); else optionArgument = argument.Substring(option.Length + 1); Argument arg; if (argumentMap.TryGetValue(option, out arg)) { hadError |= !arg.AddValue(optionArgument, argumentValues, reporter); } else if (defaultArgumentConsumesUnrecognizedSwitches) { hadError |= !defaultArgument.AddValue(argument, argumentValues, reporter); } else { ReportUnrecognizedArgument(reporter, argument); hadError = true; } break; case '@': if (SupportsResponseFiles) { string[] nestedArguments; hadError |= LexFileArguments(argument.Substring(1), reporter, out nestedArguments); if (nestedArguments != null) hadError |= ParseArgumentList(nestedArguments, argumentValues, reporter); } else if (defaultArgument != null) { hadError |= !defaultArgument.AddValue(argument, argumentValues, reporter); } else { ReportUnrecognizedArgument(reporter, argument); hadError = true; } break; default: if (defaultArgument != null) { hadError |= !defaultArgument.AddValue(argument, argumentValues, reporter); } else { ReportUnrecognizedArgument(reporter, argument); hadError = true; } break; } } return hadError; }
private bool LexFileArguments(string fileName, CommandLineErrorReporter reporter, out string[] nestedArguments) { nestedArguments = null; string args = GetResponseFileContents(fileName, reporter); if (args == null) return true; bool hadError = false; List<string> argArray = new List<string>(); StringBuilder currentArg = new StringBuilder(); bool inQuotes = false; int index = 0; try { for (; ; ) { // skip whitespace while (char.IsWhiteSpace(args[index])) { index += 1; } // # - comment to end of line if (args[index] == '#') { index += 1; while (args[index] != '\n') { index += 1; } continue; } // do one argument do { if (args[index] == '\\') { int cSlashes = 1; index += 1; while (index == args.Length && args[index] == '\\') { cSlashes += 1; } if (index == args.Length || args[index] != '"') { currentArg.Append('\\', cSlashes); } else { currentArg.Append('\\', (cSlashes >> 1)); if (0 != (cSlashes & 1)) { currentArg.Append('"'); } else { inQuotes = !inQuotes; } } } else if (args[index] == '"') { inQuotes = !inQuotes; index += 1; } else { currentArg.Append(args[index]); index += 1; } } while (!char.IsWhiteSpace(args[index]) || inQuotes); argArray.Add(currentArg.ToString()); currentArg.Length = 0; } } catch (IndexOutOfRangeException) { // got EOF if (inQuotes) { reporter(string.Format(Resources.CommandLineArgumentParser_MismatchedQuotedInResponseFile, fileName)); hadError = true; } else if (currentArg.Length > 0) { // valid argument can be terminated by EOF argArray.Add(currentArg.ToString()); } } nestedArguments = argArray.ToArray(); return hadError; }