using System; using System.IO; using System.Linq; using System.Text; namespace Tee { class Tee { static void Main(string[] args) { try { string[] BLANK_MODE = new string[] { "-B", "/B" }; string[] APPEND_MODE = new string[] { "-A", "/A" }; string[] HELP_MODE = new string[] { "-H", "/H", "-?", "/?", "--HELP" }; if (args.Any(item => HELP_MODE.Any(item2 => item2 == item.ToUpper()))) { // ヘルプの表示 Console.WriteLine("標準入力から読んだ内容を標準出力とファイルに出力します。"); Console.WriteLine(""); Console.WriteLine("tee [-ab] [file ...]"); Console.WriteLine(" -a ファイルを上書きせず末尾に追加します。"); Console.WriteLine(" -b 標準出力に出力せずファイルにのみ出力します。"); return; } // 標準入力を読み込む string contents = Console.In.ReadToEnd(); // 標準出力に書き出す if (!args.Any(item => BLANK_MODE.Any(item2 => item2 == item.ToUpper()))) { Console.Out.Write(contents); } // ファイルに書き出す foreach (var filename in args.Where(item => !item.StartsWith("/") && !item.StartsWith("-"))) { if (args.Any(item => APPEND_MODE.Any(item2 => item2 == item.ToUpper()))) { // 追加 File.AppendAllText(filename, contents, Encoding.GetEncoding("Shift-JIS")); } else { // 上書き File.WriteAllText(filename, contents, Encoding.GetEncoding("Shift-JIS")); } } } catch (Exception ex) { Console.Error.WriteLine(ex.Message); } } } }