From ee1ce5188afa50ab37f946c5b7962bab04cc3d15 Mon Sep 17 00:00:00 2001 From: Adison Cavani Date: Sat, 9 Oct 2021 21:18:47 +0200 Subject: [PATCH] Update help and parsing args --- src/Help.cs | 28 +++++++++++ src/Program.cs | 81 ++++++++++++------------------ src/ProgramOptions.cs | 3 ++ src/Properties/launchSettings.json | 8 +++ 4 files changed, 70 insertions(+), 50 deletions(-) create mode 100644 src/Help.cs create mode 100644 src/Properties/launchSettings.json diff --git a/src/Help.cs b/src/Help.cs new file mode 100644 index 0000000..ca06887 --- /dev/null +++ b/src/Help.cs @@ -0,0 +1,28 @@ +using CommandLine; +using CommandLine.Text; +using System; +using System.Collections.Generic; + +namespace DistroGrubThemes +{ + public class Help + { + public static void DisplayHelp(ParserResult result, IEnumerable errs) + { + HelpText helpText = null; + if (errs.IsVersion()) + helpText = HelpText.AutoBuild(result); + else + { + helpText = HelpText.AutoBuild(result, h => + { + h.AdditionalNewLineAfterOption = false; + h.Heading = string.Empty; + h.Copyright = string.Empty; + return HelpText.DefaultParsingErrorsHandler(result, h); + }, e => e); + } + Console.WriteLine(helpText); + } + } +} diff --git a/src/Program.cs b/src/Program.cs index 4bc81fb..3f6f66a 100644 --- a/src/Program.cs +++ b/src/Program.cs @@ -1,90 +1,70 @@ -using System; +using CommandLine; +using System; using System.Collections.Generic; using System.IO; using System.Linq; -using CommandLine; namespace DistroGrubThemes { - internal class Program { - string repoPath = string.Empty; - string iconsPath = string.Empty; - string customizePath = string.Empty; - string fontsPath = string.Empty; - static void Main(string[] args) { - Parser.Default.ParseArguments(args).WithParsed(RunOptions).WithNotParsed(HandleParseError); + var parser = new Parser(with => with.HelpWriter = null); + var parserResult = parser.ParseArguments(args); + + parserResult.WithParsed(options => RunOptions(options)).WithNotParsed(errs => Help.DisplayHelp(parserResult, errs)); } static void RunOptions(ProgramOptions opts) { Program program = new Program(); - - if (string.IsNullOrWhiteSpace(opts.ArchivedFiles)) - { - program.UpdateAssets(); - } - - else if(opts.ArchivedFiles == "all") - { - Console.WriteLine("Correct"); - } - - else - { - Console.WriteLine("DistroGrubThemes 1.0.0"); - Console.WriteLine("Copyright (C) 2021 Adison Cavani\n"); - Console.WriteLine("ERROR(S): "); - Console.WriteLine($" Argument {opts.ArchivedFiles} is unknown."); - Console.WriteLine("\n -a, --archive\tTest\n"); - } + program.CheckRepoPath(opts.RepositoryPath); + program.UpdateAssets(opts.RepositoryPath); } - static void HandleParseError(IEnumerable errs) + + + void UpdateAssets(string path) { - Environment.Exit(1); + UpdateIcons(path + @"\assets\icons", path + @"\customize"); + UpdateFonts(path + @"\font", path + @"\customize"); } - void UpdateAssets() - { - Console.Write("Repository path: "); - CheckRepoPath(Console.ReadLine()); - - iconsPath = repoPath + @"\assets\icons"; - customizePath = repoPath + @"\customize"; - fontsPath = repoPath + @"\font"; - - UpdateIcons(); - UpdateFonts(); - } - - void UpdateIcons() + void UpdateIcons(string iconsPath, string customizePath) { + Console.Write("Updating icons ... "); var icons = FilesArray(iconsPath); - foreach (var directory in CustomDirectories()) + foreach (var directory in CustomDirectories(customizePath)) { foreach (var icon in icons) { File.Copy(iconsPath + @"\" + icon, directory + @"\icons\" + icon, true); } } + + Console.ForegroundColor = ConsoleColor.Green; + Console.Write("OK\n"); + Console.ResetColor(); } - void UpdateFonts() + void UpdateFonts(string fontsPath, string customizePath) { + Console.Write("Updating fonts ... "); var fonts = FilesArray(fontsPath); - foreach (var directory in CustomDirectories()) + foreach (var directory in CustomDirectories(customizePath)) { foreach (var font in fonts) { File.Copy(fontsPath + @"\" + font, directory + @"\" + font, true); } } + + Console.ForegroundColor = ConsoleColor.Green; + Console.Write("OK\n"); + Console.ResetColor(); } List FilesArray(string folderPath) @@ -92,7 +72,7 @@ namespace DistroGrubThemes return new List(Directory.GetFiles(folderPath).Select(Path.GetFileName)); } - string[] CustomDirectories() + string[] CustomDirectories(string customizePath) { return Directory.GetDirectories(customizePath); } @@ -102,14 +82,15 @@ namespace DistroGrubThemes if (Directory.Exists(path) && path.Contains("distro-grub-themes")) { int index = path.IndexOf("distro-grub-themes") + 18; - repoPath = path.Substring(0, index); + path = path.Substring(0, index); } else { Console.ForegroundColor = ConsoleColor.Red; - Console.WriteLine("ERROR: Could not find repository in this path!"); + Console.Write("error: "); Console.ResetColor(); + Console.Write("could not find repository in this path"); Environment.Exit(1); } } diff --git a/src/ProgramOptions.cs b/src/ProgramOptions.cs index dc52d12..643b77e 100644 --- a/src/ProgramOptions.cs +++ b/src/ProgramOptions.cs @@ -4,6 +4,9 @@ namespace DistroGrubThemes { public class ProgramOptions { + [Option('r', "repository", Required = true, HelpText = "Path to repository.")] + public string RepositoryPath { get; set; } + [Option('a', "archive", HelpText = "Create theme archive.")] public string ArchivedFiles { get; set; } } diff --git a/src/Properties/launchSettings.json b/src/Properties/launchSettings.json new file mode 100644 index 0000000..8a4c539 --- /dev/null +++ b/src/Properties/launchSettings.json @@ -0,0 +1,8 @@ +{ + "profiles": { + "DistroGrubThemes": { + "commandName": "Project", + "commandLineArgs": "\"-rC:\\Users\\Adison\\repos\\distro-grub-themes\"" + } + } +} \ No newline at end of file