Skip to content

Commit 002106a

Browse files
committed
Merge pull request #6 from Pyratron/command-data
Command data
2 parents ab2e724 + 0c9f753 commit 002106a

File tree

16 files changed

+158
-80
lines changed

16 files changed

+158
-80
lines changed

DotNET/.nuget/packages.config

Lines changed: 0 additions & 4 deletions
This file was deleted.

DotNET/Command Parser.sln

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,12 @@
11

22
Microsoft Visual Studio Solution File, Format Version 12.00
3-
# Visual Studio 2013
4-
VisualStudioVersion = 12.0.31101.0
3+
# Visual Studio 14
4+
VisualStudioVersion = 14.0.23107.0
55
MinimumVisualStudioVersion = 10.0.40219.1
66
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Parser", "Parser\Parser.csproj", "{78DE8E68-61D9-4FB5-B25B-1EC94F96E1FD}"
77
EndProject
88
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Demo", "Demo\Demo.csproj", "{CEF53167-06D5-460D-927C-F5BDF8D08E9B}"
99
EndProject
10-
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = ".nuget", ".nuget", "{2ACB48F6-5E57-4066-A63D-F7E27A42C946}"
11-
ProjectSection(SolutionItems) = preProject
12-
.nuget\packages.config = .nuget\packages.config
13-
EndProjectSection
14-
EndProject
1510
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Tests", "Tests\Tests.csproj", "{C7AE3472-7138-4FF2-8161-53C7DCD255D4}"
1611
EndProject
1712
Global

DotNET/Demo/Program.cs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ public static void Main()
8888

