Skip to content

Commit 31bb101

Browse files
committed
docs: add docs on command creation (now we have covered all the examples from the Examples module)
1 parent 6ef8aa3 commit 31bb101

14 files changed

+313
-14
lines changed

docs/annotated/annotated.md

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,15 @@ we use this instead of the classic `Command.builder(String)` method.
1111
You can make a side-by-side comparison of the two approaches in the following table
1212
and links:
1313

14-
| Imperative | Annotated |
15-
|----------------------------------------------------------------------------|-------------------------------------------------------------------------|
16-
| [Basic Command Creation](../imperatively/basic.md) | [Basic Command Creation](../annotated/basic.md) |
17-
| [Command with single Argument](../imperatively/argument.md) | [Command with single Argument](../annotated/argument.md) |
18-
| [Command with multiple Arguments](../imperatively/multiple-arguments.md) | [Command with multiple Arguments](../annotated/multiple-arguments.md) |
19-
| [Command with optional Arguments](../imperatively/optional-arguments.md) | [Command with optional Arguments](../annotated/optional-arguments.md) |
14+
| Imperative | Annotated |
15+
|--------------------------------------------------------------------------|-----------------------------------------------------------------------|
16+
| [Basic Command Creation](../imperatively/basic.md) | [Basic Command Creation](../annotated/basic.md) |
17+
| [Command with single Argument](../imperatively/argument.md) | [Command with single Argument](../annotated/argument.md) |
18+
| [Command with multiple Arguments](../imperatively/multiple-arguments.md) | [Command with multiple Arguments](../annotated/multiple-arguments.md) |
19+
| [Command with optional Arguments](../imperatively/optional-arguments.md) | [Command with optional Arguments](../annotated/optional-arguments.md) |
20+
| [Command with Permission](../imperatively/with-permission.md) | [Command with Permission](../annotated/with-permission.md) |
21+
| [Command with Switch Arguments](../imperatively/with-switches.md) | [Command with Switch Arguments](../annotated/with-switches.md) |
22+
| [Command with Value Flags](../imperatively/with-value-flags.md) | [Command with Value Flags](../annotated/with-value-flags.md) |
2023

2124
### Elements of the Annotated Command API
2225

docs/annotated/index.txt

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,4 +6,7 @@ subcommand-instance-creator.md
66
basic.md
77
argument.md
88
multiple-arguments.md
9-
optional-arguments.md
9+
optional-arguments.md
10+
with-permission.md
11+
with-switches.md
12+
with-value-flags.md

docs/annotated/with-permission.md

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
## #5 With Permission
2+
3+
This example shows how to create a command with a permission requirement
4+
with the annotated approach:
5+
6+
```java
7+
@Command(names = "greet", permission = "myperm.command.greet")
8+
public class GreetingCommand implements CommandClass {
9+
10+
@Command(names = "")
11+
public void run(String name) {
12+
System.out.println("Hello, " + name + "!");
13+
}
14+
15+
}
16+
```
17+
18+
The `@Command` annotation accepts a `permission` parameter, which is
19+
used to specify the permission required to execute the command.
20+
21+
Check the [Authorizer page](../configuration/authorizer.md) for more information
22+
on how to specify how the `CommandManager` should check for permissions.

docs/annotated/with-switches.md

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
## #6 With Switches
2+
3+
This example shows how to create a command with "switch" arguments
4+
(boolean flags) with the imperative approach, note that switch arguments
5+
values are always present. Presence of the switch argument indicates `true`,
6+
and its absence indicates `false`.
7+
8+
<!--@formatter:off-->
9+
```java
10+
@Command(names = "test")
11+
public class TestCommand implements CommandClass {
12+
13+
@Command(names = "")
14+
public void run(String name, @Switch("g") boolean goodBye) {
15+
if (goodBye) {
16+
System.out.println("Goodbye " + name);
17+
return;
18+
}
19+
System.out.println("Hi " + name);
20+
}
21+
22+
}
23+
```
24+
<!--@formatter:on-->
25+
26+
- Executing `test Fixed` will print `Hi Fixed`
27+
- Executing `test -g Fixed` will print `Goodbye Fixed`
28+
- Executing `test Fixed -g` will print `Goodbye Fixed`

