예제 #1
0
        public void Ant()
        {
            Options options = new Options();

            options.AddOption("help", false, "print this message");
            options.AddOption("projecthelp", false, "print project help information");
            options.AddOption("version", false, "print the version information and exit");
            options.AddOption("quiet", false, "be extra quiet");
            options.AddOption("verbose", false, "be extra verbose");
            options.AddOption("debug", false, "print debug information");
            options.AddOption("logfile", true, "use given file for log");
            options.AddOption("logger", true, "the class which is to perform the logging");
            options.AddOption("listener", true, "add an instance of a class as a project listener");
            options.AddOption("buildfile", true, "use given buildfile");
            options.AddOption(OptionBuilder.New().WithDescription("use value for given property")
                              .HasArguments()
                              .WithValueSeparator()
                              .Create('D'));
            //, null, true, , false, true );
            options.AddOption("find", true, "search for buildfile towards the root of the filesystem and use it");

            String[] args = new String[] { "-buildfile", "mybuild.xml",
                                           "-Dproperty=value", "-Dproperty1=value1",
                                           "-projecthelp" };

            // use the GNU parser
            ICommandLineParser parser = new GnuParser();

            ICommandLine line = parser.Parse(options, args);

            // check multiple values
            String[] opts = line.GetOptionValues("D");
            Assert.AreEqual("property", opts[0]);
            Assert.AreEqual("value", opts[1]);
            Assert.AreEqual("property1", opts[2]);
            Assert.AreEqual("value1", opts[3]);

            // check single value
            Assert.AreEqual("mybuild.xml", line.GetOptionValue("buildfile").Value);

            // check option
            Assert.IsTrue(line.HasOption("projecthelp"));
        }
예제 #2
0
        public void Ant()
        {
            Options options = new Options();
            options.AddOption("help", false, "print this message");
            options.AddOption("projecthelp", false, "print project help information");
            options.AddOption("version", false, "print the version information and exit");
            options.AddOption("quiet", false, "be extra quiet");
            options.AddOption("verbose", false, "be extra verbose");
            options.AddOption("debug", false, "print debug information");
            options.AddOption("logfile", true, "use given file for log");
            options.AddOption("logger", true, "the class which is to perform the logging");
            options.AddOption("listener", true, "add an instance of a class as a project listener");
            options.AddOption("buildfile", true, "use given buildfile");
            options.AddOption(OptionBuilder.New().WithDescription("use value for given property")
                                            .HasArguments()
                                            .WithValueSeparator()
                                            .Create('D'));
            //, null, true, , false, true );
            options.AddOption("find", true, "search for buildfile towards the root of the filesystem and use it");

            String[] args = new String[]{ "-buildfile", "mybuild.xml",
            "-Dproperty=value", "-Dproperty1=value1",
            "-projecthelp" };

            // use the GNU parser
            ICommandLineParser parser = new GnuParser();

            ICommandLine line = parser.Parse(options, args);

            // check multiple values
            String[] opts = line.GetOptionValues("D");
            Assert.AreEqual("property", opts[0]);
            Assert.AreEqual("value", opts[1]);
            Assert.AreEqual("property1", opts[2]);
            Assert.AreEqual("value1", opts[3]);

            // check single value
            Assert.AreEqual("mybuild.xml", line.GetOptionValue("buildfile").Value);

            // check option
            Assert.IsTrue(line.HasOption("projecthelp"));
        }
예제 #3
0
        public void GetOptionProperties()
        {
            string[] args = new String[] { "-Dparam1=value1", "-Dparam2=value2", "-Dparam3", "-Dparam4=value4", "-D", "--property", "foo=bar" };

            Options options = new Options();
            options.AddOption(OptionBuilder.New().WithValueSeparator().HasOptionalArgs(2).Create('D'));
            options.AddOption(OptionBuilder.New().WithValueSeparator().HasArguments(2).WithLongName("property").Create());

            Parser parser = new GnuParser();
            ICommandLine cl = parser.Parse(options, args);

            IDictionary<string, string> props = cl.GetOptionProperties("D");

            Assert.IsNotNull(props, "null properties");
            Assert.AreEqual(4, props.Count, "number of properties in " + props);
            Assert.AreEqual("value1", props["param1"], "property 1");
            Assert.AreEqual("value2", props["param2"], "property 2");
            Assert.AreEqual("true", props["param3"], "property 3");
            Assert.AreEqual("value4", props["param4"], "property 4");

            Assert.AreEqual("bar", cl.GetOptionProperties("property")["foo"], "property with long format");
        }
예제 #4
0
        public void GetOptionProperties()
        {
            string[] args = new String[] { "-Dparam1=value1", "-Dparam2=value2", "-Dparam3", "-Dparam4=value4", "-D", "--property", "foo=bar" };

            Options options = new Options();

            options.AddOption(OptionBuilder.New().WithValueSeparator().HasOptionalArgs(2).Create('D'));
            options.AddOption(OptionBuilder.New().WithValueSeparator().HasArguments(2).WithLongName("property").Create());

            Parser       parser = new GnuParser();
            ICommandLine cl     = parser.Parse(options, args);

            IDictionary <string, string> props = cl.GetOptionProperties("D");

            Assert.IsNotNull(props, "null properties");
            Assert.AreEqual(4, props.Count, "number of properties in " + props);
            Assert.AreEqual("value1", props["param1"], "property 1");
            Assert.AreEqual("value2", props["param2"], "property 2");
            Assert.AreEqual("true", props["param3"], "property 3");
            Assert.AreEqual("value4", props["param4"], "property 4");

            Assert.AreEqual("bar", cl.GetOptionProperties("property")["foo"], "property with long format");
        }
