Esempio n. 1
1
        private static int OnAppExecute(CommandArgument argOperator, CommandOption optMin, CommandOption optMax, CommandOption optCount)
        {
            if (argOperator.Values.Count == 0)
            {
                Console.Error.WriteLine("未指定算数类型。");
                return -1;
            }

            // 简单起见,我们假设 argOperator 始终有正确的值,因此忽略参数检测
            string[] operators = argOperator.Values.Select(o =>
            {
                switch (o)
                {
                    case "减":
                        return "-";
                    case "乘":
                        return "×";
                    case "除":
                        return "÷";
                    case "加":
                    default:
                        return "+";
                }
            }).ToArray();

            int minValue, maxValue, count;

            if (optMin.HasValue())
            {
                if (!int.TryParse(optMin.Value(), out minValue))
                {
                    Console.Error.WriteLine($"invalid minValue: {optMin.Value()}");
                    return -2;
                }
            }
            else
            {
                minValue = 0;
            }

            if (optMax.HasValue())
            {
                if (!int.TryParse(optMax.Value(), out maxValue))
                {
                    Console.Error.WriteLine($"invalid maxValue: {optMax.Value()}");
                    return -3;
                }
            }
            else
            {
                maxValue = 100;
            }

            if (optCount.HasValue())
            {
                if (!int.TryParse(optCount.Value(), out count))
                {
                    Console.Error.WriteLine($"invalid count: {optCount.Value()}");
                    return -4;
                }
            }
            else
            {
                count = 10;
            }

            Random rndForOp = new Random();
            Random rnd = new Random();
            int rndMaxValue = maxValue + 1; // 因为 Random 类的 Next 方法返回的值是大于等于最小值,小于最大值,因此我们需要 maxValue + 1,确保 maxValue 是可能的返回结果之一
            int valueWidth = maxValue.ToString().Length;
            for (int i = 1; i <= count; i++)
            {
                Console.Write("{0} {1} {2} = "
                    , rnd.Next(minValue, rndMaxValue).ToString().PadLeft(valueWidth)
                    , operators[rndForOp.Next(0, operators.Length)]
                    , rnd.Next(minValue, rndMaxValue).ToString().PadLeft(valueWidth));

                if (i % 3 == 0)
                {
                    Console.WriteLine();
                }
                else
                {
                    Console.Write("\t");
                }
            }

            Console.WriteLine();
            return 0;
        }
        private void AddDotnetBaseParameters()
        {
            _app.HelpOption("-?|-h|--help");

            _configurationOption = _app.Option(
                "-c|--configuration <CONFIGURATION>",
                "Configuration under which to build",
                CommandOptionType.SingleValue);
            _outputOption = _app.Option(
                "-o|--output <OUTPUT_DIR>",
                "Directory in which to find the binaries to be run",
                CommandOptionType.SingleValue);
            _buildBasePath = _app.Option(
                "-b|--build-base-path <OUTPUT_DIR>",
                "Directory in which to find temporary outputs",
                CommandOptionType.SingleValue);
            _frameworkOption = _app.Option(
                "-f|--framework <FRAMEWORK>",
                "Looks for test binaries for a specific framework",
                CommandOptionType.SingleValue);
            _runtimeOption = _app.Option(
                "-r|--runtime <RUNTIME_IDENTIFIER>",
                "Look for test binaries for a for the specified runtime",
                CommandOptionType.SingleValue);
            _projectPath = _app.Option(
                "-p|--project-path <PROJECT_JSON_PATH>",
                "Path to Project.json that contains the tool dependency",
                CommandOptionType.SingleValue);
            _command = _app.Argument(
                "<COMMAND>",
                "The command to execute.");
        }