docs/annotated/with-value-flags.md

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
## #7 With Value Flags
2+
3+
This example shows how to create a command with value flag arguments
4+
the main difference between a value flag and a switch is that the value flag
5+
takes the next argument as its value, and the switch does not.
6+
7+
For example: `-g true`, `-p 25565`, `-n Fixed`
8+
9+
<!--@formatter:off-->
10+
```java
11+
@Command(names = "test")
12+
public class TestCommand implements CommandClass {
13+
14+
@Command(names = "")
15+
public void run(String name, @Flag("g") String greeting) {
16+
System.out.println(greeting + " " + name);
17+
}
18+
19+
}
20+
```
21+
<!--@formatter:on-->
22+
23+
- Executing `test Fixed` will print `Hi Fixed`
24+
- Executing `test Fixed -g GoodBye` will print `GoodBye Fixed`
25+
- Executing `test Fixed -g Hello` will print `Hello Fixed`
26+
- Executing `test -g Fixed` will throw a `NoMoreArguments` exception, meaning that the parsing failed
27+
because the `Fixed` argument was taken as the value for the flag and no argument
28+
is remaining for the name.
29+
- Executing `test Fixed -g` will throw a `NoMoreArguments` exception, meaning that the parsing failed
30+
because the flag doesn't has any argument left to use.

docs/execution/context.md

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
## Command Context
2+
3+
As we have seen in [Command Execution](./execution.md), we can access previously set
4+
variables in the execution namespace, from a command action, awesome!
5+
6+
But now, we are going to explore more about the command context, and how we can
7+
access more information about the command execution.
8+
9+
### Information
10+
11+
The `CommandContext` contains information about the command execution, such as:
12+
- A reference to the command *(it may be root or sub command)* being executed
13+
- A reference to the root command being executed
14+
- The execution path
15+
- The input tokens or arguments
16+
- The used labels (which name/alias was used for every command and sub-command)
17+
- A command part value
18+
- All the data from the execution namespace
19+
20+
### Example
21+
22+
<!--@formatter:off-->
23+
```java
24+
// create the command manager
25+
CommandManager manager = ...;
26+
27+
// create & register our command
28+
Command command = Command.builder("test")
29+
.addAlias("testalias")
30+
.action(context -> {
31+
// Here we have an action of the command, and here we can use the context for this command
32+
// The CommandContext is the result of parsing the arguments of a command.
33+
// It also extends Namespace, so you can use the Namespace methods on it.
34+
35+
// The labels are the names for every command/subcommand executed.
36+
// This is the name of the last subcommand/command
37+
String label = context.getLabels().get(context.getLabels().size() - 1);
38+
39+
System.out.println("Label: " + label);
40+
})
41+
.build();
42+
manager.registerCommand(command);
43+
44+
// execute the command
45+
manager.execute(Namespace.create(), "testalias"); // Will print 'Label: testalias'
46+
manager.execute(Namespace.create(), "test"); // Will print 'Label: test'
47+
```
48+
<!--@formatter:on-->

