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); }
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); }
public void ParseNewAccount(string newAccountStr) { string fullInput = newAccountStr + " \"account name\""; CommandLibrary library = BudgetCliCommands.BuildCommandLibrary(); CommandUsageMatchData matchData = library.Parse(fullInput); Assert.True(matchData.IsSuccessful); }
public bool TryParseCommand(string input, out ICommandAction action) { CommandUsageMatchData match = Commands.Parse(input); if (match == null) { action = null; return(false); } action = BuildAction(input, match); return(true); }
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); }
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); }
private ICommandAction BuildAction(string input, CommandUsageMatchData matchData) { if (matchData.Usage.IsHelp) { return(new HelpCommand(input, Repositories, matchData.Command)); } if (matchData.Command == BudgetCliCommands.CMD_CLEAR) { return(new SystemCommand(input, CommandKind.ClearConsole)); } if (matchData.Command == BudgetCliCommands.CMD_EXIT) { return(new SystemCommand(input, CommandKind.Exit)); } if (matchData.Command == BudgetCliCommands.CMD_HELP) { return(new SystemCommand(input, CommandKind.Help)); } if (matchData.Command == BudgetCliCommands.CMD_VERSION) { return(new SystemCommand(input, CommandKind.Version)); } if (matchData.Command == BudgetCliCommands.CMD_DETAIL_ACCOUNTS) { return(BuildDetailAccountCommand(input, matchData)); } if (matchData.Command == BudgetCliCommands.CMD_LS_ACCOUNTS) { return(BuildListAccountCommand(input, matchData)); } if (matchData.Command == BudgetCliCommands.CMD_NEW_ACCOUNT) { return(BuildNewAccountCommand(input, matchData)); } if (matchData.Command == BudgetCliCommands.CMD_REMOVE_ACCOUNTS) { return(BuildRemoveAccountCommand(input, matchData)); } if (matchData.Command == BudgetCliCommands.CMD_SET_ACCOUNTS) { return(BuildSetAccountCommand(input, matchData)); } return(null); }
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); }
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); }
public CommandUsageMatchData Parse(string text) { CommandUsageMatchData bestMatchData = null; float bestMatchQuality = float.NegativeInfinity; foreach (var command in _commands) { Dictionary <ICommandUsage, TokenMatchCollection> matchDict = CommandParser.GetUsageMatchResults(command, text); foreach (var kvp in matchDict) { ICommandUsage usage = kvp.Key; TokenMatchCollection matchCollection = kvp.Value; if (matchCollection.IsFullMatch && matchCollection.MatchQuality > bestMatchQuality) { bestMatchQuality = matchCollection.MatchQuality; bestMatchData = new CommandUsageMatchData(command, usage, matchCollection); } } } return(bestMatchData); }
private ICommandAction BuildSetAccountCommand(string input, CommandUsageMatchData matchData) { throw new NotImplementedException(); }