コード例 #1
0
ファイル: Program.cs プロジェクト: Treer/POpp
        public static int Main(string[] args)
        {
            List<TaskInfo> inputFiles = new List<TaskInfo>();
            Options options = new Options();

            for (int i = 0; i < args.Length; i++)
            {
                switch (args[i].ToLower())
                {
                    case "--help":
                    case "-h":
                    case "/h":
                    case "-?":
                    case "/?":
                        ShowUsage();
                        return (int)ErrorLevel.Success;

                    case "--version":
                        ShowVersion();
                        return (int)ErrorLevel.Success;

                    case "--nlf":
                    case "-nlf":
                    case "/nlf":
                    case "-nl":
                    case "/nl":
                        options.NewlinePreference = NewLineOptions.LF;
                        break;

                    case "--ncrlf":
                    case "-ncrlf":
                    case "/ncrlf":
                    case "-nc":
                    case "/nc":
                        options.NewlinePreference = NewLineOptions.CRLF;
                        break;

                    case "--nsource":
                    case "-nsource":
                    case "/nsource":
                    case "-ns":
                    case "/ns":
                        options.NewlinePreference = NewLineOptions.SameAsSource;
                        break;

                    case "--quiet":
                    case "-quiet":
                    case "/quiet":
                    case "-q":
                    case "/q":
                    case "--silent":
                    case "-silent":
                    case "/silent":
                        options.Quiet = true;
                        break;

                    case "--sensitive":
                    case "-sensitive":
                    case "/sensitive":
                    case "-s":
                    case "/s":
                    case "--casesensitive":
                    case "-casesensitive":
                    case "/casesensitive":
                        options.CaseSensitiveIDs = true;
                        break;

                    case "--count":
                    case "-count":
                    case "/count":
                    case "-c":
                    case "/c":
                        options.CountReferences = true;
                        break;

                    case "--includedirectory":
                    case "-includedirectory":
                    case "/includedirectory":
                    case "-i":
                    case "/i":
                        if ((i + 1) < args.Length) {
                            if (AddIncludeDirectory(args[i + 1], options)) {
                                i++;
                            } else {
                                return (int)ErrorLevel.FatalError_InvalidArgs;
                            }
                        } else {
                            ShowUsage();
                            return (int)ErrorLevel.FatalError_InvalidArgs;
                        }
                        break;

                    default:
                        if (!IsFileArgument(args[i]))
                        {
                            ShowUsage();
                            return (int)ErrorLevel.FatalError_InvalidArgs;
                        }

                        TaskInfo resInf = new TaskInfo();
                        if ((i + 1) < args.Length)
                        {
                            resInf.InputFileName = Path.GetFullPath(args[i]);
                            // move to next arg, since we assume that one holds
                            // the name of the output file
                            i++;

                            if (args[i] == "-") {
                                // specifying the last filename as '-' will override the default
                                // behaviour of using a default named output file, will use stdout instead
                                resInf.OutputFileName = String.Empty;
                            } else {
                                resInf.OutputFileName = Path.GetFullPath(args[i]);
                            }
                        }
                        else
                        {
                            resInf.InputFileName = Path.GetFullPath(args[i]);
                            resInf.OutputFileName = Path.ChangeExtension(resInf.InputFileName, "po");

                            if (resInf.InputFileName == resInf.OutputFileName) {
                                // The input file already had a .po extension! Our output file will fail to open

                                if (options.CountReferences) {
                                    // When counting references in the input file we don't need an
                                    // output file - the count result is returned as the errorLevel.
                                } else {
                                    Console.Error.WriteLine(
                                        "Error: When only a source file is provided, popp will assume a .po extension\r\n" +
                                        "       for the output file, but this input file already has a .po extension.\r\n\r\n" +
                                        "       If you want the output sent to stdout instead, then specify a hyphen (-)\r\n" +
                                        "       as the output file."
                                    );
                                    return (int)ErrorLevel.FatalError_InvalidArgs;
                                }
                            }

                        }
                        inputFiles.Add(resInf);
                        break;
                }
            }

            if (inputFiles.Count == 0)
            {
                // no files were specified, assume they want to use stdin/stdout

                bool stdInputAvailable;
                try {
                    stdInputAvailable = Console.KeyAvailable;
                } catch {
                    // Apparently Console.KeyAvailable can throw an exception if the stdin is coming from a file
                    // See https://stackoverflow.com/questions/3961542/checking-standard-input-in-c-sharp
                    stdInputAvailable = true;
                }

                if (stdInputAvailable) {

                    TaskInfo resInf = new TaskInfo();
                    resInf.InputFileName = String.Empty;
                    resInf.OutputFileName = String.Empty;
                    inputFiles.Add(resInf);

                } else {

                    ShowUsage();
                    return (int)ErrorLevel.FatalError_InvalidArgs;
                }
            }

            foreach (TaskInfo res in inputFiles)
            {
                int ret = PreprocessFile(res.InputFileName, res.OutputFileName, options);
                if (ret != 0) return ret;
            }
            return 0;
        }