docs/execution/execution.md

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
## Command Execution
2+
3+
We already know how to create commands and register them, but now we need to
4+
actually execute the commands.
5+
6+
> Note that, in certain platforms like Bukkit, Velocity, Discord with JDA, you will not
7+
have to worry about executing commands since the platform-specific `CommandManager` implementation
8+
will listen to native events and automatically execute the commands for you, awesome!
9+
10+
We can execute a command by calling the `CommandManager#execute` method, which will
11+
parse and execute the command from a string command line.
12+
13+
For example:
14+
15+
<!--@formatter:off-->
16+
```java
17+
// create a command manager
18+
CommandManager manager = ...;
19+
20+
// set up the variables passed to the command
21+
Namespace namespace = Namespace.create();
22+
namespace.setObject(User.class, "USER", new User("Fixed", 16));
23+
24+
// executes the command "greet" with an argument "Yusshu"
25+
manager.execute(namespace, "greet Yusshu");
26+
```
27+
<!--@formatter:on-->
28+
29+
Note that a `Namespace` is given to the `execute` command, namespaces are used to
30+
store variables that can be accessed by the command. For example, the `greet` command
31+
can access the `USER` variable that we set in the namespace.
32+
33+
Here is an example where we access a previously set variable from inside the `Command`
34+
action:
35+
36+
<!--@formatter:off-->
37+
```java
38+
// create a command manager
39+
CommandManager manager = ...;
40+
41+
// create & register the command
42+
Command command = Command.builder("test")
43+
.action(context -> {
44+
User sender = context.getObject(User.class, "SENDER");
45+
System.out.println("'test' command used by " + sender.getName());
46+
})
47+
.build();
48+
manager.registerCommand(command);
49+
50+
// set up the variables passed to the command
51+
Namespace namespace = Namespace.create();
52+
namespace.setObject(User.class, "SENDER", new User("Yusshu", 18));
53+
54+
// executes the command "test"
55+
manager.execute(namespace, "test");
56+
```
57+
<!--@formatter:on-->
File renamed without changes.

docs/imperatively/index.txt

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,7 @@ intro.md
22
basic.md
33
argument.md
44
multiple-arguments.md
5-
optional-arguments.md
5+
optional-arguments.md
6+
with-permission.md
7+
with-switches.md
8+
with-value-flags.md

