コード例 #1
0
        static private bool GetParameters(string[] args)
        {
            Parameters = new StructParameters();
            if (args.Length == 0)
            {
                Usage();
                return(false);
            }
            Parameters.FixEndOfLine = false;
            Parameters.FixTab       = false;
            Parameters.OverWrite    = false;
            Parameters.InPlace      = false;

            foreach (var a in args)
            {
                string arg = a.Trim();
                if (arg == "" || arg == "?" || arg == "-?" || arg == "/?" || arg.Equals("--help", StringComparison.InvariantCultureIgnoreCase))
                {
                    Usage();
                    return(false);
                }
                if (arg.Length == 2 && (arg.Substring(0, 1) == "/" || arg.Substring(0, 1) == "-"))
                {
                    switch (arg.Substring(1, 1).ToUpper())
                    {
                    case "F":
                        Parameters.OverWrite = true;
                        break;

                    case "I":
                        Parameters.InPlace = true;
                        break;

                    case "E":
                        Parameters.FixEndOfLine = true;
                        break;

                    case "T":
                        Parameters.FixTab = true;
                        break;

                    default:
                        Console.WriteLine($"Unknown switch: {a}\r\n");
                        return(false);
                    }
                    continue;
                }
                if (Parameters.FileSource != null && Parameters.FileDestination != null)
                {
                    Console.WriteLine("To many files listed\r\n");
                    return(false);
                }
                if (Parameters.FileSource != null && Parameters.FileDestination == null)
                {
                    Parameters.FileDestination = arg;
                }
                if (Parameters.FileSource == null)
                {
                    Parameters.FileSource = arg;
                }
            }
            if (string.IsNullOrEmpty(Parameters.FileSource))
            {
                Console.WriteLine("Source file not specified\r\n");
                return(false);
            }
            if (!string.IsNullOrEmpty(Parameters.FileDestination) && Parameters.InPlace)
            {
                Console.WriteLine("When -i is used, do not specify a destination file\r\n");
                return(false);
            }
            if (string.IsNullOrEmpty(Parameters.FileDestination) && !Parameters.InPlace)
            {
                Console.WriteLine("Destination file was not specified");
                return(false);
            }
            if (!File.Exists(Parameters.FileSource))
            {
                Console.WriteLine($"The source file {Parameters.FileSource} does not exist.");
                return(false);
            }
            if (Parameters.InPlace)
            {
                return(true);
            }
            if (File.Exists(Parameters.FileDestination) && !Parameters.OverWrite)
            {
                Console.WriteLine("The destination file {Parameters.FileDestination} exists and -f is not specified.");
                return(false);
            }
            return(true);
        }
コード例 #2
0
        static private bool GetParameters(string[] args)
        {
            Parameters = new StructParameters();
            if (args.Length == 0)
            {
                Usage();
                return(false);
            }
            Parameters.FixEndOfLine = false;
            Parameters.FixTab       = false;
            Parameters.OverWrite    = false;
            Parameters.InPlace      = false;
            Parameters.Verbose      = false;

            // Walk through each argument, assigning the values to the Parameters Structure
            // If the files is a switch, assign, then continue.
            // Otherwise the args are in order. First is source, second (if any) is destination.
            foreach (var a in args)
            {
                string arg = a.Trim();
                if (arg == "" || arg == "?" || arg == "-?" || arg == "/?" || arg.Equals("--help", StringComparison.InvariantCultureIgnoreCase))
                {
                    Usage();
                    return(false);
                }
                if (arg.Length == 2 && (arg.Substring(0, 1) == "/" || arg.Substring(0, 1) == "-"))
                {
                    switch (arg.Substring(1, 1).ToUpper())
                    {
                    case "F":
                        Parameters.OverWrite = true;
                        break;

                    case "I":
                        Parameters.InPlace = true;
                        break;

                    case "E":
                        Parameters.FixEndOfLine = true;
                        break;

                    case "T":
                        Parameters.FixTab = true;
                        break;

                    case "V":
                        Parameters.Verbose = true;
                        break;

                    default:
                        Console.WriteLine($"Unknown switch: {a}\r\n");
                        return(false);
                    }
                    continue;
                }
                if (Parameters.FileSource != null && Parameters.FileDestination != null)
                {
                    Console.WriteLine("To many files listed\r\n");
                    return(false);
                }
                if (Parameters.FileSource != null && Parameters.FileDestination == null)
                {
                    Parameters.FileDestination = arg;
                }
                if (Parameters.FileSource == null)
                {
                    Parameters.FileSource = arg;
                }
            }
            if (string.IsNullOrEmpty(Parameters.FileSource))
            {
                Console.WriteLine("Source file not specified\r\n");
                return(false);
            }
            if (!string.IsNullOrEmpty(Parameters.FileDestination) && Parameters.InPlace)
            {
                Console.WriteLine("When -I is used, do not specify a destination file\r\n");
                return(false);
            }
            if (string.IsNullOrEmpty(Parameters.FileDestination) && !Parameters.InPlace)
            {
                Console.WriteLine("Destination file was not specified");
                return(false);
            }
            if (!File.Exists(Parameters.FileSource))
            {
                Console.WriteLine($"The source file {Parameters.FileSource} does not exist.");
                return(false);
            }
            if (Parameters.InPlace)
            {
                Parameters.FileDestination = Path.GetTempFileName();
                return(true);
            }
            if (File.Exists(Parameters.FileDestination) && !Parameters.OverWrite)
            {
                Console.WriteLine("The destination file {Parameters.FileDestination} exists and -F is not specified.");
                return(false);
            }
            return(true);
        }