internal static void Check() { if (File.Exists(Program.DbPath)) { return; } CustomConsole.WriteWarning("Database not found."); Updater.Update(); }
internal static int Print(string pageName, string prefLanguage = null, string platform = null, bool markdown = false) { pageName = pageName.ToLower().TrimStart().Replace(' ', '-'); List <string> languages; if (prefLanguage == null) { languages = Locale.GetPreferredLanguages(); if (languages.Count == 0) { Console.WriteLine( $"[INFO] None of the preferred languages found, using {Locale.GetLanguageName(Locale.DefaultLanguage)} instead. " + "See `tldr --list-languages` for a list of all available languages."); languages.Add(Locale.DefaultLanguage); } } else { languages = new List <string> { prefLanguage }; } platform ??= GetPlatform(); string altPlatform = null; List <Page> results = QueryByName(pageName); if (results.Count == 0) { if ((DateTime.UtcNow.Date - Cache.LastUpdate()).TotalDays > 5) { Console.WriteLine("Page not found."); Console.Write("Cache older than 5 days. "); Updater.Update(); results = QueryByName(pageName); } if (results.Count == 0) { return(NotFound(pageName)); } } results = results.OrderBy(item => item, new PageComparer(new[] { platform, "common" }, languages)).ToList(); Page page; try { page = Find(results, languages, platform); } catch (ArgumentNullException) { if (prefLanguage != null) { Console.WriteLine( $"The `{pageName}` page could not be found in {Locale.GetLanguageName(prefLanguage)}. " + $"{Environment.NewLine}Feel free to translate it: https://github.com/tldr-pages/tldr/blob/master/CONTRIBUTING.md#translations"); return(2); } try { page = FindAlternative(results, languages); if (platform != page.Platform && page.Platform != "common") { altPlatform = page.Platform; } } catch (ArgumentNullException) { return(NotFound(pageName)); } } if (!page.Local) { CustomSpinner.Run("Page not cached. Downloading", page.Download); } string path = page.GetPath(); if (!File.Exists(path)) { Console.WriteLine("[ERROR] File \"{0}\" not found.", path); return(1); } if (markdown) { Console.WriteLine(File.ReadAllText(path)); } else { return(Render(path, altPlatform)); } return(0); }
public static int Main(string[] args) { if (Environment.OSVersion.Platform == PlatformID.Win32NT) { IntPtr iStdOut = GetStdHandle(STD_OUTPUT_HANDLE); if (!GetConsoleMode(iStdOut, out uint outConsoleMode)) { AnsiSupport = false; } else { outConsoleMode |= ENABLE_VIRTUAL_TERMINAL_PROCESSING | DISABLE_NEWLINE_AUTO_RETURN; if (!SetConsoleMode(iStdOut, outConsoleMode)) { AnsiSupport = false; } } } bool showHelp = false; bool list = false; bool ignorePlatform = false; string language = null; string platform = null; string search = null; bool markdown = false; string render = null; OptionSet options = new OptionSet { "Usage: tldr command [options]" + Environment.NewLine, "Simplified and community-driven man pages" + Environment.NewLine, { "a|list-all", "List all pages", a => list = ignorePlatform = a != null }, { "c|clear-cache", "Clear the cache", c => { Console.WriteLine("Clearing cache..."); Cache.Clear(); Console.WriteLine("Cache cleared."); Environment.Exit(0); } }, { "f=|render=", "Render a specific markdown file", v => render = v }, { "h|help", "Display this help text", h => showHelp = h != null }, { "l|list", "List all pages for the current platform and language", l => list = l != null }, { "list-os", "List all platforms", o => { Cache.Check(); Console.WriteLine(string.Join(Environment.NewLine, Index.ListPlatform())); } }, { "list-languages", "List all languages", la => { Cache.Check(); Console.WriteLine(string.Join(Environment.NewLine, ListLanguages().Select(x => { try { return(x + ": " + CultureInfo.GetCultureInfo(x.Replace('_', '-')).EnglishName); } catch (CultureNotFoundException) { return(null); } }).Where(x => x != null))); } }, { "L=|language=|lang=", "Specifies the preferred language", la => language = la }, { "m|markdown", "Show the markdown source of a page", v => markdown = v != null }, { "p=|platform=", "Override the default platform", o => platform = o }, { "s=|search=", "Search for a string", s => search = s }, { "u|update", "Update the cache", u => Updater.Update() }, { "self-update", "Check for tldr-sharp updates", u => { SelfUpdater.CheckSelfUpdate(); Environment.Exit(0); } }, { "v|version", "Show version information", v => { Console.WriteLine("tldr-sharp " + Assembly.GetExecutingAssembly().GetName().Version.Major + "." + Assembly.GetExecutingAssembly().GetName().Version.Minor + "." + Assembly.GetExecutingAssembly().GetName().Version.Build); Console.WriteLine("tldr-pages client specification " + ClientSpecVersion); Environment.Exit(0); } } }; List <string> extra; try { extra = options.Parse(args); } catch (OptionException e) { Console.WriteLine(e.Message); return(1); } if (showHelp || args.Length == 0) { options.WriteOptionDescriptions(Console.Out); return(args.Length == 0 ? 1 : 0); } if (render != null) { return(Page.Render(render)); } // All following functions rely on the cache, so check it. Cache.Check(); if (language != null) { if (!CheckLanguage(language)) { Console.WriteLine("[ERROR] unknown language '{0}'", language); return(1); } } if (list) { ListAll(ignorePlatform, language, platform); return(0); } if (search != null) { return(Page.Search(search, language, platform)); } string page = string.Empty; foreach (string arg in extra) { if (arg.StartsWith("-")) { if (page == string.Empty) { Console.WriteLine("[ERROR] unknown option '{0}'", arg); } return(1); } page += $" {arg}"; } return(page.Trim().Length > 0 ? Page.Print(page, language, platform, markdown) : 0); }
internal static int Print(string pageName, string prefLanguage = null, string platform = null, bool markdown = false) { pageName = pageName.TrimStart().Replace(' ', '-'); List <string> languages; string language = prefLanguage; if (language == null) { languages = GetPreferredLanguages(); if (languages.Count == 0) { Console.WriteLine("[INFO] None of the preferred languages found, using {0} instead.", Program.DefaultLanguage); languages.Add(Program.DefaultLanguage); } } else { languages = new List <string> { language }; } platform = platform ?? GetPlatform(); string altPlatform = null; var results = Query(pageName); if (results.Count == 0) { if ((DateTime.UtcNow.Date - Cache.LastUpdate()).TotalDays > 5) { Console.WriteLine("Page not found."); Console.Write("Cache older than 5 days. "); Updater.Update(); results = Query(pageName); } if (results.Count == 0) { return(NotFound(pageName)); } } results = results.OrderBy(item => item, new PageComparer(new[] { platform, "common" }, languages)).ToList(); Page page; try { page = Find(results, languages, platform); } catch (Exception) { if (prefLanguage != null) { Console.WriteLine("Page not found in the requested language."); return(2); } try { page = FindAlternative(results, languages); if (platform != page.Platform && page.Platform != "common") { altPlatform = page.Platform; } } catch (Exception) { return(NotFound(pageName)); } } if (!page.Local) { Console.WriteLine("Page not cached, downloading..."); page.Download(); } string path = page.GetPath(); if (!File.Exists(path)) { Console.WriteLine("[ERROR] File \"{0}\" not found.", path); return(1); } if (markdown) { Console.WriteLine(File.ReadAllText(path)); } else { return(Render(path, altPlatform)); } return(0); }
private static int doMain(string[] args) { bool showHelp = false; bool list = false; bool ignorePlatform = false; string language = null; string platform = null; string search = null; bool markdown = false; string render = null; var options = new OptionSet { "Usage: tldr command [options]" + Environment.NewLine, "Simplified and community-driven man pages" + Environment.NewLine, { "a|list-all", "List all pages", a => list = ignorePlatform = a != null }, { "c|clear-cache", "Clear the cache", c => { CustomSpinner.Run("Clearing cache", Cache.Clear); Environment.Exit(0); } }, { "f=|render=", "Render a specific markdown file", v => render = v }, { "h|help", "Display this help text", h => showHelp = h != null }, { "l|list", "List all pages for the current platform and language", l => list = l != null }, { "list-os", "List all platforms", o => { Cache.Check(); Console.WriteLine(string.Join(Environment.NewLine, ListPlatform())); } }, { "list-languages", "List all languages", la => { Cache.Check(); Console.WriteLine(string.Join(Environment.NewLine, ListLanguages().Select(lang => { string name = Locale.GetLanguageName(lang); return(name == null ? lang : $"{lang}:\t{name}"); }))); } }, { "L=|language=|lang=", "Specifies the preferred language", la => language = la }, { "m|markdown", "Show the markdown source of a page", v => markdown = v != null }, { "p=|platform=", "Override the default platform", o => platform = o }, { "s=|search=", "Search for a string", s => search = s }, { "u|update", "Update the cache", u => Updater.Update() }, { "self-update", "Check for tldr-sharp updates", u => { SelfUpdater.CheckSelfUpdate(); Environment.Exit(0); } }, { "v|version", "Show version information", v => { string version = FileVersionInfo.GetVersionInfo(Assembly.GetExecutingAssembly().Location) .ProductVersion; Console.WriteLine($"tldr-sharp {version}"); Console.WriteLine("tldr-pages client specification " + ClientSpecVersion); Environment.Exit(0); } } }; List <string> extra; try { extra = options.Parse(args); } catch (OptionException e) { Console.WriteLine(e.Message); return(1); } if (showHelp || args.Length == 0) { options.WriteOptionDescriptions(Console.Out); return(args.Length == 0 ? 1 : 0); } if (render != null) { return(PageController.Render(render)); } // All following functions rely on the cache, so check it. Cache.Check(); if (language != null) { if (!CheckLanguage(language)) { Console.WriteLine("[ERROR] unknown language '{0}'", language); return(1); } } if (list) { PageController.ListAll(ignorePlatform, language, platform); return(0); } if (search != null) { return(PageController.Search(search, language, platform)); } StringBuilder builder = new StringBuilder(); foreach (string arg in extra) { if (arg.StartsWith("-")) { if (builder.Length == 0) { Console.WriteLine("[ERROR] unknown option '{0}'", arg); } return(1); } builder.Append($" {arg}"); } string page = builder.ToString(); return(page.Trim().Length > 0 ? PageController.Print(page, language, platform, markdown) : 0); }