public void ParsingNullString_ThrowsError()
        {
            //******** GIVEN
            var test   = new VerbDefinitionAttribute("copy", "Copy all files from source to output fodler");
            var parser = new VerbParser(Verb.FromAttribute(test).OnTargetProperty(typeof(CopyArguments)));

            //******** WHEN
            var parsed = parser.Parse("");
        }
        public void ParsingDifferentVerb_ThrowsException()
        {
            //************* GIVEN
            var test   = new VerbDefinitionAttribute("copy", "Copy all files from source to output fodler");
            var parser = new VerbParser(Verb.FromAttribute(test).OnTargetProperty(typeof(CopyArguments)));

            //************* WHEN
            parser.Parse("del");

            //************* ASSERT
        }
        public void ParsingVerbStartingWithMinus_DoesNotThrowException()
        {
            //************* GIVEN
            var test   = new VerbDefinitionAttribute("copy", "Copy all files from source to output fodler");
            var parser = new VerbParser(Verb.FromAttribute(test).OnTargetProperty(typeof(CopyArguments)));

            //************* WHEN
            var value = parser.Parse("-copy");

            //************* ASSERT
            Assert.IsNotNull(value);
        }
        public void ParsingVerbStartingWithMinus_ReturnsKeyIsValid()
        {
            //******** GIVEN
            var test   = new VerbDefinitionAttribute("copy", "Copy all files from source to output fodler");
            var parser = new VerbParser(Verb.FromAttribute(test).OnTargetProperty(typeof(CopyArguments)));

            //******** WHEN
            string errorMessage;
            var    parsed = parser.IsKeyValid("-copy", out errorMessage);

            //******** ASSERT
            Assert.IsTrue(parsed);
        }
        public void ParsingVerbWithoutArguments_ReturnsThatClass()
        {
            //******** GIVEN
            var test   = new VerbDefinitionAttribute("copy", "Copy all files from source to output fodler");
            var parser = new VerbParser(Verb.FromAttribute(test).OnTargetProperty(typeof(CopyArguments)));

            //******** WHEN
            var parsed = parser.Parse("-copy");

            //******** ASSERT
            Assert.IsNotNull(parsed);
            Assert.IsInstanceOfType(parsed, typeof(CopyArguments));
        }
        public void ParsingVerbWithMandatoryArgument_ReturnsThatFilledProperty()
        {
            //************* GIVEN
            VerbDefinitionAttribute test = typeof(CopyFilesWith1MandatoryOption).GetCustomAttributes(typeof(VerbDefinitionAttribute), true).First() as VerbDefinitionAttribute;
            var verbModel = Verb.FromAttribute(test).OnTargetProperty(typeof(CopyFilesWith1MandatoryOption));

            verbModel.AddOptionFromAttribute(new OptionDefinitionAttribute("src", "source", "source folder to use to copy files", mandatory: true), typeof(string));
            var parser = new VerbParser(verbModel);

            //************* WHEN
            var value = parser.Parse(@"-copy2 --src='C:\Temp\My Folder\TTT'");

            //************* ASSERT
            Assert.IsNotNull(value);
            Assert.IsInstanceOfType(value, typeof(CopyFilesWith1MandatoryOption));
            CopyFilesWith1MandatoryOption verb = (CopyFilesWith1MandatoryOption)value;

            Assert.AreEqual(@"'C:\Temp\My Folder\TTT'", verb.SrcFolder);
        }
Ejemplo n.º 7
0
        private void ParseVerb <T>(string arg, T parsedArguments) where T : CLIArguments
        {
            if (String.IsNullOrEmpty(arg))
            {
                throw new ArgumentNullException(nameof(arg));
            }
            if (!arg.StartsWith(Verb.VERB_IDENTIFIER))
            {
                throw new InvalidOperationException($"Invalid Verb: {arg} has to start with {Verb.VERB_IDENTIFIER}");
            }

            // get the tokens
            var tokenGenerator = new TokenGenerator(new List <string>()
            {
                Verb.VERB_IDENTIFIER, Option.OPTION_IDENTIFIER
            });
            var tokens = tokenGenerator.TokenizeThisString(arg).ToList();

            if (tokens.Count == 0)
            {
                throw new InvalidOperationException("Unable to get tokens from Verb " + arg);
            }
            // the first token must to be the verb, with value null
            if (!String.IsNullOrEmpty(tokens[0].Value))
            {
                throw new InvalidOperationException("Unable to get tokens from Verb " + arg);
            }

            // analyze the option
            string argumentKey = tokens[0].Name;

            // get information
            var verbDefinition = this._Model.Verbs.Single(x => x.Name == argumentKey);
            var p = this._Model.VerbPropertyRegistry[verbDefinition.Name];
            // parse the verb
            var parser      = new VerbParser(verbDefinition);
            var optionValue = parser.Parse(tokens);

            // set the proper value
            p.SetValue(parsedArguments, optionValue);
        }