Diff of /trunk/cforge/cforge/Program.cs [000000] .. [r3]  Maximize  Restore

Switch to unified view

a b/trunk/cforge/cforge/Program.cs
1
using System;
2
using System.Collections.Generic;
3
using System.Linq;
4
using System.Text;
5
using System.Threading.Tasks;
6
7
namespace cforge
8
{
9
    class Program
10
    {
11
        static void Main(string[] args)
12
        {
13
            Console.WriteLine("cforge\r\n");
14
15
            if (args.Length == 1 && args[0].StartsWith("cforge:"))
16
            {
17
                // invokation via URL Handler:
18
                // format is
19
                // "cforge:<command>:additionalargs_or_path_or_whatever
20
                // now we can remove the start
21
                // split for the first ":" and the rest is like the normal commandline                
22
                String arg = args[0].Remove(0, "cforge:".Length);
23
                List<string> newargs = new List<string>();
24
25
                String command = arg.Split(':')[0];
26
                newargs.Add(command);
27
                string s = System.Uri.UnescapeDataString(arg.Remove(0, command.Length + 1));
28
                s = System.Uri.UnescapeDataString(s);
29
                newargs.Add(s);
30
                
31
                args = newargs.ToArray();
32
            }
33
34
            if(!Helper.CheckRegistryProtocol())
35
            {
36
                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\ )");
37
            }
38
39
            List<String> liScripts = Helper.GetAllScripts();
40
41
            for (int i = 0; i < args.Length; i++)
42
            {
43
                switch (args[i])
44
                {
45
                    case "--register-url-handler":
46
                        Helper.RegisterProtocol();
47
                        break;
48
49
                    case "--list-scripts":
50
51
                        Console.WriteLine("List all scripts: ");
52
                        foreach (String item in liScripts)
53
                        {
54
                            Console.WriteLine(" -" + item);
55
                        }
56
                        break;
57
            
58
                    /* fallthrough intended ! */
59
                    case "--help":
60
                    case "-h":
61
                        ShowUsage();
62
                        break;
63
64
                    default:
65
                        // check if we have matching script:
66
                        if(liScripts.Contains(args[i]))
67
                        {
68
                            // the rest of the command is passed as argument to the script
69
                            string[] newargs = new List<string>(args).GetRange(i, args.Length - i - 1).ToArray();
70
                            Helper.ExecuteIPyScript(args[i], newargs);
71
                        }
72
                        break;
73
                }
74
            }
75
        }
76
77
        static void ShowUsage()
78
        {
79
            Console.WriteLine(@"usage: cforge <command> [options]");
80
            Console.WriteLine(@"");
81
            Console.WriteLine(@"commands:");
82
            Console.WriteLine(@"-h/--help: show this help");
83
            Console.WriteLine(@"--register-url-handler      register cforge url handler in windows registry");
84
            Console.WriteLine(@"--list-scripts              list all available scripts (ironpython subcommands)");
85
86
            // generic part
87
88
            Console.WriteLine(@"");
89
90
        }
91
92
    }
93
}