Difference between revisions of "Creating Commands"

From ReflexCLI
Jump to: navigation, search
(Customising Command Names)
Line 42: Line 42:
 
namespace CommandTest
 
namespace CommandTest
 
{
 
{
[ConsoleCommandClassCustomizer("TestCommands")]
+
[ConsoleCommandClassCustomizer("TestCommand")]
 
class TestCommandLibrary
 
class TestCommandLibrary
 
{
 
{
Line 50: Line 50:
 
This will change the prefix of the commands to <code>TestCommand</code>. and so the commands become:
 
This will change the prefix of the commands to <code>TestCommand</code>. and so the commands become:
  
# <code>TestCommands.Echo</code>
+
# <code>TestCommand.Echo</code>
# <code>TestCommands.IntegerField</code>
+
# <code>TestCommand.IntegerField</code>
# <code>TestCommands.BooleanProperty</code>
+
# <code>TestCommand.BooleanProperty</code>
  
 
You can also customize the name of the commands by adding the optional parameter to <code>[ConsoleCommand]</code>. For example
 
You can also customize the name of the commands by adding the optional parameter to <code>[ConsoleCommand]</code>. For example
Line 61: Line 61:
 
</pre>
 
</pre>
  
which will change this command to be <code>TestCommands.Int</code>.
+
which will change this command to be <code>TestCommand.Int</code>.

Revision as of 16:10, 30 August 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 in)
        {
            return in;
        }
		
        [ConsoleCommand]
        protected static int IntegerField = 32;
		
        [ConsoleCommand]
        public static bool BooleanProperty
        {
            get { return IntegerField > 0; }
        }			
    }
}

This will create the following commands:

  1. CommandTest.TestCommandLibrary.Echo
  2. CommandTest.TestCommandLibrary.IntegerField
  3. CommandTest.TestCommandLibrary.BooleanProperty

Customising Command Names

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:

  1. TestCommand.Echo
  2. TestCommand.IntegerField
  3. TestCommand.BooleanProperty

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.