Ejemplo n.º 1
0
        public void TestParseWithStringArg()
        {
            VerbToken token1 = new VerbToken(new Name("verb1", "a"));

            ArgumentToken token2 = TokenUtils.BuildArgString("arg1");

            CommandRoot command1 = new CommandRoot.Builder()
                                   .Id(0)
                                   .Description("command")
                                   .WithToken(token1)
                                   .WithUsage(new CommandUsage.Builder()
                                              .Description("usage1")
                                              .WithToken(token2)
                                              .Build())
                                   .Build();

            CommandLibrary library = new CommandLibrary();

            library.AddCommand(command1);

            CommandUsageMatchData matchData = library.Parse("verb1 \"the entire string\"");

            Assert.True(matchData.IsSuccessful);
            Assert.True(matchData.HasToken(token1));
            Assert.True(matchData.HasToken(token2));

            bool hasArg1Val = matchData.TryGetArgValue(token2, out string arg1Val);

            Assert.True(hasArg1Val);
            Assert.Equal("the entire string", arg1Val);
        }
Ejemplo n.º 2
0
        public void TestParse()
        {
            VerbToken token1 = new VerbToken(new Name("verb1", "a"));

            ArgumentToken token2 = new ArgumentToken <int> .Builder().Name("arg").Parser(int.TryParse).IsOptional(false).Build();

            CommandRoot command1 = new CommandRoot.Builder()
                                   .Id(0)
                                   .Description("command")
                                   .WithToken(token1)
                                   .WithUsage(new CommandUsage.Builder()
                                              .Description("usage1")
                                              .WithToken(token2)
                                              .Build())
                                   .Build();

            CommandLibrary library = new CommandLibrary();

            library.AddCommand(command1);

            CommandUsageMatchData matchData = library.Parse("verb1 123");

            Assert.True(matchData.IsSuccessful);
            Assert.True(matchData.HasToken(token1));
            Assert.True(matchData.HasToken(token2));

            bool hasArgVal = matchData.TryGetArgValue(token2, out int argVal);

            Assert.True(hasArgVal);
            Assert.Equal(123, argVal);
        }
Ejemplo n.º 3
0
        private ICommandAction BuildDetailAccountCommand(string input, CommandUsageMatchData matchData)
        {
            DetailAccountCommand cmd = new DetailAccountCommand(input, Repositories);

            string name;

            if (matchData.TryGetArgValue(BudgetCliCommands.ARG_ACCOUNT_NAME, out name))
            {
                cmd.NameOption.SetData(name);
            }

            DateTime date;

            if (matchData.TryGetArgValue(BudgetCliCommands.OPT_DATE.Arguments[0], out date))
            {
                cmd.DateOption.SetData(date);
            }

            return(cmd);
        }
Ejemplo n.º 4
0
        private ICommandAction BuildRemoveAccountCommand(string input, CommandUsageMatchData matchData)
        {
            DeleteAccountCommand cmd = new DeleteAccountCommand(input, Repositories);

            string accountName;

            if (!matchData.TryGetArgValue(BudgetCliCommands.ARG_ACCOUNT_NAME, out accountName))
            {
                return(null);
            }

            cmd.AccountName.SetData(accountName);

            if (matchData.HasToken(BudgetCliCommands.OPT_RECURSIVE))
            {
                cmd.IsRecursiveOption.SetData(true);
            }

            return(cmd);
        }
Ejemplo n.º 5
0
        private ICommandAction BuildNewAccountCommand(string input, CommandUsageMatchData matchData)
        {
            string accountName;

            if (!matchData.TryGetArgValue(BudgetCliCommands.ARG_ACCOUNT_NAME, out accountName))
            {
                return(null);
            }

            AddAccountCommand cmd = new AddAccountCommand(input, Repositories, accountName);

            string categoryName;

            if (matchData.TryGetArgValue(BudgetCliCommands.OPT_CATEGORY.Arguments[0], out categoryName))
            {
                cmd.CategoryNameOption.SetData(categoryName);
            }

            string description;

            if (matchData.TryGetArgValue(BudgetCliCommands.OPT_DESCRIPTION.Arguments[0], out description))
            {
                cmd.DescriptionOption.SetData(description);
            }

            Money funds;

            if (matchData.TryGetArgValue(BudgetCliCommands.OPT_FUNDS.Arguments[0], out funds))
            {
                cmd.FundsOption.SetData(funds);
            }

            AccountKind accountKind;

            if (matchData.TryGetArgValue(BudgetCliCommands.OPT_ACCOUNT_TYPE.Arguments[0], out accountKind))
            {
                cmd.AccountTypeOption.SetData(accountKind);
            }

            int priority;

            if (matchData.TryGetArgValue(BudgetCliCommands.OPT_PRIORITY.Arguments[0], out priority))
            {
                cmd.PriorityOption.SetData(priority);
            }

            return(cmd);
        }
Ejemplo n.º 6
0
        private ICommandAction BuildListAccountCommand(string input, CommandUsageMatchData matchData)
        {
            ListAccountCommand cmd = new ListAccountCommand(input, Repositories);

            AccountKind accountType;

            if (matchData.TryGetArgValue(BudgetCliCommands.OPT_ACCOUNT_TYPE.Arguments[0], out accountType))
            {
                cmd.AccountTypeOption.SetData(accountType);
            }

            string categoryName;

            if (matchData.TryGetArgValue(BudgetCliCommands.OPT_CATEGORY.Arguments[0], out categoryName))
            {
                cmd.CategoryIdOption.SetData(Repositories.AccountRepository.GetIdByName(categoryName));
            }

            string description;

            if (matchData.TryGetArgValue(BudgetCliCommands.OPT_DESCRIPTION.Arguments[0], out description))
            {
                cmd.DescriptionOption.SetData(description);
            }

            string name;

            if (matchData.TryGetArgValue(BudgetCliCommands.OPT_ACCOUNT_NAME.Arguments[0], out name))
            {
                cmd.NameOption.SetData(name);
            }

            Range <Money> fundsRange;

            if (matchData.TryGetArgValue(BudgetCliCommands.OPT_FUNDS_RANGE.Arguments[0], out fundsRange))
            {
                cmd.FundsOption.SetData(fundsRange);
            }

            Range <long> priorityRange;

            if (matchData.TryGetArgValue(BudgetCliCommands.OPT_PRIORITY_RANGE.Arguments[0], out priorityRange))
            {
                cmd.PriorityOption.SetData(priorityRange);
            }

            return(cmd);
        }