8989
Parser.AddCommand(Command.Create("Register").AddAlias("register").SetDescription("Create an account")
9090
//Note inline action.
91-
.SetAction(arguments =>
91+
.SetAction((arguments, data) =>
9292
{
9393
var user = arguments.FromName("username");
9494
var email = arguments.FromName("email");
@@ -121,7 +121,7 @@ public static void Main()
121121
.AddAlias("god", "godmode")
122122
.SetDescription("Disables or enables godmode.")
123123
.SetAction(
124-
delegate(Argument[] arguments)
124+
(arguments, data) =>
125125
{
126126
Console.WriteLine("Godmode turned {0} for {1}", arguments.FromName("status"),
127127
arguments.FromName("player"));
@@ -153,7 +153,7 @@ public static void Main()
153153
.AddAlias("worth")
154154
.SetDescription("Item worth")
155155
.SetAction(
156-
delegate(Argument[] arguments)
156+
(arguments, data) =>
157157
{
158158
var type = arguments.FromName("type");
159159
if (type == "hand")
@@ -206,7 +206,7 @@ public static void Main()
206206
}
207207
}
208208

209-
private static void OnMailExecuted(Argument[] args)
209+
private static void OnMailExecuted(Argument[] args, object data)
210210
{
211211
var type = args.FromName("type");
212212
switch (type)
@@ -229,22 +229,22 @@ private static void OnMailExecuted(Argument[] args)
229229
}
230230
}
231231

232-
private static void OnGiveExecuted(Argument[] args)
232+
private static void OnGiveExecuted(Argument[] args, object data)
233233
{
234234
var user = args.FromName("user");
235235
var item = args.FromName("item");
236236
var amount = args.FromName("amount");
237237
Console.WriteLine("User {0} was given {1} of {2}", user, amount, item);
238238
}
239239

240-
private static void OnUnbanExecuted(Argument[] args)
240+
private static void OnUnbanExecuted(Argument[] args, object data)
241241
{
242242
var user = args.FromName("user");
243243
Console.WriteLine("User {0} was unbanned!", user);
244244
banned = false;
245245
}
246246

247-
private static void OnBanExecuted(Argument[] args)
247+
private static void OnBanExecuted(Argument[] args, object data)
248248
{
249249
var user = args.FromName("user");
250250
Console.WriteLine("User {0} was banned!", user);

DotNET/Demo/Properties/AssemblyInfo.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,11 @@
55
[assembly: AssemblyDescription("Demo application to showcase the Pyratron Command Parser Framework")]
66
[assembly: AssemblyCompany("Pyratron Studios")]
77
[assembly: AssemblyProduct("Command Parser Demo")]
8-
[assembly: AssemblyCopyright("Copyright © 2015")]
8+
[assembly: AssemblyCopyright("Copyright © Pyratron Studios 2015")]
99

1010
[assembly: ComVisible(false)]
1111

1212
[assembly: Guid("f737eed5-4675-406a-ad8d-dd61da202bbd")]
1313

14-
[assembly: AssemblyVersion("1.2")]
15-
[assembly: AssemblyFileVersion("1.2")]
14+
[assembly: AssemblyVersion("1.3")]
15+
[assembly: AssemblyFileVersion("1.3")]

DotNET/Parser/Command.cs

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ public class Command : IArguable
1919
/// <summary>
2020
/// An action to be executed when the command is ran with successful input.
2121
/// </summary>
22-
public Action<Argument[]> Action { get; set; }
22+
public Action<Argument[], object> Action { get; set; }
2323

2424
/// <summary>
2525
/// The strings that will call the command. The first alias is known as the default alias.
@@ -229,9 +229,9 @@ public Command RestrictAccess(int accessLevel)
229229
/// </summary>
230230
/// <param name="action">
231231
/// Action to be ran, which takes a <c>Argument</c> array parameter representing the passed
232-
/// input.
232+
/// input, as well as any kind of optional data to be sent to the command.
233233
/// </param>
234-
public Command SetAction(Action<Argument[]> action)
234+
public Command SetAction(Action<Argument[], object> action)
235235
{
236236
if (action == null) throw new ArgumentNullException("action");
237237

@@ -240,18 +240,19 @@ public Command SetAction(Action<Argument[]> action)
240240
}
241241

242242
/// <summary>
243-
/// Executes a command with the specified input and an optional access level.
243+
/// Executes this command with the specified arguments.
244244
/// If CanExecute returns false, the command is not run.
245245
/// </summary>
246246
/// <param name="arguments">The parsed input</param>
247-
public Command Execute(Argument[] arguments)
247+
/// <param name="data">Optional data to be passed to the command.</param>
248+
public Command Execute(Argument[] arguments, object data = null)
248249
{
249250
if (Action == null)
250251
throw new InvalidOperationException("The command's action must be defined before calling it.");
251252

252253
if (string.IsNullOrEmpty(CanExecute(this)))
253254
//Run the pre-condition, if it passes (returns no error), run the action
254-
Action(arguments);
255+
Action(arguments, data);
255256
return this;
256257
}
257258

DotNET/Parser/CommandParser.cs

Lines changed: 24 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,27 @@ public CommandParser OnError(Action<object, string> callback)
121121
/// If the input does not start with a prefix, it returns false so the message can be processed further. (As a chat message, for example)
122122
/// </returns>
123123
public bool Parse(string input, int accessLevel = 0)
124+
{
125+
return Parse(input, null, accessLevel);
126+
}
127+
128+
/// <summary>
129+
/// Parses text in search of a command (with prefix), and runs it accordingly.
130+
/// </summary>
131+
/// <remarks>
132+
/// Data does not need to be formatted in any way before parsing. Simply pass your input to the function and
133+
/// it will determine if it is a valid command, check the command's <c>Command.CanExecute</c> function, and run the
134+
/// command.
135+
/// Use <c>Arguments[].FromName(...)</c> to get the values of the parsed arguments in the command action.
136+
/// </remarks>
137+
/// <param name="data">Data to pass to the command. This data can be used by the command when it is executed.</param>
138+
/// <param name="input">A string inputted by a user. If the string does not start with the parser prefix, it will return false, otherwise it will parse the command.</param>
139+
/// <param name="accessLevel">An optional level to limit executing commands if the user doesn't have permission.</param>
140+
/// <returns>
141+
/// True if the input is non-empty and starts with the <c>Prefix</c>.
142+
/// If the input does not start with a prefix, it returns false so the message can be processed further. (As a chat message, for example)
143+
/// </returns>
144+
public bool Parse(string input, object data, int accessLevel = 0)
124145
{
125146
if (string.IsNullOrEmpty(input))
126147
return false;
@@ -130,9 +151,9 @@ public bool Parse(string input, int accessLevel = 0)
130151
if (!string.IsNullOrEmpty(Prefix))
131152
{
132153
var index = input.IndexOf(Prefix, StringComparison.OrdinalIgnoreCase);
133-
if (index == -1)
154+
if (index != 0)
134155
return false;
135-
input = input.Remove(index, Prefix.Length);
156+
input = input.Remove(0, Prefix.Length);
136157
}
137158
if (string.IsNullOrEmpty(input))
138159
return false;
@@ -177,7 +198,7 @@ public bool Parse(string input, int accessLevel = 0)
177198
var alias = inputArgs.ElementAt(0).ToLower(); //Preserve the alias typed in.
178199
inputArgs.RemoveAt(0); //Remove the command name.
179200
if (!ParseArguments(false, alias, command, command, inputArgs, returnArgs))
180-
command.Execute(returnArgs.ToArray()); //Execute the command.
201+
command.Execute(returnArgs.ToArray(), data); //Execute the command.
181202

182203
//Return argument values back to default.
183204
ResetArgs(command);

DotNET/Parser/Properties/AssemblyInfo.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
using System.Runtime.InteropServices;
33

44
[assembly: AssemblyTitle("Command Parser")]
5-
[assembly: AssemblyDescription("Simple, lightweight, but powerful command parsing library. Useful for command handling in chat or game applications.")]
5+
[assembly: AssemblyDescription("Simple, lightweight, and powerful command parsing library. Useful for command handling in chat or game applications.")]
66
[assembly: AssemblyCompany("Pyratron Studios")]
77
[assembly: AssemblyProduct("Command Parser")]
88
[assembly: AssemblyCopyright("Copyright © 2015 Pyratron Studios")]
@@ -11,5 +11,5 @@
1111

1212
[assembly: Guid("a8cce35f-dbe6-4ab6-b340-c10dc7c907e3")]
1313

14-
[assembly: AssemblyVersion("1.2")]
15-
[assembly: AssemblyFileVersion("1.2")]
14+
[assembly: AssemblyVersion("1.3")]
15+
[assembly: AssemblyFileVersion("1.3")]

DotNET/Tests/Properties/AssemblyInfo.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,5 +11,5 @@
1111

1212
[assembly: Guid("ec03b077-6925-4571-ba49-df9371da0941")]
1313

14-
[assembly: AssemblyVersion("1.2")]
15-
[assembly: AssemblyFileVersion("1.2")]
14+
[assembly: AssemblyVersion("1.3")]
15+
[assembly: AssemblyFileVersion("1.3")]

DotNET/Tests/Tests.cs

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ public void TestArguments()
7676
.Create("Test")
7777
.AddAlias("test")
7878
.AddArgument(Argument.Create("arg"))
79-
.SetAction(delegate(Argument[] args) { result = args.FromName("arg"); }));
79+
.SetAction(delegate(Argument[] args, object data) { result = args.FromName("arg"); }));
8080

8181
parser.Parse("test " + input);
8282

@@ -95,7 +95,7 @@ public void TestDefaultArguments()
9595
.Create("Test")
9696
.AddAlias("test")
9797
.AddArgument(Argument.Create("arg").MakeOptional().SetDefault(10))
98-
.SetAction(delegate(Argument[] arguments) { value = int.Parse(arguments.FromName("arg")); }));
98+
.SetAction(delegate(Argument[] arguments, object data) { value = int.Parse(arguments.FromName("arg")); }));
9999

100100
//Test specified value
101101
parser.Parse("test 20");
@@ -239,7 +239,7 @@ public void TestOptionalEnumArguments()
239239
.Create("Test")
240240
.AddAlias("test")
241241
.SetAction(
242-
delegate(Argument[] arguments)
242+
delegate(Argument[] arguments, object data)
243243
{
244244
arg1 = arguments.FromName("arg1");
245245
arg2 = arguments.FromName("arg2");
@@ -285,10 +285,11 @@ public void TestCanExecute()
285285
var parser = CommandParser.CreateNew().UsePrefix(string.Empty);
286286

287287
parser.AddCommand(Command
288-
.Create("Test")
289-
.AddAlias("test")
290-
.SetAction(obj => ran = true)
291-
.SetExecutePredicate(command => canExecute ? string.Empty : "Error"));
288+
.Create("Test")
289+
.AddAlias("test")
290+
.SetAction(delegate { ran = true; }
291+
)
292+
.SetExecutePredicate(command => canExecute ? string.Empty : "Error"));
292293

293294
//CanExecute true
294295
parser.Parse("test");

DotNET/Tests/Tests.csproj

Lines changed: 20 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -36,12 +36,25 @@
3636
<StartupObject />
3737
</PropertyGroup>
3838
<ItemGroup>
39-
<Reference Include="nunit.framework">
39+
<Reference Include="nunit.core, Version=2.6.4.14350, Culture=neutral, PublicKeyToken=96d09a1eb7f44a77, processorArchitecture=MSIL">
40+
<HintPath>..\packages\NUnitTestAdapter.2.0.0\lib\nunit.core.dll</HintPath>
41+
<Private>True</Private>
42+
</Reference>
43+
<Reference Include="nunit.core.interfaces, Version=2.6.4.14350, Culture=neutral, PublicKeyToken=96d09a1eb7f44a77, processorArchitecture=MSIL">
44+
<HintPath>..\packages\NUnitTestAdapter.2.0.0\lib\nunit.core.interfaces.dll</HintPath>
45+
<Private>True</Private>
46+
</Reference>
47+
<Reference Include="nunit.framework, Version=2.6.4.14350, Culture=neutral, PublicKeyToken=96d09a1eb7f44a77, processorArchitecture=MSIL">
4048
<HintPath>..\packages\NUnit.2.6.4\lib\nunit.framework.dll</HintPath>
49+
<Private>True</Private>
50+
</Reference>
51+
<Reference Include="nunit.util, Version=2.6.4.14350, Culture=neutral, PublicKeyToken=96d09a1eb7f44a77, processorArchitecture=MSIL">
52+
<HintPath>..\packages\NUnitTestAdapter.2.0.0\lib\nunit.util.dll</HintPath>
53+
<Private>True</Private>
4154
</Reference>
42-
<Reference Include="NUnit.VisualStudio.TestAdapter">
43-
<HintPath>..\packages\NUnitTestAdapter.1.2\lib\NUnit.VisualStudio.TestAdapter.dll</HintPath>
44-
<Private>False</Private>
55+
<Reference Include="NUnit.VisualStudio.TestAdapter, Version=2.0.0.0, Culture=neutral, PublicKeyToken=4cb40d35494691ac, processorArchitecture=MSIL">
56+
<HintPath>..\packages\NUnitTestAdapter.2.0.0\lib\NUnit.VisualStudio.TestAdapter.dll</HintPath>
57+
<Private>True</Private>
4558
</Reference>
4659
<Reference Include="System" />
4760
<Reference Include="System.Core" />
@@ -51,9 +64,6 @@
5164
<Compile Include="Tests.cs" />
5265
<Compile Include="Properties\AssemblyInfo.cs" />
5366
</ItemGroup>
54-
<ItemGroup>
55-
<None Include="packages.config" />
56-
</ItemGroup>
5767
<ItemGroup>
5868
<ProjectReference Include="..\Parser\Parser.csproj">
5969
<Project>{78de8e68-61d9-4fb5-b25b-1ec94f96e1fd}</Project>
@@ -63,6 +73,9 @@
6373
<ItemGroup>
6474
<Service Include="{82A7F48D-3B50-4B1E-B82E-3ADA8210C358}" />
6575
</ItemGroup>
76+
<ItemGroup>
77+
<None Include="packages.config" />
78+
</ItemGroup>
6679
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
6780
<!-- To modify your build process, add your task inside one of the targets below and uncomment it.
6881
Other similar extension points exist, see Microsoft.Common.targets.

0 commit comments

Comments
 (0)