private bool compare(Option s, string arg) { if (!s.needsValue) { foreach (string optname in s.Names) { if (optname.Equals(arg)) { s.Name = optname; //set name in case we match an alias name return (true); } } return false; } else { foreach (string optname in s.Names) { if (arg.StartsWith(optname)) { checkDuplicateAndSetName(s, optname); return (true); } } return false; } }
public static bool UpdateOption(ref List<Option> lstOptions, bool Load, string optionFileName) { string opFile = Path.Combine(AppDomain.CurrentDomain.BaseDirectory,optionFileName); if (Load) { lstOptions = new List<Option>(); StreamReader sr = null; try { sr = new StreamReader(opFile); string rtext = ""; while ((rtext = sr.ReadLine()) != null) { rtext = rtext.Trim(); string optionName = ""; string optionValue = ""; if (rtext.Length > 0) { int pos = rtext.IndexOf("="); if (pos != -1) { optionName = rtext.Substring(0, pos); optionValue = rtext.Substring(pos + 1, rtext.Length - pos - 1); } } Option op = new Option(); lstOptions.Add(op); op.OptionName = optionName; op.OptionValue = optionValue; } sr.Close(); sr.Dispose(); } catch { if (sr != null) sr.Dispose(); return false; } } else { StreamWriter sw = null; try { sw = new StreamWriter(opFile); foreach (var op in lstOptions) { string wtext = string.Format("{0}={1}", op.OptionName, op.OptionValue); sw.WriteLine(wtext); } sw.Close(); sw.Dispose(); } catch { if (sw != null) sw.Dispose(); return false; } } return true; }
private void checkDuplicateAndSetName(Option s, string optname) { if (s.isMatched && s.needsValue) throw new DuplicateOptionException("Duplicate: The Option:'" + optname + "' allready exists on the comand line as +'" + s.Name + "'"); else { s.Name = optname; //set name in case we match an alias name } }
protected int processMatchedSwitch(Option s, string[] cmdlineArgs, int pos) { //if help switch is matched give help .. only works for console apps if (s.Equals(_help)) { if (isConsoleApplication) { Console.Write(this.HelpMessage()); } } //process bool switch if (s.Type == typeof(bool) && s.needsValue == false) { s.Value = true; return pos; } if (s.needsValue == true) { //retrieve parameter value and adjust pos string parameter = ""; pos = retrieveParameter(ref parameter, s.Name, cmdlineArgs, pos); //parse option using 'IParsableOptionParameter.parseValue(parameter)' //and set parameter value try { if (s.Type != null) { ((IParsableOptionParameter)s).Value = ((IParsableOptionParameter)s).parseValue(parameter); return pos; } } catch (Exception ex) { throw new ParameterConversionException(ex.Message); } } //unsupported type .. throw new CMDLineParserException("Unsupported Parameter Type:" + s.Type); }
/// <summary> /// Add a string parameter command line option. /// </summary> public Option AddStringParameter(string name, string description,bool required) { Option opt = new Option(name, description, typeof(string), true, required); AddOption(opt); return (opt); }
/// <summary> /// Add (a custom) Option (Optional) /// </summary> /// <remarks> /// To add instances (or subclasses) of 'CMDLineParser.Option' /// that implement: /// <code>'public override object parseValue(string parameter)'</code> /// </remarks> /// <param name="opt">subclass from 'CMDLineParser.Option'</param> /// <seealso cref="AddBoolSwitch"/> /// <seealso cref="AddStringParameter"/> public void AddOption(Option opt) { CheckCmdLineOption(opt.Name); if (SwitchesStore == null) SwitchesStore = new System.Collections.ArrayList(); SwitchesStore.Add(opt); }
/// <summary> /// Add a basic command line switch. /// (exist = 'true' otherwise 'false'). /// </summary> public Option AddBoolSwitch(string name, string description) { Option opt = new Option(name, description, typeof(bool), false, false); AddOption(opt); return (opt); }