コード例 #1
0
ファイル: Program.cs プロジェクト: Vanillepanda/ParManager
        private static void Main(string[] args)
        {
            using var parser = new Parser(with => with.HelpWriter = null);
            ParserResult <object> parserResult = parser.ParseArguments <Options.List, Options.Extract, Options.Create, Options.Remove, Options.Add>(args);

            parserResult
            .WithParsed <Options.List>(List)
            .WithParsed <Options.Extract>(Extract)
            .WithParsed <Options.Create>(Create)
            .WithParsed <Options.Remove>(Remove)
            .WithParsed <Options.Add>(Add)
            .WithNotParsed(x =>
            {
                if (args.Length == 1)
                {
                    if (File.Exists(args[0]))
                    {
                        var opts = new Options.Extract
                        {
                            ParArchivePath  = args[0],
                            OutputDirectory = string.Concat(args[0], ".unpack"),
                            Recursive       = false,
                        };

                        Extract(opts);
                        return;
                    }

                    if (Directory.Exists(args[0]))
                    {
                        var opts = new Options.Create
                        {
                            InputDirectory = args[0],
                            ParArchivePath =
                                args[0].EndsWith(".unpack", StringComparison.InvariantCultureIgnoreCase)
                                        ? args[0].Substring(0, args[0].Length - 7)
                                        : string.Concat(args[0], ".par"),
                            AlternativeMode = false,
                            Compression     = 1,
                        };

                        Create(opts);
                        return;
                    }
                }

                var helpText = HelpText.AutoBuild(
                    parserResult,
                    h =>
                {
                    h.AutoHelp    = false;      // hide --help
                    h.AutoVersion = false;      // hide --version
                    return(HelpText.DefaultParsingErrorsHandler(parserResult, h));
                },
                    e => e);

                Console.WriteLine(helpText);
            });
        }
コード例 #2
0
        private static void Create(Options.Create opts)
        {
            WriteHeader();

            if (!Directory.Exists(opts.InputDirectory))
            {
                Console.WriteLine($"ERROR: \"{opts.InputDirectory}\" not found!!!!");
                return;
            }

            if (File.Exists(opts.ParArchivePath))
            {
                Console.WriteLine("WARNING: Output file already exists. It will be overwritten.");
                Console.Write("Continue? (y/N) ");
                string answer = Console.ReadLine();
                if (!string.IsNullOrEmpty(answer) && answer.ToUpperInvariant() != "Y")
                {
                    Console.WriteLine("CANCELLED BY USER.");
                    return;
                }

                File.Delete(opts.ParArchivePath);
            }

            var parameters = new ParArchiveWriterParameters
            {
                CompressorVersion = opts.Compression,
                OutputPath        = Path.GetFullPath(opts.ParArchivePath),
                IncludeDots       = !opts.AlternativeMode,
            };

            Console.Write("Reading input directory... ");
            string nodeName = new DirectoryInfo(opts.InputDirectory).Name;
            Node   node     = ReadDirectory(opts.InputDirectory, nodeName);

#pragma warning disable CA1308 // Normalize strings to uppercase
            node.SortChildren((x, y) => string.CompareOrdinal(x.Name.ToLowerInvariant(), y.Name.ToLowerInvariant()));
#pragma warning restore CA1308 // Normalize strings to uppercase
            Console.WriteLine("DONE!");

            ParArchiveWriter.NestedParCreating += sender => Console.WriteLine($"Creating nested PAR {sender.Name}... ");
            ParArchiveWriter.NestedParCreated  += sender => Console.WriteLine($"{sender.Name} created!");
            ParArchiveWriter.FileCompressing   += sender => Console.WriteLine($"Compressing {sender.Name}... ");

            DateTime startTime = DateTime.Now;
            Console.WriteLine("Creating PAR (this may take a while)... ");
            Directory.CreateDirectory(Path.GetDirectoryName(Path.GetFullPath(opts.ParArchivePath)));
            node.TransformWith <ParArchiveWriter, ParArchiveWriterParameters>(parameters);
            node.Dispose();

            DateTime endTime = DateTime.Now;
            Console.WriteLine("DONE!");

            Console.WriteLine($"Time elapsed: {endTime - startTime:g}");
        }