1 // =================================
  2 // Copyright (c) 2022 Seppo Laakko
  3 // Distributed under the MIT license
  4 // =================================
  5 
  6 using System;
  7 using System.Collections;
  8 using System.IO;
  9 using paths;
 10 
 11 int main(int argcconst char** argv)
 12 {
 13     try
 14     {
 15         bool recursive = false;
 16         FileCopyOptions copyOptions = FileCopyOptions.none;
 17         List<string> files;
 18         string target;
 19         for (int i = 1; i < argc; ++i;)
 20         {
 21             string arg = argv[i];
 22             if (arg.StartsWith("--"))
 23             {
 24                 if (arg == "--help")
 25                 {
 26                     PrintHelp();
 27                     return 1;
 28                 }
 29                 else if (arg == "--verbose")
 30                 {
 31                     copyOptions = cast<FileCopyOptions>(copyOptions | FileCopyOptions.verbose);
 32                 }
 33                 else if (arg == "--recursive")
 34                 {
 35                     recursive = true;
 36                 }
 37                 else if (arg == "--update")
 38                 {
 39                     copyOptions = cast<FileCopyOptions>(copyOptions | FileCopyOptions.update);
 40                 }
 41                 else
 42                 {
 43                     throw Exception("unknown argument '" + arg + "'");
 44                 }
 45             }
 46             else if (arg.StartsWith("-"))
 47             {
 48                 string options = arg.Substring(1);
 49                 if (options.IsEmpty())
 50                 {
 51                     throw Exception("unknown option '" + arg + "'");
 52                 }
 53                 else
 54                 {
 55                     bool unknown = false;
 56                     string uo;
 57                     for (char o : options)
 58                     {
 59                         switch (o)
 60                         {
 61                             case 'h':
 62                             {
 63                                 PrintHelp();
 64                                 return 1;
 65                             }
 66                             case 'v':
 67                             {
 68                                 copyOptions = cast<FileCopyOptions>(copyOptions | FileCopyOptions.verbose);
 69                                 break;
 70                             }
 71                             case 'r':
 72                             {
 73                                 recursive = true;
 74                                 break;
 75                             }
 76                             case 'u':
 77                             {
 78                                 copyOptions = cast<FileCopyOptions>(copyOptions | FileCopyOptions.update);
 79                                 break;
 80                             }
 81                             default:
 82                             {
 83                                 unknown = true;
 84                                 uo.Append(o);
 85                                 break;
 86                             }
 87                         }
 88                         if (unknown)
 89                         {
 90                             throw Exception("unknown option '-" + uo + "'");
 91                         }
 92                     }
 93                 }
 94             }
 95             else
 96             {
 97                 files.Add(arg);
 98             }
 99         }
100         if (files.IsEmpty())
101         {
102             throw Exception("no files specified");
103         }
104         else if (files.Count() == 1)
105         {
106             throw Exception("target not specified");
107         }
108         else
109         {
110             target = files.Back();
111             files.RemoveLast();
112         }
113         if (!HasEnv("SHELL"))
114         {
115             files = Expand(files);
116         }
117         Copy(filestargetrecursivecopyOptions);
118     }
119     catch (const Exception& ex)
120     {
121         Console.Error() << ex.ToString() << endl();
122         return 1;
123     }
124     return 0;
125 }