Ejemplo n.º 1
0
        /// <summary>
        /// Processes the -SettingFile Argument.
        /// </summary>
        /// <param name="args">
        /// The command line parameters to be processed.
        /// </param>
        /// <param name="settingFileArgIndex">
        /// The index in args to the argument following '-SettingFile'.
        /// </param>
        /// <param name="parser">
        /// Used to allow the helper to write errors to the console.  If not supplied, no errors will be written.
        /// </param>
        /// <returns>
        /// Returns true if the argument was parsed successfully and false if not.
        /// </returns>
        private static bool TryParseSettingFileHelper(string[] args, int settingFileArgIndex, CommandLineParameterParser parser)
        {
            if (settingFileArgIndex >= args.Length)
            {
                if (parser != null)
                {
                    parser.WriteCommandLineError(
                        CommandLineParameterParserStrings.MissingSettingsFileArgument);
                }

                return(false);
            }

            string configFile = null;

            try
            {
                configFile = NormalizeFilePath(args[settingFileArgIndex]);
            }
            catch (Exception ex)
            {
                if (parser != null)
                {
                    string error = string.Format(CultureInfo.CurrentCulture, CommandLineParameterParserStrings.InvalidSettingsFileArgument, args[settingFileArgIndex], ex.Message);
                    parser.WriteCommandLineError(error);
                }

                return(false);
            }

            if (!System.IO.File.Exists(configFile))
            {
                if (parser != null)
                {
                    string error = string.Format(CultureInfo.CurrentCulture, CommandLineParameterParserStrings.SettingsFileNotExists, configFile);
                    parser.WriteCommandLineError(error);
                }

                return(false);
            }

            PowerShellConfig.Instance.SetSystemConfigFilePath(configFile);
            return(true);
        }