Command Switches and Args

Commands often have switches and arguments. The basic command format is:

root/switch args

For example:

scene/join 123

There are some other advanced command options, but for now let’s focus on the basics.

Accessing Command Parameters

We can access the different parts of the command using the cmd variable, which is one of those handy variables (like client and enactor) provided by the CommandHandler class.

cmd.root
cmd.switch
cmd.args

There are also two methods for easily determining if a root or switch matches some text.

cmd.switch_is?("someswitch")
cmd.root_is?("someroot")

Try It! - Adding a Switch

The tinker command doesn’t have any switches and arguments by default, but we can add some.

Let’s try a switch first. Edit the tinker handle method as shown and save it.

def handle
   if (cmd.switch_is?("one"))
     client.emit_success "One success!"
   elsif (cmd.switch_is?("two"))
     client.emit_success "Two successes!"
   else
     client.emit_failure "Unrecognized tinker switch."
   end
 end

Try out the command with different switches: tinker/one, tinker/two, tinker/three. Observe how the output changes.

Try It! - Adding an Argument

Now let’s try using an argument. Change the tinker handle method as shown and save it.

def handle
  client.emit_ooc "#{cmd.args} little piggies!"
end

Try out the command with different arguments: tinker 1, tinker 2. Observe the output.

It would be nice to make the grammar correct though, wouldn’t it? Try this:

def handle
  if (cmd.args == "1")
    client.emit_ooc "#{cmd.args} little piggy!"
  else
    client.emit_ooc "#{cmd.args} little piggies!"
  end
end

Notice that we used a string (“1”) instead of an actual number in the if check. That’s because all arguments are strings by default. We’ll learn how to parse arguments in different formats (numbers, lists, etc.) in the next exercise.

This article is part of the Code Quickstart tutorial.