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 verbose = false;
 16         List<string> files;
 17         string target;
 18         for (int i = 1; i < argc; ++i;)
 19         {
 20             string arg = argv[i];
 21             if (arg.StartsWith("--"))
 22             {
 23                 if (arg == "--help")
 24                 {
 25                     PrintHelp();
 26                     return 1;
 27                 }
 28                 else if (arg == "--verbose")
 29                 {
 30                     verbose = true;
 31                 }
 32                 else
 33                 {
 34                     throw Exception("unknown argument '" + arg + "'");
 35                 }
 36             }
 37             else if (arg.StartsWith("-"))
 38             {
 39                 string options = arg.Substring(1);
 40                 if (options.IsEmpty())
 41                 {
 42                     throw Exception("unknown option '" + arg + "'");
 43                 }
 44                 else
 45                 {
 46                     bool unknown = false;
 47                     string uo;
 48                     for (char o : options)
 49                     {
 50                         switch (o)
 51                         {
 52                             case 'h':
 53                             {
 54                                 PrintHelp();
 55                                 return 1;
 56                             }
 57                             case 'v':
 58                             {
 59                                 verbose = true;
 60                                 break;
 61                             }
 62                             default:
 63                             {
 64                                 unknown = true;
 65                                 uo.Append(o);
 66                                 break;
 67                             }
 68                         }
 69                         if (unknown)
 70                         {
 71                             throw Exception("unknown option '-" + uo + "'");
 72                         }
 73                     }
 74                 }
 75             }
 76             else
 77             {
 78                 files.Add(arg);
 79             }
 80         }
 81         if (files.IsEmpty())
 82         {
 83             throw Exception("no files specified");
 84         }
 85         else if (files.Count() == 1)
 86         {
 87             throw Exception("target not specified");
 88         }
 89         else
 90         {
 91             target = files.Back();
 92             files.RemoveLast();
 93         }
 94         if (!HasEnv("SHELL"))
 95         {
 96             files = Expand(files);
 97         }
 98         Move(filestargetverbose);
 99     }
100     catch (const Exception& ex)
101     {
102         Console.Error() << ex.ToString() << endl();
103         return 1;
104     }
105     return 0;
106 }