示例#1
0
        /// <summary>
        /// Wrapped main program, uses <see cref="ConsoleException"/> as return code in case of
        /// error and does not wait at the end.
        /// </summary>
        private static void MainWrapper()
        {
            CommandLineHelper cmdLine = new CommandLineHelper();
            var showHelpOption        = cmdLine.RegisterOption("help").Alias("h", "?");
            var showVersionOption     = cmdLine.RegisterOption("version").Alias("ver");
            var debugOption           = cmdLine.RegisterOption("debug");
            var utf8Option            = cmdLine.RegisterOption("utf8");
            var utf16Option           = cmdLine.RegisterOption("utf16");
            var utf8NoBomOption       = cmdLine.RegisterOption("utf8nobom");
            var defaultEncodingOption = cmdLine.RegisterOption("defaultenc");

            try
            {
                //cmdLine.ReadArgs(Environment.CommandLine, true);   // Alternative split method, should have the same result
                cmdLine.Parse();
                showDebugOutput = debugOption.IsSet;
                if (showDebugOutput)
                {
                    ShowDebugMessage(
                        "Command line: " +
                        Environment.GetCommandLineArgs()
                        .Select(s => "[" + s + "]")
                        .Aggregate((a, b) => a + " " + b));
                }
            }
            catch (Exception ex)
            {
                throw new ConsoleException(ex.Message, ExitCodes.CmdLineError);
            }

            // Handle simple text output options
            if (showHelpOption.IsSet)
            {
                ShowHelp();
                return;
            }
            if (showVersionOption.IsSet)
            {
                ShowVersion();
                return;
            }

            if (cmdLine.FreeArguments.Length < 2)
            {
                throw new ConsoleException("Too few arguments.", ExitCodes.CmdLineError);
            }

            // Prepare arguments
            string fileName   = cmdLine.FreeArguments[0];
            string targetName = cmdLine.FreeArguments[1];

            Encoding encoding = null;

            if (utf8Option.IsSet)
            {
                encoding = Encoding.UTF8;
            }
            if (utf16Option.IsSet)
            {
                encoding = Encoding.Unicode;
            }
            if (utf8NoBomOption.IsSet)
            {
                encoding = new UTF8Encoding(false);
            }
            if (defaultEncodingOption.IsSet)
            {
                encoding = Encoding.Default;
            }

            Dictionary <string, string> data = new Dictionary <string, string>();

            for (int i = 2; i < cmdLine.FreeArguments.Length; i++)
            {
                string   arg   = cmdLine.FreeArguments[i];
                string[] parts = arg.Split(new[] { '=' }, 2);
                if (parts.Length < 2)
                {
                    throw new ConsoleException("Invalid name value pair specification in argument " + (i + 1) + ".", ExitCodes.CmdLineError);
                }
                if (data.ContainsKey(parts[0]))
                {
                    throw new ConsoleException("Duplicate name value pair specification in argument " + (i + 1) + ".", ExitCodes.CmdLineError);
                }
                data[parts[0]] = parts[1];
            }

            // Replace data in file
            ReplaceHelper rh = new ReplaceHelper(fileName, encoding);

            rh.ReplaceData(targetName, data);
        }
示例#2
0
        /// <summary>
        /// Wrapped main program, uses <see cref="ConsoleException"/> as return code in case of
        /// error and does not wait at the end.
        /// </summary>
        private static void MainWrapper()
        {
            CommandLineHelper cmdLine = new CommandLineHelper();
            var showHelpOption = cmdLine.RegisterOption("help").Alias("h", "?");
            var showVersionOption = cmdLine.RegisterOption("version").Alias("ver");
            var debugOption = cmdLine.RegisterOption("debug");
            var utf8Option = cmdLine.RegisterOption("utf8");
            var utf16Option = cmdLine.RegisterOption("utf16");
            var utf8NoBomOption = cmdLine.RegisterOption("utf8nobom");
            var defaultEncodingOption = cmdLine.RegisterOption("defaultenc");

            try
            {
                //cmdLine.ReadArgs(Environment.CommandLine, true);   // Alternative split method, should have the same result
                cmdLine.Parse();
                showDebugOutput = debugOption.IsSet;
                if (showDebugOutput)
                {
                    ShowDebugMessage(
                        "Command line: " +
                            Environment.GetCommandLineArgs()
                                .Select(s => "[" + s + "]")
                                .Aggregate((a, b) => a + " " + b));
                }
            }
            catch (Exception ex)
            {
                throw new ConsoleException(ex.Message, ExitCodes.CmdLineError);
            }

            // Handle simple text output options
            if (showHelpOption.IsSet)
            {
                ShowHelp();
                return;
            }
            if (showVersionOption.IsSet)
            {
                ShowVersion();
                return;
            }

            if (cmdLine.FreeArguments.Length < 2)
            {
                throw new ConsoleException("Too few arguments.", ExitCodes.CmdLineError);
            }

            // Prepare arguments
            string fileName = cmdLine.FreeArguments[0];
            string targetName = cmdLine.FreeArguments[1];

            Encoding encoding = null;
            if (utf8Option.IsSet) encoding = Encoding.UTF8;
            if (utf16Option.IsSet) encoding = Encoding.Unicode;
            if (utf8NoBomOption.IsSet) encoding = new UTF8Encoding(false);
            if (defaultEncodingOption.IsSet) encoding = Encoding.Default;

            Dictionary<string, string> data = new Dictionary<string, string>();
            for (int i = 2; i < cmdLine.FreeArguments.Length; i++)
            {
                string arg = cmdLine.FreeArguments[i];
                string[] parts = arg.Split(new[] { '=' }, 2);
                if (parts.Length < 2)
                {
                    throw new ConsoleException("Invalid name value pair specification in argument " + (i + 1) + ".", ExitCodes.CmdLineError);
                }
                if (data.ContainsKey(parts[0]))
                {
                    throw new ConsoleException("Duplicate name value pair specification in argument " + (i + 1) + ".", ExitCodes.CmdLineError);
                }
                data[parts[0]] = parts[1];
            }

            // Replace data in file
            ReplaceHelper rh = new ReplaceHelper(fileName, encoding);
            rh.ReplaceData(targetName, data);
        }