示例#1
0
        public static int Main(string[] args)
        {
            Options options = new Options();

            using (Parser cmdParser = new Parser())
            {
                cmdParser.ParseArguments(args, options);
            }

            List <string> files = Directory.EnumerateFiles(Environment.CurrentDirectory, options.Input, SearchOption.TopDirectoryOnly).ToList();

            if (files.Count == 0)
            {
                Console.Error.WriteLine("File {0} not found", options.Input);

                return(-1);
            }

            StreamWriter streamWriter;
            IParser      parser;

            int result = AssertConditions(options, out streamWriter, out parser);

            if (result != 0)
            {
                string usage = options.GetUsage();

                Console.WriteLine(usage);

                return(result);
            }

            using (CSVWriter csvWriter = new CSVWriter(',', streamWriter))
            {
                foreach (string file in files)
                {
                    using (StreamReader reader = new StreamReader(file))
                    {
                        if (options.Header)
                        {
                            string[] header = parser.GetHeader();

                            csvWriter.Append(header);
                        }

                        IEnumerable <string[]> rows = parser.Read(reader);

                        foreach (string[] row in rows)
                        {
                            csvWriter.Append(row);
                        }
                    }
                }
            }

            return(0);
        }
示例#2
0
        public static int Main(string[] args)
        {
            Options options = new Options();

            using (Parser cmdParser = new Parser())
            {
                cmdParser.ParseArguments(args, options);
            }

            List<string> files = Directory.EnumerateFiles(Environment.CurrentDirectory, options.Input, SearchOption.TopDirectoryOnly).ToList();

            if (files.Count == 0)
            {
                Console.Error.WriteLine("File {0} not found", options.Input);

                return -1;
            }

            StreamWriter streamWriter;
            IParser parser;

            int result = AssertConditions(options, out streamWriter, out parser);

            if (result != 0)
            {
                string usage = options.GetUsage();

                Console.WriteLine(usage);

                return result;
            }

            using (CSVWriter csvWriter = new CSVWriter(',', streamWriter))
            {
                foreach(string file in files)
                {
                    using (StreamReader reader = new StreamReader(file))
                    {
                        if (options.Header)
                        {
                            string[] header = parser.GetHeader();

                            csvWriter.Append(header);
                        }

                        IEnumerable<string[]> rows = parser.Read(reader);

                        foreach (string[] row in rows)
                        {
                            csvWriter.Append(row);
                        }
                    }
                }
            }

            return 0;
        }