Difference between revisions of "Creating Commands"
From ReflexCLI
(Created page with "Category:Commands| ") |
|||
(5 intermediate revisions by the same user not shown) | |||
Line 1: | Line 1: | ||
− | [[Category:Commands| ] | + | [[Category:Commands| ]] |
+ | Creating Commands: | ||
+ | You can create commands from any c# methods, fields or properties by adding the <code>[ConsoleCommand]</code> attribute. | ||
+ | |||
+ | ==Basic Example== | ||
+ | |||
+ | <pre> | ||
+ | using ReflexCLI.Attributes; | ||
+ | |||
+ | namespace CommandTest | ||
+ | { | ||
+ | class TestCommandLibrary | ||
+ | { | ||
+ | [ConsoleCommand] | ||
+ | private static string Echo(string inString) | ||
+ | { | ||
+ | return inString; | ||
+ | } | ||
+ | |||
+ | [ConsoleCommand] | ||
+ | protected static int IntegerField = 32; | ||
+ | |||
+ | [ConsoleCommand] | ||
+ | public static bool BooleanProperty | ||
+ | { | ||
+ | get { return IntegerField > 0; } | ||
+ | } | ||
+ | } | ||
+ | } | ||
+ | </pre> | ||
+ | |||
+ | This will create the following commands: | ||
+ | |||
+ | #<code>CommandTest.TestCommandLibrary.Echo</code> | ||
+ | #<code>CommandTest.TestCommandLibrary.IntegerField</code> | ||
+ | #<code>CommandTest.TestCommandLibrary.BooleanProperty</code> | ||
+ | |||
+ | ==Customising Command Prefixes== | ||
+ | By default, the commands are given fully-qualified names with namespaces etc., which can get pretty verbose. This can be fixed by using a [CommandConsoleClassCustomizer] attribute on the class: | ||
+ | |||
+ | <pre> | ||
+ | namespace CommandTest | ||
+ | { | ||
+ | [ConsoleCommandClassCustomizer("TestCommand")] | ||
+ | class TestCommandLibrary | ||
+ | { | ||
+ | // etc. | ||
+ | </pre> | ||
+ | |||
+ | This will change the prefix of the commands to <code>TestCommand</code>. and so the commands become: | ||
+ | |||
+ | # <code>TestCommand.Echo</code> | ||
+ | # <code>TestCommand.IntegerField</code> | ||
+ | # <code>TestCommand.BooleanProperty</code> | ||
+ | |||
+ | ==Customising Command Names== | ||
+ | You can also customize the name of the commands by adding the optional parameter to <code>[ConsoleCommand]</code>. For example | ||
+ | |||
+ | <pre> | ||
+ | [ConsoleCommand("Int")] | ||
+ | protected static int IntegerField = 32; | ||
+ | </pre> | ||
+ | |||
+ | which will change this command to be <code>TestCommand.Int</code>. | ||
+ | |||
+ | ==Limitations== | ||
+ | There are some limitations on what members can be turned into commands. See [[Command Limitations]] for details. |
Latest revision as of 06:06, 3 September 2017
Creating Commands:
You can create commands from any c# methods, fields or properties by adding the [ConsoleCommand]
attribute.
Basic Example
using ReflexCLI.Attributes; namespace CommandTest { class TestCommandLibrary { [ConsoleCommand] private static string Echo(string inString) { return inString; } [ConsoleCommand] protected static int IntegerField = 32; [ConsoleCommand] public static bool BooleanProperty { get { return IntegerField > 0; } } } }
This will create the following commands:
CommandTest.TestCommandLibrary.Echo
CommandTest.TestCommandLibrary.IntegerField
CommandTest.TestCommandLibrary.BooleanProperty
Customising Command Prefixes
By default, the commands are given fully-qualified names with namespaces etc., which can get pretty verbose. This can be fixed by using a [CommandConsoleClassCustomizer] attribute on the class:
namespace CommandTest { [ConsoleCommandClassCustomizer("TestCommand")] class TestCommandLibrary { // etc.
This will change the prefix of the commands to TestCommand
. and so the commands become:
-
TestCommand.Echo
-
TestCommand.IntegerField
-
TestCommand.BooleanProperty
Customising Command Names
You can also customize the name of the commands by adding the optional parameter to [ConsoleCommand]
. For example
[ConsoleCommand("Int")] protected static int IntegerField = 32;
which will change this command to be TestCommand.Int
.
Limitations
There are some limitations on what members can be turned into commands. See Command Limitations for details.