internal int Execute(string program, string[] route, string[] args, CmdLineSettings settings) { try { var arg = args.FirstOrDefault(); var prefixes = settings.OptionPrefixes.Select(c => c.ToString(CultureInfo.CurrentCulture)) .Concat(new[] {string.Empty}); var names = new[] {"?", "help"}; if ( prefixes.SelectMany(p => names.Select(n => p + n)) .Any(x => x.Equals(arg, StringComparison.CurrentCultureIgnoreCase))) { ShowHelp(program, route, settings); return 0; } return ExecuteCommand(program, route, args, settings); } catch (ParserException exception) { using (Writer.Color(ConsoleColor.Red)) Writer.WriteLine(exception.Message); return -10; } catch (Exception exception) { using (Writer.Color(ConsoleColor.Red)) Writer.WriteLine(exception.ToString()); return -1; } }
public int Execute(string[] args, CmdLineSettings settings = null) { var location = Assembly.GetEntryAssembly().Location; var program = Path.GetFileName(location); var route = new string[] { }; return Execute(program, route, args, settings ?? new CmdLineSettings()); }
protected abstract void ShowHelp(string program, string[] route, CmdLineSettings settings);
protected abstract int ExecuteCommand(string program, string[] route, string[] args, CmdLineSettings settings);
public CmdLineParser(CmdLineInputDefinition definition, CmdLineSettings settings) { _definition = definition; _settings = settings; }
private static IEnumerable<string> PreProcess(IEnumerable<string> strings, CmdLineSettings settings) { if (settings.OptionValueFormat == OptionValueFormat.Separated) return strings; var list = new List<string>(); foreach (var s in strings) { var index = s.IndexOf(settings.OptionNameValueSeparator); if (index == -1) { list.Add(s); } else { list.Add(s.Substring(0, index)); list.AddRange(s.Substring(index + 1).Split(settings.OptionValueSeparator)); } } return list; }