예제 #5
0
        public static int Main(string[] args)
        {
            string netConfig = null;
            string hostArg = null, portArg = null;

            StringWriter wout = new StringWriter();
            Options options = GetOptions();

            CommandLine commandLine = null;

            bool failed = false;
            bool isService = false;

            try {
                ICommandLineParser parser = new GnuParser(options);
                commandLine = parser.Parse(args);

                netConfig = commandLine.GetOptionValue("netconfig", "./network.conf");
                hostArg = commandLine.GetOptionValue("host");
                portArg = commandLine.GetOptionValue("port");
            } catch (ParseException) {
                wout.WriteLine("Error parsing arguments.");
                failed = true;
            }

            if (commandLine != null) {
                if (commandLine.HasOption("install")) {
                    try {
                        Install(commandLine);
                        Console.Out.WriteLine("Service installed succesfully.");
                        return 0;
                    } catch (Exception e) {
                        Console.Error.WriteLine("Error installing service: " + e.Message);
            #if DEBUG
                        Console.Error.WriteLine(e.StackTrace);
            #endif
                        return 1;
                    }
                }
                if (commandLine.HasOption("uninstall")) {
                    try {
                        Uninstall();
                        Console.Out.WriteLine("Service uninstalled succesfully.");
                        return 0;
                    } catch (Exception e) {
                        Console.Error.WriteLine("Error uninstalling service: " + e.Message);
            #if DEBUG
                        Console.Error.WriteLine(e.StackTrace);
            #endif
                        return 1;
                    }
                }

                isService = commandLine.HasOption("service");
            }

            if (isService) {
                CloudBClientService clientService = new CloudBClientService(commandLine);

                try {
                    if (Environment.UserInteractive) {
                        clientService.Start(args);
                        Console.Out.WriteLine("Press any key to stop...");
                        Console.Read();
                        clientService.Stop();
                    } else {
                        ServiceBase.Run(clientService);
                    }
                } catch (Exception) {
                    return 1;
                }

                return 0;
            }

            AppDomain.CurrentDomain.UnhandledException += new UnhandledExceptionEventHandler(OnUnhandledException);
            SetEventHandlers();

            ProductInfo libInfo = ProductInfo.GetProductInfo(typeof(PathClientService));
            ProductInfo nodeInfo = ProductInfo.GetProductInfo(typeof(PathClient));

            Console.Out.WriteLine("{0} {1} ( {2} )", nodeInfo.Title, nodeInfo.Version, nodeInfo.Copyright);
            Console.Out.WriteLine(nodeInfo.Description);
            Console.Out.WriteLine();
            Console.Out.WriteLine("{0} {1} ( {2} )", libInfo.Title, libInfo.Version, libInfo.Copyright);

            // Check arguments that can be null,
            if (netConfig == null) {
                wout.WriteLine("Error, no network configuration given.");
                failed = true;
            }

            if (portArg == null) {
                wout.WriteLine("Error, no port address given.");
                failed = true;
            }

            if (!failed) {
                //TODO: support for remote (eg. HTTP, FTP, TCP/IP) configurations)

                netConfig = NormalizeFilePath(netConfig);

                if (!File.Exists(netConfig)) {
                    wout.WriteLine("Error, node configuration file not found ({0}).", netConfig);
                    failed = true;
                }
            }

            wout.Flush();

            // If failed,
            if (failed) {
                HelpFormatter formatter = new HelpFormatter();
                if (!IsConsoleRedirected()) {
                    formatter.Width = Console.WindowWidth;
                }
                formatter.CommandLineSyntax = "mnode";
                formatter.Options = options;
                formatter.PrintHelp();
                Console.Out.WriteLine();
                Console.Out.WriteLine(wout.ToString());
                return 1;
            }

            try {
            #if DEBUG
                Console.Out.WriteLine("Retrieving network configuration from {0}", netConfig);
            #endif

                // Parse the network configuration string,
                NetworkConfigSource netConfigSource;
                using (FileStream stream = new FileStream(netConfig, FileMode.Open, FileAccess.Read, FileShare.None)) {
                    netConfigSource = new NetworkConfigSource();
                    //TODO: make it configurable ...
                    netConfigSource.LoadProperties(stream);
                }

                //TODO: support also IPv6

                // The base path,
                IPAddress host = null;
                if (hostArg != null) {
                    IPAddress[] addresses = Dns.GetHostAddresses(hostArg);
                    for (int i = 0; i < addresses.Length; i++) {
                        IPAddress address = addresses[i];
                        if (address.AddressFamily == AddressFamily.InterNetwork) {
                            host = address;
                            break;
                        }
                    }
                } else {
                    host = IPAddress.Loopback;
                }

                if (host == null) {
                    Console.Out.WriteLine("Error: couldn't determine the host address.");
                    return 1;
                }

                int port;
                if (!Int32.TryParse(portArg, out port)) {
                    Console.Out.WriteLine("Error: couldn't parse port argument: " + portArg);
                    return 1;
                }

                string storage = commandLine.GetOptionValue("storage", null);

                Console.Out.WriteLine("Path Client Service, " + host + " : " + port);
                //TODO:
                service = new TcpPathClientService(null, null, null);
                service.Init();

                waitHandle = new AutoResetEvent(false);
                waitHandle.WaitOne();
            } catch (Exception e) {
                Console.Out.WriteLine(e.Message);
                Console.Out.WriteLine(e.StackTrace);
                return 1;
            } finally {
                if (service != null)
                    service.Dispose();
            }

            return 0;
        }