Exemplo n.º 1
0
        public static void Main(string[] args)
        {
            // debug stuff
#if DEBUG
            if (args.Length == 0)
            {
                // input for arguments
                Console.Write("args: ");
                var input = Console.ReadLine();
                Console.Clear();

                // string builder for string operations
                var sb = new StringBuilder();

                // if not empty
                if (!string.IsNullOrWhiteSpace(input))
                {
                    var rargs     = new List <string>();
                    var inLiteral = false;
                    for (var i = 0; i < input.Length; i++)
                    {
                        var c = input[i];
                        if (inLiteral)
                        {
                            if (c == '"')
                            {
                                if (i != input.Length - 1 && input[i + 1] == '"')
                                {
                                    sb.Append('"');
                                }
                                else
                                {
                                    inLiteral = false;
                                }
                            }
                            else
                            {
                                sb.Append(c);
                            }
                        }
                        else
                        {
                            if (char.IsWhiteSpace(c))
                            {
                                if (sb.Length == 0)
                                {
                                    continue;
                                }
                                rargs.Add(sb.ToString());
                                sb.Length = 0;
                            }
                            else if (c == '"')
                            {
                                inLiteral = true;
                            }
                            else
                            {
                                sb.Append(c);
                            }
                        }
                    }
                    if (sb.Length != 0)
                    {
                        rargs.Add(sb.ToString());
                    }
                    args = rargs.ToArray();
                }

                // provide info
                Console.WriteLine($"calling with {args.Length} arguments");

                // show the arguments that will be passed over
                sb.Length = 0;
                sb.Append('[');
                var l = args.Length;
                for (var i = 0; i < l; i++)
                {
                    sb.Append($"\"{args[i]}\"{(i != l - 1 ? ", " : string.Empty)}");
                }
                sb.Append(']');
                Console.WriteLine(sb.ToString());
                Console.WriteLine();
            }
#endif

            try
            {
                BlockyAlgorithm.SetBlockfindingDebugConsoleEnabled(false);

                // initiate the parser
                IArgumentParser <CommandLineLayers> argumentParser = new ArgumentParser <CommandLineLayers>();
                argumentParser.Description = "A command line tool for compressing Open Foam (r) files.";
                argumentParser.Name        = "ofc.exe";

                // add validators
                argumentParser.Validator <int>(new FuncValidator((string v, ref object d) =>
                {
                    int value;
                    if (!int.TryParse(v, out value))
                    {
                        return(false);
                    }
                    d = value;
                    return(true);
                }));

                // add parser definitions
                argumentParser.NewLayer(CommandLineLayers.Help).AddOption(e => e.SetShortName('h').SetLongName("help").Description("Display this help message."));
                argumentParser.NewLayer(CommandLineLayers.Version).AddOption(e => e.SetLongName("version").Description("Display the current version of the tool."));

                argumentParser.NewLayer(CommandLineLayers.ListAlgorithms).Command("algorithms").Command("list", "Lists all available algorithms for compression/decompression.");

                argumentParser.NewLayer(CommandLineLayers.CompressDirectory).Command("compress").Command("directory", "Compress the specified directory.").Argument("input").Argument("output").Option("algorithm", e => e.SetName("name")).Option("rounding", e => e.SetName("digits").Type <int>()).Option('f').Option('r').Option('p').Option('s');
                argumentParser.NewLayer(CommandLineLayers.CompressFile).Command("compress").Command("file", "Compress the specified file.").Argument("input").Argument("output").Option("algorithm", e => e.SetName("name")).Option("rounding", e => e.SetName("digits").Type <int>()).Option('f').Option('s');

                argumentParser.NewLayer(CommandLineLayers.DecompressDirectory).Command("decompress").Command("directory", "Decompress the specified compressed directory.").Argument("input").Argument("output").Option("algorithm", e => e.SetName("name")).Option('f').Option('r').Option('p');
                argumentParser.NewLayer(CommandLineLayers.DecompressFile).Command("decompress").Command("file", "Decompress the specified compressed file or directory.").Argument("input").Argument("output").Option("algorithm", e => e.SetName("name")).Argument("data", true).Option('f');

                argumentParser.NewOption().SetLongName("rounding").Description("Enable rounding to the specified amount of digits.");
                argumentParser.NewOption().SetShortName('f').Description("Force overriding of files.");
                argumentParser.NewOption().SetShortName('r').Description("Enable recursive compression/decompression.");
                argumentParser.NewOption().SetShortName('p').Description("Enable parallel compression/decompression.");
                argumentParser.NewOption().SetShortName('s').Description("Treat anonymous lists as lists of one type.");

                // parse the arguments
                var result = argumentParser.Parse(args);

                var ok = false;
                // check if the parser succeeded
                if (result.Success)
                {
                    ok = true;
                    var             manager = new OfcActionManager();
                    IConfiguaration config  = new SimpleConfiguration();

                    // check for rounding
                    if (result.GetFlag("rounding"))
                    {
                        config["rounding"]         = true;
                        config["roundingDecimals"] = result.GetOption <int>("rounding");
                    }
                    if (result.GetFlag("s"))
                    {
                        config["simplealists"] = true;
                    }

                    // check is algorithm is supplied
                    string algorithm = null;
                    if (result.GetFlag("algorithm"))
                    {
                        var supposedAlgorithm = result.GetOption("algorithm") as string;
                        if (supposedAlgorithm == null || !AlgorithmHelper.IsValidAlgorithm(supposedAlgorithm))
                        {
                            Console.WriteLine("Invalid algorithm.\n");
                            Console.Write(argumentParser.GenerateHelp());
                            return;
                        }
                        algorithm = supposedAlgorithm;
                    }

                    switch (result.LayerId)
                    {
                    // Displays the CLI help
                    case CommandLineLayers.Help:
                        Console.Write(argumentParser.GenerateHelp());
                        break;

                    // Displays the current version
                    case CommandLineLayers.Version:
                        Console.WriteLine($"{argumentParser.Name} [v1.0]");
                        break;

                    // Compresses the specified file
                    case CommandLineLayers.CompressFile:
                        manager.AddCompressFileActionWithAlgorithm(algorithm, config, result[0], result[1]);
                        manager.Override = result['f'];
                        manager.Handle();
                        break;

                    // Compresses the specified directory
                    case CommandLineLayers.CompressDirectory:
                        manager.AddCompressDirectoryActionWithAlgorithm(algorithm, config, result[0], result[1], result['r']);
                        if (manager.Empty)
                        {
                            Console.WriteLine(" WARNING: input folder is empty");
                        }
                        manager.Override = result['f'];
                        manager.Parallel = result['p'];
                        manager.Handle();
                        break;

                    // Decompresses the specified file
                    case CommandLineLayers.DecompressFile:
                        manager.AddDecompressFileActionWithAlgorithm(algorithm, config, result[0], result[2] ?? Path.ChangeExtension(result[0], ActionUtils.DataFileExtention), result[1]);
                        manager.Handle();
                        break;

                    // Decompresses the specified directory
                    case CommandLineLayers.DecompressDirectory:
                        manager.AddDecompressDirectoryActionWithAlgorithm(algorithm, config, result[0], result[1], result['r']);
                        if (manager.Empty)
                        {
                            Console.WriteLine(" WARNING: input folder is empty");
                        }
                        manager.Override = result['f'];
                        manager.Parallel = result['p'];
                        manager.Handle();
                        break;

                    // List all available algorithms
                    case CommandLineLayers.ListAlgorithms:
                        var defaultAlgorithm = AlgorithmHelper.DefaultAlgorithm;
                        foreach (var name in AlgorithmHelper.RegisteredAlgorithmByName.OrderBy(e => e))
                        {
                            Console.WriteLine($" {(defaultAlgorithm == name ? "*" : " ")} {name}");
                        }
                        Console.WriteLine();
                        break;
                    }
                }

                // Write an error message
                if (ok)
                {
                    return;
                }
                Console.WriteLine("Invalid arguments.\n");
                Console.Write(argumentParser.GenerateHelp());
            }
            catch (Exception ex)
            {
                Console.WriteLine("fatal application error: \n" + ex);
            }
        }