docs/imperatively/intro.md

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,12 @@ An alternative to this approach is to use the [annotation-based command creation
1010
You can make a side-by-side comparison of the two approaches in the following table
1111
and links:
1212

13-
| Imperative | Annotated |
14-
|----------------------------------------------------------------------------|-------------------------------------------------------------------------|
15-
| [Basic Command Creation](../imperatively/basic.md) | [Basic Command Creation](../annotated/basic.md) |
16-
| [Command with single Argument](../imperatively/argument.md) | [Command with single Argument](../annotated/argument.md) |
17-
| [Command with multiple Arguments](../imperatively/multiple-arguments.md) | [Command with multiple Arguments](../annotated/multiple-arguments.md) |
18-
| [Command with optional Arguments](../imperatively/optional-arguments.md) | [Command with optional Arguments](../annotated/optional-arguments.md) |
13+
| Imperative | Annotated |
14+
|--------------------------------------------------------------------------|-----------------------------------------------------------------------|
15+
| [Basic Command Creation](../imperatively/basic.md) | [Basic Command Creation](../annotated/basic.md) |
16+
| [Command with single Argument](../imperatively/argument.md) | [Command with single Argument](../annotated/argument.md) |
17+
| [Command with multiple Arguments](../imperatively/multiple-arguments.md) | [Command with multiple Arguments](../annotated/multiple-arguments.md) |
18+
| [Command with optional Arguments](../imperatively/optional-arguments.md) | [Command with optional Arguments](../annotated/optional-arguments.md) |
19+
| [Command with Permission](../imperatively/with-permission.md) | [Command with Permission](../annotated/with-permission.md) |
20+
| [Command with Switch Arguments](../imperatively/with-switches.md) | [Command with Switch Arguments](../annotated/with-switches.md) |
21+
| [Command with Value Flags](../imperatively/with-value-flags.md) | [Command with Value Flags](../annotated/with-value-flags.md) |

docs/imperatively/with-permission.md

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
## #5 With Permission
2+
3+
This example shows how to create a command with a permission requirement
4+
with the annotated approach:
5+
6+
<!--@formatter:off-->
7+
```java
8+
Command testUserCommand = Command.builder("test")
9+
// We set the permission of the test command into admin
10+
.permission("admin")
11+
.action(context -> {
12+
System.out.println("Hi");
13+
})
14+
.build();
15+
```
16+
<!--@formatter:on-->
17+
18+
Executing the `testUserCommand` will print `Hi` if the user has the `admin`
19+
permission, but will result in a `NoPermissionException` if the user does not.
20+
21+
Check the [Authorizer page](../configuration/authorizer.md) for more information
22+
on how to specify how the `CommandManager` should check for permissions.

docs/imperatively/with-switches.md

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
## #6 With Switches
2+
3+
This example shows how to create a command with "switch" arguments
4+
(boolean flags) with the imperative approach, note that switch arguments
5+
values are always present. Presence of the switch argument indicates `true`,
6+
and its absence indicates `false`.
7+
8+
<!--@formatter:off-->
9+
```java
10+
CommandPart name = string("name");
11+
12+
// If the -g argument is present, then the switch value will be true, otherwise false
13+
// It can be in any position, but the parts registered before will take priority,
14+
// that means that if the name part is registered before the switch part, the -g only
15+
// can be after the name
16+
CommandPart goodByeSwitch = switchPart("goodBye", "g");
17+
18+
Command testUserCommand = Command.builder("test")
19+
.addPart(goodByeSwitch)
20+
.addPart(name)
21+
.action(context -> {
22+
// The value for a switch is never absent
23+
boolean goodBye = context.<Boolean>getValue(goodByeSwitch).get();
24+
25+
context.<String>getValue(name).ifPresent(s -> {
26+
if (goodBye) {
27+
System.out.println("Goodbye " + s);
28+
return;
29+
}
30+
System.out.println("Hi " + s);
31+
});
32+
})
33+
.build();
34+
```
35+
<!--@formatter:on-->
36+
37+
- Executing `test Fixed` will print `Hi Fixed`
38+
- Executing `test -g Fixed` will print `Goodbye Fixed`
39+
- Executing `test Fixed -g` will print `Goodbye Fixed`

docs/imperatively/with-value-flags.md

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
## #7 With Value Flags
2+
3+
This example shows how to create a command with value flag arguments
4+
the main difference between a value flag and a switch is that the value flag
5+
takes the next argument as its value, and the switch does not.
6+
7+
For example: `-g true`, `-p 25565`, `-n Fixed`
8+
9+
<!--@formatter:off-->
10+
```java
11+
CommandPart name = string("name");
12+
13+
// This part is like a switch part, the difference is that when the main "switch" is found
14+
// the part provided in the first argument takes the parsing, consuming one or more arguments
15+
// from the stack at that position.
16+
CommandPart greetingValue = string("greeting");
17+
CommandPart greetingValueFlag = valueFlag(greetingValue, "g");
18+
19+
Command testUserCommand = Command.builder("test")
20+
.addPart(greetingValueFlag)
21+
.addPart(name)
22+
.action(context -> {
23+
// The value for a value flag can be absent
24+
String greeting = context.<String>getValue(greetingValue).orElse("Hi");
25+
context.<String>getValue(name).ifPresent(s -> {
26+
System.out.println(greeting + " " + s);
27+
});
28+
})
29+
.build();
30+
```
31+
<!--@formatter:on-->
32+
33+
- Executing `test Fixed` will print `Hi Fixed`
34+
- Executing `test Fixed -g GoodBye` will print `GoodBye Fixed`
35+
- Executing `test Fixed -g Hello` will print `Hello Fixed`
36+
- Executing `test -g Fixed` will throw a `NoMoreArguments` exception, meaning that the parsing failed
37+
because the `Fixed` argument was taken as the value for the flag and no argument
38+
is remaining for the name.
39+
- Executing `test Fixed -g` will throw a `NoMoreArguments` exception, meaning that the parsing failed
40+
because the flag doesn't has any argument left to use.

docs/index.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,5 @@ installation.md
33
concepts
44
configuration
55
annotated
6+
execution
67
platforms

0 commit comments

Comments
 (0)