예제 #1
0
        public static void Main(string [] args)
        {
            var parser    = new CommandLineParser.CommandLineParser();
            var algorithm = new EnumeratedValueArgument <string> (
                'a',
                "algorithm",
                "available algorithms for computing hash, default to SHA1",
                new [] { Md2Hash.ALGORITHM_NAME,
                         Md5Hash.ALGORITHM_NAME,
                         Sha1Hash.ALGORITHM_NAME,
                         Sha256Hash.ALGORITHM_NAME,
                         Sha384Hash.ALGORITHM_NAME,
                         Sha512Hash.ALGORITHM_NAME });

            algorithm.DefaultValue = Sha1Hash.ALGORITHM_NAME;
            var source = new ValueArgument <string> (
                's',
                "source",
                "source to compute hash");

            source.Optional = false;
            var salt = new ValueArgument <string> (
                't',
                "salt",
                "salt for computing hash");
            var iterations = new ValueArgument <int> (
                'i',
                "iterations",
                "iterations when compute hash");

            iterations.DefaultValue = 1;
            parser.Arguments.AddRange(
                new Argument [] { algorithm, source, salt, iterations });

            parser.ParseCommandLine(args);

            if (!source.Parsed || string.IsNullOrEmpty(source.Value))
            {
                parser.ShowUsage();
                return;
            }

            SimpleHash hash = null;

            if (algorithm.Parsed)
            {
                hash = new SimpleHash(algorithm.Value, source.Value, salt.Value, iterations.Value);
            }
            if (hash == null)
            {
                hash = new Sha1Hash(source.Value, salt.Value, iterations.Value);
            }

            Console.WriteLine("algorithms  :" + hash.AlgorithmName);
            Console.WriteLine("source      :" + source.Value);
            Console.WriteLine("salt        :" + salt.Value);
            Console.WriteLine("iterations  :" + iterations.Value);
            Console.WriteLine("hash(hex)   :" + hash.ToHex());
            Console.WriteLine("hash(base64):" + hash.ToBase64());
        }
예제 #2
0
        public void EnumeratedValueArgument_shouldNotAllowIgnoreCaseForNonStrings()
        {
            EnumeratedValueArgument <int> arg = new EnumeratedValueArgument <int>('t');
            var e = Assert.Throws <ArgumentException>(() => arg.IgnoreCase = true);

            Assert.Equal("Ignore case can be used only for string arguments, type of TValue is System.Int32", e.Message);
        }
예제 #3
0
        private CommandLineParser.CommandLineParser InitIgnoreCase()
        {
            var commandLineParser = new CommandLineParser.CommandLineParser();

            commandLineParser.IgnoreCase = true;
            commandLineParser.ShowUsageOnEmptyCommandline = true;

            SwitchArgument showArgument = new SwitchArgument('s', "show", "Set whether show or not", true);

            SwitchArgument hideArgument = new SwitchArgument('h', "hide", "Set whether hid or not", false);

            ValueArgument <string> level = new ValueArgument <string>('l', "level", "Set the level");

            ValueArgument <decimal> version = new ValueArgument <decimal>('v', "version", "Set desired version");

            ValueArgument <Point> point = new ValueArgument <Point>('p', "point", "specify the point");

            BoundedValueArgument <int> optimization = new BoundedValueArgument <int>('o', "optimization", 0, 3);

            EnumeratedValueArgument <string> color = new EnumeratedValueArgument <string>('c', "color", new[] { "red", "green", "blue" });

            FileArgument inputFile = new FileArgument('i', "input", "Input file");

            inputFile.FileMustExist = false;
            FileArgument outputFile = new FileArgument('x', "output", "Output file");

            outputFile.FileMustExist = false;

            DirectoryArgument inputDirectory = new DirectoryArgument('d', "directory", "Input directory");

            inputDirectory.DirectoryMustExist = false;

            point.ConvertValueHandler = delegate(string stringValue)
            {
                if (stringValue.StartsWith("[") && stringValue.EndsWith("]"))
                {
                    string[] parts =
                        stringValue.Substring(1, stringValue.Length - 2).Split(';', ',');
                    Point p = new Point();
                    p.x = int.Parse(parts[0]);
                    p.y = int.Parse(parts[1]);
                    return(p);
                }

                throw new CommandLineArgumentException("Bad point format", "point");
            };

            commandLineParser.Arguments.Add(showArgument);
            commandLineParser.Arguments.Add(hideArgument);
            commandLineParser.Arguments.Add(level);
            commandLineParser.Arguments.Add(version);
            commandLineParser.Arguments.Add(point);
            commandLineParser.Arguments.Add(optimization);
            commandLineParser.Arguments.Add(color);
            commandLineParser.Arguments.Add(inputFile);
            commandLineParser.Arguments.Add(outputFile);
            commandLineParser.Arguments.Add(inputDirectory);

            return(commandLineParser);
        }
예제 #4
0
        private static void Main(string[] args)
        {
            CommandLineParser.CommandLineParser parser = new CommandLineParser.CommandLineParser();

            ValueArgument <string> assembly = new ValueArgument <string>(
                'a', "assembly", "Specify the assembly file");

            assembly.Optional = false;

            SwitchArgument silent = new SwitchArgument(
                's', "silent", "Enable silent mode", false);

            EnumeratedValueArgument <string> output = new EnumeratedValueArgument <string>(
                'o', "output", new string[] { "none", "screen", "environment-machine", "environment-user", "environment-process" });

            output.DefaultValue = "none";

            ValueArgument <string> name = new ValueArgument <string>(
                'n', "name", "Name of environment to create");

            parser.Arguments.Add(assembly);
            parser.Arguments.Add(silent);
            parser.Arguments.Add(output);
            parser.Arguments.Add(name);

            if (args.Length == 0)
            {
                parser.ShowUsage();
            }
            else
            {
                try
                {
                    parser.ParseCommandLine(args);

                    if (!string.Equals(output.Value, "none", StringComparison.OrdinalIgnoreCase) && !string.Equals(output.Value, "screen", StringComparison.OrdinalIgnoreCase))
                    {
                        if (string.IsNullOrEmpty(name.Value))
                        {
                            throw new Exception("The name parameter must be informed");
                        }
                    }

                    var version = GetInfo.GetFileVersionFrom(assembly.Value);

                    switch (output.Value.ToLower())
                    {
                    case "screen":
                        Console.Write(version);
                        break;

                    case "environment-machine":
                        EnvironmentHelper.SetEnvironment(name.Value, version, EnvironmentVariableTarget.Machine);
                        break;

                    case "environment-user":
                        EnvironmentHelper.SetEnvironment(name.Value, version, EnvironmentVariableTarget.User);
                        break;

                    case "environment-process":
                        EnvironmentHelper.SetEnvironment(name.Value, version, EnvironmentVariableTarget.Process);
                        break;
                    }

                    if (!silent.Value)
                    {
                        Console.WriteLine($"Assembly File Version {version}");
                    }

                    Environment.Exit(0); // Exit with successful
                }
                catch (Exception e)
                {
                    Console.WriteLine(e.Message);
                    Environment.Exit(2); // Exit default error
                }
            }

            Environment.Exit(1); // Exit without execute
        }