Пример #1
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);
        }
Пример #2
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);
        }