コード例 #2
0
ファイル: Program.cs プロジェクト: Treer/POpp
        public static int Main(string[] args)
        {
            List <TaskInfo> inputFiles = new List <TaskInfo>();
            Options         options    = new Options();

            for (int i = 0; i < args.Length; i++)
            {
                switch (args[i].ToLower())
                {
                case "--help":
                case "-h":
                case "/h":
                case "-?":
                case "/?":
                    ShowUsage();
                    return((int)ErrorLevel.Success);

                case "--version":
                    ShowVersion();
                    return((int)ErrorLevel.Success);

                case "--nlf":
                case "-nlf":
                case "/nlf":
                case "-nl":
                case "/nl":
                    options.NewlinePreference = NewLineOptions.LF;
                    break;

                case "--ncrlf":
                case "-ncrlf":
                case "/ncrlf":
                case "-nc":
                case "/nc":
                    options.NewlinePreference = NewLineOptions.CRLF;
                    break;

                case "--nsource":
                case "-nsource":
                case "/nsource":
                case "-ns":
                case "/ns":
                    options.NewlinePreference = NewLineOptions.SameAsSource;
                    break;

                case "--quiet":
                case "-quiet":
                case "/quiet":
                case "-q":
                case "/q":
                case "--silent":
                case "-silent":
                case "/silent":
                    options.Quiet = true;
                    break;

                case "--sensitive":
                case "-sensitive":
                case "/sensitive":
                case "-s":
                case "/s":
                case "--casesensitive":
                case "-casesensitive":
                case "/casesensitive":
                    options.CaseSensitiveIDs = true;
                    break;


                case "--count":
                case "-count":
                case "/count":
                case "-c":
                case "/c":
                    options.CountReferences = true;
                    break;

                case "--includedirectory":
                case "-includedirectory":
                case "/includedirectory":
                case "-i":
                case "/i":
                    if ((i + 1) < args.Length)
                    {
                        if (AddIncludeDirectory(args[i + 1], options))
                        {
                            i++;
                        }
                        else
                        {
                            return((int)ErrorLevel.FatalError_InvalidArgs);
                        }
                    }
                    else
                    {
                        ShowUsage();
                        return((int)ErrorLevel.FatalError_InvalidArgs);
                    }
                    break;


                default:
                    if (!IsFileArgument(args[i]))
                    {
                        ShowUsage();
                        return((int)ErrorLevel.FatalError_InvalidArgs);
                    }

                    TaskInfo resInf = new TaskInfo();
                    if ((i + 1) < args.Length)
                    {
                        resInf.InputFileName = Path.GetFullPath(args[i]);
                        // move to next arg, since we assume that one holds
                        // the name of the output file
                        i++;

                        if (args[i] == "-")
                        {
                            // specifying the last filename as '-' will override the default
                            // behaviour of using a default named output file, will use stdout instead
                            resInf.OutputFileName = String.Empty;
                        }
                        else
                        {
                            resInf.OutputFileName = Path.GetFullPath(args[i]);
                        }
                    }
                    else
                    {
                        resInf.InputFileName  = Path.GetFullPath(args[i]);
                        resInf.OutputFileName = Path.ChangeExtension(resInf.InputFileName, "po");

                        if (resInf.InputFileName == resInf.OutputFileName)
                        {
                            // The input file already had a .po extension! Our output file will fail to open

                            if (options.CountReferences)
                            {
                                // When counting references in the input file we don't need an
                                // output file - the count result is returned as the errorLevel.
                            }
                            else
                            {
                                Console.Error.WriteLine(
                                    "Error: When only a source file is provided, popp will assume a .po extension\r\n" +
                                    "       for the output file, but this input file already has a .po extension.\r\n\r\n" +
                                    "       If you want the output sent to stdout instead, then specify a hyphen (-)\r\n" +
                                    "       as the output file."
                                    );
                                return((int)ErrorLevel.FatalError_InvalidArgs);
                            }
                        }
                    }
                    inputFiles.Add(resInf);
                    break;
                }
            }

            if (inputFiles.Count == 0)
            {
                // no files were specified, assume they want to use stdin/stdout

                bool stdInputAvailable;
                try {
                    stdInputAvailable = Console.KeyAvailable;
                } catch {
                    // Apparently Console.KeyAvailable can throw an exception if the stdin is coming from a file
                    // See https://stackoverflow.com/questions/3961542/checking-standard-input-in-c-sharp
                    stdInputAvailable = true;
                }

                if (stdInputAvailable)
                {
                    TaskInfo resInf = new TaskInfo();
                    resInf.InputFileName  = String.Empty;
                    resInf.OutputFileName = String.Empty;
                    inputFiles.Add(resInf);
                }
                else
                {
                    ShowUsage();
                    return((int)ErrorLevel.FatalError_InvalidArgs);
                }
            }

            foreach (TaskInfo res in inputFiles)
            {
                int ret = PreprocessFile(res.InputFileName, res.OutputFileName, options);
                if (ret != 0)
                {
                    return(ret);
                }
            }
            return(0);
        }