public void ParseArgumentsArray()
        {
            Argument[] arguments = new Argument[]
            {
                new Argument("check", bool.TrueString, "/check"),
                new Argument("uncheck", bool.FalseString, "/-uncheck"),
                new Argument("named", "value", "/named:value"),
                new Argument(string.Empty, "unnamed", "unnamed"),
                new Argument("named", "quoted value\"\"", "/named:quoted value\"\""),
                new Argument(string.Empty, "quoted value\"\"", "quoted value\"\""),
            };

            foreach (IEnumerable <Argument> permutation in arguments.Permutations())
            {
                ArgumentEnumerator enumerator = new ArgumentEnumerator(permutation.Select(p => p.Representation).ToArray());

                foreach (Argument argument in permutation)
                {
                    Assert.IsTrue(enumerator.MoveNext());
                    Assert.AreEqual(enumerator.CurrentName, argument.Name);
                    Assert.AreEqual(enumerator.CurrentValue, argument.Value);
                }

                Assert.IsFalse(enumerator.MoveNext());
            }
        }
        public void ContinueFromCurrent()
        {
            string[]           arguments   = new string[] { "/first", "/second" };
            ArgumentEnumerator enumerator1 = new ArgumentEnumerator(arguments);

            Assert.IsTrue(enumerator1.MoveNext());
            Assert.IsTrue(enumerator1.MoveNext());

            ArgumentEnumerator enumerator2 = enumerator1.ContinueFromCurrent();

            Assert.IsTrue(enumerator2.MoveNext());
            Assert.AreEqual(enumerator1.CurrentName, "second");
            Assert.AreEqual(enumerator1.CurrentName, enumerator2.CurrentName);
            Assert.AreEqual(enumerator1.CurrentValue, enumerator2.CurrentValue);
            Assert.IsFalse(enumerator2.MoveNext());
        }
Esempio n. 3
0
        public override void AssignProperty <TTarget>(ArgumentEnumerator enumerator, TTarget target, Dictionary <string, PropertyDescriptor> properties, PropertyDescriptor property, string argument)
        {
            var    followingArguments = new List <string>();
            string name;

            while ((name = CleanArgument(enumerator.Peek())) != null && !properties.ContainsKey(name) && enumerator.MoveNext())
            {
                followingArguments.Add((string)enumerator.Current);
            }

            if (followingArguments.Count > 0)
            {
                property.SetValue(target, followingArguments.ToArray());
            }
            else
            {
                RaiseError(Errors.MissingArgumentAfter, argument); // error because there are no more arguments
            }
        }
Esempio n. 4
0
        static void Main(string[] args)
        {
            // Set the default settings.
            string outputDir = "Services";
            var flags = GeneratorFlags.CompileLibrary;

            if (args.Length == 0) // If no argument is specified, display the usage.
            {
                args = new[] { "--help" };
            }

            // Run the application.
            try
            {
                // Parse arguments.
                string type = null;
                string source = null;
                var enumerator = new ArgumentEnumerator(args);
                while (enumerator.MoveNext())
                {
                    string arg = enumerator.Current.ToLower();

                    if (enumerator.IsParameter)
                    {
                        switch (arg)
                        {
                            case "--help":
                            case "-h":
                                Console.WriteLine("SYNTAX:  ServiceGenerator.exe [<arguments>] <Type> <Source>");
                                Console.WriteLine(" Types can be:");
                                Console.WriteLine("   'repository' -- Generate a whole discovery repository");
                                Console.WriteLine("   'url'        -- Generate a service of an uri");
                                Console.WriteLine("   'service'    -- Generate a named google service");
                                Console.WriteLine(" Source is:");
                                Console.WriteLine("   URI (http:// .. or file:/// ..) for 'repository'/'url'");
                                Console.WriteLine("   serviceName:version for 'service'");
                                Console.WriteLine(" Optional arguments:");
                                Console.WriteLine("   --help, -h           Displays this help screen");
                                Console.WriteLine("   --no-compile, -nc    Will not generate a .dll");
                                Console.WriteLine("   --google             Will add a Google prefix to the service");
                                Console.WriteLine("   --output, -o <dir>   Changes the output directory");
                                return;

                            case "--no-compile":
                            case "-nc":
                                flags &= ~GeneratorFlags.CompileLibrary;
                                break;

                            case "--google":
                                flags |= GeneratorFlags.GoogleService;
                                break;

                            case "--output":
                            case "-o":
                                enumerator.MoveNext();
                                outputDir = enumerator.GetParameterValue("--output");
                                break;

                            default:
                                throw new ArgumentException("Unknown argument: " + arg);
                        }
                    }
                    else
                    {
                        if (type == null)
                        {
                            type = enumerator.GetMandatory("type").ToLower();
                        }
                        else if (source == null)
                        {
                            source = enumerator.Current;
                        }
                        else
                        {
                            Console.Error.WriteLine("Unexpected argument: "+enumerator.Current);
                        }
                    }
                }

                type.ThrowIfNullOrEmpty("type");

                // Create the generator, and run it.
                Generator generator = new Generator(flags) { OutputDir = outputDir};
                switch (type)
                {
                    case "repository":
                        var apis = (source == null)
                                       ? DiscoveryRepository.RetrieveGoogleDiscovery()
                                       : DiscoveryRepository.RetrieveDiscovery(new Uri(source));
                        generator.GenerateServices(apis);
                        break;

                    case "url":
                        source.ThrowIfNull("source");
                        generator.GenerateService(new Uri(source));
                        break;

                    case "service":
                        source.ThrowIfNull("source");
                        var api =
                            (from a in DiscoveryRepository.RetrieveGoogleDiscovery()
                             where a.Id == source
                             select a).SingleOrDefault();

                        if (api == null)
                        {
                            throw new ArgumentException("The api '" + source + "' was not found in the repository.");
                        }

                        generator.GenerateService(api);
                        break;

                    default:
                        throw new ArgumentException("Unknown type: " + type);
                }
            }
            catch (Exception exception)
            {
                Console.Error.WriteLine("ERROR: " + exception.Message);
                Console.Error.WriteLine(exception.StackTrace);
                Environment.Exit(1);
            }
        }
Esempio n. 5
0
 public override void AssignProperty<TTarget>(ArgumentEnumerator enumerator, TTarget target, Dictionary<string, PropertyDescriptor> properties, PropertyDescriptor property, string argument)
 {
     string name;
     if ((name = CleanArgument(enumerator.Peek())) != null && !properties.ContainsKey(name) && enumerator.MoveNext())
         property.SetValue(target, (string)enumerator.Current);
     else
         RaiseError(Errors.MissingArgumentAfter, argument); // error because there are no more arguments
 }