/// <summary> /// Initializes a new instance of the <see cref="GetOpt" /> class /// </summary> /// <param name="applicationDescription"> /// Description of this application, to be used in ShowUsage /// </param> /// <param name="options"> /// The list of options that should be parsed for /// </param> /// <param name="addHelp"> /// If true (default) a help option will be added automatically /// </param> public GetOpt(string applicationDescription, IEnumerable <CommandLineOption> options, bool addHelp = true) { this.applicationDescription = applicationDescription; ParsedOptions = -1; this.options = options.ToList(); AdditionalParameters = new List <string>(); foreach (var option in this.options) { if (option.ShortName != '\0') { shortNameLookup.Add(option.ShortName, option); } if (option.LongName != null) { longNameLookup.Add(option.LongName, option); } if (option.ShortName == '\0' && option.LongName == null) { unnamedList.Add(option); } } if (addHelp) { var helpOption = new CommandLineOption('h', "help", "This help", ParameterType.None, o => ShowUsage()); shortNameLookup.Add(helpOption.ShortName, helpOption); longNameLookup.Add(helpOption.LongName, helpOption); this.options.Add(helpOption); } }
/// <summary> /// Initializes a new instance of the <see cref="GetOpt"/> class /// </summary> /// <param name="applicationDescription"> /// Description of this application, to be used in ShowUsage /// </param> /// <param name="options"> /// The list of options that should be parsed for /// </param> /// <param name="addHelp"> /// If true (default) a help option will be added automatically /// </param> public GetOpt(string applicationDescription, IEnumerable<CommandLineOption> options, bool addHelp = true) { this.applicationDescription = applicationDescription; this.ParsedOptions = -1; this.options = options.ToList(); this.AdditionalParameters = new List<string>(); foreach (var option in this.options) { if (option.ShortName != '\0') { this.shortNameLookup.Add(option.ShortName, option); } if (option.LongName != null) { this.longNameLookup.Add(option.LongName, option); } if (option.ShortName == '\0' && option.LongName == null) { this.unnamedList.Add(option); } } if (addHelp) { var helpOption = new CommandLineOption('h', "help", "This help", ParameterType.None, o => this.ShowUsage()); this.shortNameLookup.Add(helpOption.ShortName, helpOption); this.longNameLookup.Add(helpOption.LongName, helpOption); this.options.Add(helpOption); } }
public void Constructor2Works() { // Arrange int t = 1; // Act var clo = new CommandLineOption("desc", ParameterType.Integer, value => t++, true); clo.SetFunction(null); // Assert Assert.AreEqual("desc", clo.Description); Assert.AreEqual(null, clo.LongName); Assert.AreEqual('\0', clo.ShortName); Assert.AreEqual(ParameterType.Integer, clo.ParameterType); Assert.AreEqual(2, t); }
public void Constructor1Works() { // Arrange int t = 1; // Act var clo = new CommandLineOption('a', "abc", "desc", ParameterType.None, value => t++); clo.SetFunction(null); // Assert Assert.AreEqual("desc", clo.Description); Assert.AreEqual("abc", clo.LongName); Assert.AreEqual('a', clo.ShortName); Assert.AreEqual(ParameterType.None, clo.ParameterType); Assert.AreEqual(2, t); }
/// <summary> /// Parses any parameters on the command line that is associated with this option, and calls the set function /// </summary> /// <param name="args"> /// Command line arguments, usually the parameter to the "main" function /// </param> /// <param name="i"> /// Index in the args array we should start at /// </param> /// <param name="option"> /// Which option we are trying to parse /// </param> /// <param name="inlineValue"> /// optional value for parameter if found /// </param> /// <returns> /// How many of the arguments in args we used up /// </returns> private static int ParseAndDispatch(string[] args, int i, CommandLineOption option, string inlineValue = null) { if (option.ParameterType != ParameterType.None && args.Length < i + 2 && inlineValue == null) { throw new CommandLineException( $"Option '{option.LongName ?? option.ShortName.ToString()}' requires a parameter"); } switch (option.ParameterType) { case ParameterType.Double: var ci = new CultureInfo("en-US"); if (!double.TryParse(inlineValue ?? args[++i], NumberStyles.Any, ci, out var tempChar)) { throw new CommandLineException( $"Option '{option.Name}': '{inlineValue ?? args[i]}' is not a valid numeric value"); } option.SetFunction?.Invoke(tempChar); return(i); case ParameterType.Integer: if (!int.TryParse(inlineValue ?? args[++i], out var tempInt)) { throw new CommandLineException( $"Option '{option.Name}': '{inlineValue ?? args[i]}' is not a valid integer"); } option.SetFunction?.Invoke(tempInt); return(i); case ParameterType.String: i++; option.SetFunction?.Invoke(inlineValue ?? args[i]); return(i); case ParameterType.None: option.SetFunction?.Invoke(null); return(i); } return(i); }
/// <summary> /// Parses any parameters on the command line that is associated with this option, and calls the set function /// </summary> /// <param name="args"> /// Command line arguments, usually the parameter to the "main" function /// </param> /// <param name="i"> /// Index in the args array we should start at /// </param> /// <param name="option"> /// Which option we are trying to parse /// </param> /// <param name="inlineValue"> /// optional value for parameter if found /// </param> /// <returns> /// How many of the arguments in args we used up /// </returns> private static int ParseAndDispatch(string[] args, int i, CommandLineOption option, string inlineValue = null) { if (option.ParameterType != ParameterType.None && args.Length < i + 2 && inlineValue == null) { throw new CommandLineException(string.Format("Option '{0}' requires a parameter", option.LongName ?? option.ShortName.ToString())); } switch (option.ParameterType) { case ParameterType.Double: double tempChar; var ci = new CultureInfo("en-US"); if (!double.TryParse(inlineValue ?? args[++i], NumberStyles.Any, ci, out tempChar)) { throw new CommandLineException( string.Format("Option '{0}': '{1}' is not a valid numeric value", option.Name, inlineValue ?? args[i])); } if (option.SetFunction != null) { option.SetFunction(tempChar); } return i; case ParameterType.Integer: int tempInt; if (!int.TryParse(inlineValue ?? args[++i], out tempInt)) { throw new CommandLineException(string.Format("Option '{0}': '{1}' is not a valid integer", option.Name, inlineValue ?? args[i])); } if (option.SetFunction != null) { option.SetFunction(tempInt); } return i; case ParameterType.String: i++; if (option.SetFunction != null) { option.SetFunction(inlineValue ?? args[i]); } return i; case ParameterType.None: if (option.SetFunction != null) { option.SetFunction(null); } return i; } return i; }