Пример #1
0
        /// <summary>
        /// Gets the program arguments by reading them interactively.
        /// </summary>
        /// <returns>Input arguments read from the keyboard.</returns>
        private static ProgramArguments GetProgramArgumentsInteractively()
        {
            ProgramArguments programArgs = new ProgramArguments();

            foreach (PropertyInfo property in typeof(ProgramArguments).GetProperties())
            {
                string helpText     = "";
                bool   isRequired   = false;
                object?defaultValue = null;
                foreach (CustomAttributeData attrData in property.CustomAttributes)
                {
                    if (attrData.AttributeType == typeof(OptionAttribute))
                    {
                        foreach (CustomAttributeNamedArgument namedArg in attrData.NamedArguments)
                        {
                            switch (namedArg.MemberName)
                            {
                            case nameof(OptionAttribute.HelpText):
                                helpText = (string)(namedArg.TypedValue.Value ?? "");
                                break;

                            case nameof(OptionAttribute.Required):
                                isRequired = (bool)(namedArg.TypedValue.Value ?? false);
                                break;

                            case nameof(OptionAttribute.Default):
                                defaultValue = namedArg.TypedValue.Value;
                                break;
                            }
                        }
                        break;
                    }
                    Debug.Assert(isRequired != (defaultValue != null));
                }

                Console.WriteLine(helpText);
                bool success = false;
                while (!success)
                {
                    Console.Write($"{property.Name}{(defaultValue == null ? "" : " [" + defaultValue + "]")}: ");

                    string valueStr = Console.ReadLine().Trim();
                    if (property.PropertyType == typeof(CloudEnvironment))
                    {
                        Debug.Assert(isRequired);
                        success = Enum.TryParse(valueStr, true, out CloudEnvironment environment);
                        property.SetValue(programArgs, environment);
                    }
                    else if (property.PropertyType == typeof(CloudRegion))
                    {
                        Debug.Assert(isRequired);
                        success = Enum.TryParse(valueStr, true, out CloudRegion region);
                        property.SetValue(programArgs, region);
                    }
                    else if (property.PropertyType == typeof(string))
                    {
                        if (valueStr != "")
                        {
                            property.SetValue(programArgs, valueStr);
                            success = true;
                        }
                        else if (defaultValue != null)
                        {
                            property.SetValue(programArgs, defaultValue);
                            success = true;
                        }
                    }
                    else if (property.PropertyType == typeof(int))
                    {
                        if (valueStr != "")
                        {
                            success = int.TryParse(valueStr, out int intValue);
                            property.SetValue(programArgs, intValue);
                        }
                        else if (defaultValue != null)
                        {
                            property.SetValue(programArgs, defaultValue);
                            success = true;
                        }
                    }
                    else if (property.PropertyType == typeof(bool))
                    {
                        if (valueStr != "")
                        {
                            success = bool.TryParse(valueStr, out bool boolValue);
                            property.SetValue(programArgs, boolValue);
                        }
                        else if (defaultValue != null)
                        {
                            property.SetValue(programArgs, defaultValue);
                            success = true;
                        }
                    }
                    else
                    {
                        Debug.Fail("Unsupported value type.");
                    }

                    if (!success)
                    {
                        ConsoleColor originalColor = Console.ForegroundColor;
                        Console.ForegroundColor = ConsoleColor.Red;
                        Console.WriteLine("Invalid value.");
                        Console.ForegroundColor = originalColor;
                    }
                }

                Console.WriteLine();
            }

            return(programArgs);
        }
Пример #2
0
        /// <summary>
        /// Gets the platform endpoint URL to use that matches the command-line arguments specified.
        /// </summary>
        /// <param name="p_Args">Parsed command-line arguments.</param>
        /// <returns>The platform endpoint URL to use.</returns>
        private static string GetPlatformApiUrl(ProgramArguments p_Args)
        {
            switch (p_Args.region)
            {
            case CloudRegion.UsEast1:
                switch (p_Args.environment)
                {
                case CloudEnvironment.Hipaa:
                    return(Constants.PlatformEndpoint.UsEast1.HIPAA_PLATFORM_API_URL);

                case CloudEnvironment.Prod:
                    return(Constants.PlatformEndpoint.UsEast1.PROD_PLATFORM_API_URL);

                case CloudEnvironment.QA:
                    return(Constants.PlatformEndpoint.UsEast1.QA_PLATFORM_API_URL);

                case CloudEnvironment.Dev:
                    return(Constants.PlatformEndpoint.UsEast1.DEV_PLATFORM_API_URL);

                default:
                    throw new InvalidEnumArgumentException(INVALID_CLOUD_ENVIRONMENT);
                }

            case CloudRegion.EuWest1:
                switch (p_Args.environment)
                {
                case CloudEnvironment.Hipaa:
                    throw new InvalidEnumArgumentException(string.Format(ENVIRONMENT_X_IS_INVALID_FOR_REGION_Y, p_Args.environment, p_Args.region));

                case CloudEnvironment.Prod:
                    return(Constants.PlatformEndpoint.EuWest1.PROD_PLATFORM_API_URL);

                case CloudEnvironment.QA:
                    return(Constants.PlatformEndpoint.EuWest1.QA_PLATFORM_API_URL);

                case CloudEnvironment.Dev:
                    return(Constants.PlatformEndpoint.EuWest1.DEV_PLATFORM_API_URL);

                default:
                    throw new InvalidEnumArgumentException(INVALID_CLOUD_ENVIRONMENT);
                }

            case CloudRegion.ApSouthEast2:
                switch (p_Args.environment)
                {
                case CloudEnvironment.Prod:
                    return(Constants.PlatformEndpoint.ApSoutheast2.PROD_PLATFORM_API_URL);

                case CloudEnvironment.Hipaa:
                case CloudEnvironment.QA:
                case CloudEnvironment.Dev:
                    throw new InvalidEnumArgumentException(string.Format(ENVIRONMENT_X_IS_INVALID_FOR_REGION_Y, p_Args.environment, p_Args.region));

                default:
                    throw new InvalidEnumArgumentException(INVALID_CLOUD_ENVIRONMENT);
                }

            default:
                throw new InvalidEnumArgumentException(INVALID_CLOUD_REGION);
            }
        }