private static bool ReadOptionsStop(GetOptContext getOptContext, IEnumerator <string> args) { if (getOptContext.OptionsStop) { return(false); } if (args.Current.Equals(LongPrefix)) { getOptContext.OptionsStop = true; return(true); } return(false); }
private static bool ReadShortOptions(GetOptContext getOptContext, IEnumerator <string> args) { if (getOptContext.OptionsStop) { return(false); } if (!args.Current.StartsWith(ShortPrefix)) { return(false); } var option = args.Current.Substring(ShortPrefix.Length); while (!string.IsNullOrEmpty(option)) { var optionName = option[0]; var optionValue = option.Substring(1); var o = FindShortOption(getOptContext.Options, optionName); if (o == null) { throw new Exception($"{ShortPrefix}{optionName} is not an option."); } if (o.IsFlag) { option = optionValue; o.SetFlag(); } else { option = string.Empty; if (string.IsNullOrEmpty(optionValue)) { args.MoveNext(); optionValue = args.Current; } o.Set(optionValue); } } return(true); }
private static bool ReadLongOption(GetOptContext getOptContext, IEnumerator <string> args) { if (getOptContext.OptionsStop) { return(false); } if (!args.Current.StartsWith(LongPrefix)) { return(false); } var parts = args.Current.Substring(LongPrefix.Length).Split(new[] { '=' }, 2); var optionName = parts[0]; var o = FindLongOption(getOptContext.Options, optionName); if (o == null) { throw new Exception($"{LongPrefix}{optionName} is not an option."); } if (o.IsFlag) { o.SetFlag(); } else { string optionValue; if (parts.Length >= 2) { optionValue = parts[1]; } else { args.MoveNext(); optionValue = args.Current; } o.Set(optionValue); } return(true); }
/// <summary> /// Parse args into options /// </summary> /// <param name="args"></param> /// <param name="options"></param> public static void Parse(IEnumerable <string> args, object options) { var context = new GetOptContext(options); using (var e = args.GetEnumerator()) { while (e.MoveNext()) { try { ReadOption(context, e); } catch (Exception ex) { throw new ParseException(args, e, ex); } } } }
private static void ReadOption(GetOptContext getOptContext, IEnumerator <string> args) { if (ReadOptionsStop(getOptContext, args)) { return; } if (ReadLongOption(getOptContext, args)) { return; } if (ReadShortOptions(getOptContext, args)) { return; } if (ReadArgument(getOptContext, args)) { return; } }
private static bool ReadArgument(GetOptContext getOptContext, IEnumerator <string> args) { FindOperands(getOptContext.Options).Set(args.Current); return(true); }