[r7]: / trunk / cforge / cforge / Program.cs  Maximize  Restore  History

Download this file

157 lines (126 with data), 6.0 kB

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;

namespace cforge
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("");
            Console.WriteLine(@"----------------------------------------------------------------------------");
            Console.WriteLine("cforge started");
            Console.WriteLine(@"----------------------------------------------------------------------------");
            Console.WriteLine("");


            if (args.Length == 1 && args[0].StartsWith("cforge:"))
            {
                // invokation via URL Handler:
                // format is
                // "cforge:<command>:additionalargs_or_path_or_whatever
                // now we can remove the start
                // split for the first ":" and the rest is like the normal commandline                
                String arg = args[0].Remove(0, "cforge:".Length);
                List<string> newargs = new List<string>();

                String command = arg.Split(':')[0];
                newargs.Add(command);
                string s = System.Uri.UnescapeDataString(arg.Remove(0, command.Length + 1));
                s = System.Uri.UnescapeDataString(s);
                newargs.Add(s);

                args = newargs.ToArray();
            }
            

            if (!Helper.CheckRegistryProtocol())
            {
                Console.WriteLine(@"[WARNING] URL Handler is not registered. CFORGE: links will not work properly. Use RegisterURLHandler.bat to do this (at <CODESYS Installation folder>\CFORGE\ )");
            }

            List<String> liScripts = Helper.GetAllScripts();
            
            bool bDebug = false;

            for (int i = 0; i < args.Length; i++)
            {
                switch (args[i])
                {
                    case "-d":
                        bDebug = true;
                        if (Helper.IsUserElevated())
                            Console.WriteLine("Running elevated");
                        break;

                    // fallthrough intended
                    case "-v":
                    case "--version":
                        
                        Version version = Assembly.GetEntryAssembly().GetName().Version;
                        Console.WriteLine("Version: " + version.ToString());
                        break;

                    case "--setup":
                        if (!Helper.IsUserElevated())
                            Helper.RunElevated("--setup");
                        else
                        {
                            Helper.RegisterProtocol(bDebug);
                            Helper.AddToSystemPath(bDebug);
                        }
                        break;
                    

                    case "--license-info":
                        Helper.ShowLicenseInfo();
                        break;

                    
                    
                    /* fallthrough intended ! */
                    case "--help":
                    case "-h":
                        ShowUsage();
                        break;

                    default:
                        // check if we have matching script:
                        if (args[i].StartsWith("--"))
                        {
                            String command = args[i].Replace("--", "");
                            if (liScripts.Contains(command))
                            {
                                // the rest of the command is passed as argument to the script

                                string[] newargs = args.Skip(i + 1).ToArray();
                                
                                Helper.ExecuteIPyScript(command, newargs, bDebug);
                                // as we "used" all params...
                                i = args.Length;
                            }
                            else
                            {
                                ShowUsage();
                            }
                        }
                        break;
                }
            }

            if (args.Length == 0)
                ShowUsage();
            
            Console.WriteLine("");
            Console.WriteLine(@"----------------------------------------------------------------------------");
            Console.WriteLine("cforge finished.");
            Console.WriteLine(@"----------------------------------------------------------------------------");
            Console.WriteLine("");

            //Console.ReadLine();
        }

        static void ShowUsage()
        {
            // pad right 32 for description ;-)
            Console.WriteLine(@"usage: cforge <command> <arguments>");
            Console.WriteLine(@"");
            Console.WriteLine(@"internal commands:");
            Console.WriteLine(@"-h/--help                       show this help");
            Console.WriteLine(@"--setup                         register cforge url handler and add cforge to path");
            Console.WriteLine(@"--license-info                  show license information");
            Console.WriteLine(@"-v/--version                    show version information");
            Console.WriteLine(@"");
            
            Console.WriteLine(@"");
            Console.WriteLine(@"external commands (scripts):");
            Console.WriteLine(@"------------------------------");

            // generic part
            
            List<String> liCommands = Helper.GetAllScripts();
            foreach (String  command in liCommands)
            {
                Helper.ShowUsageIPyScript(command, false);
            }
            Console.WriteLine(@"");
            Console.WriteLine(@"");

        }

    }
}