Register forum user name Search FAQ

Gammon Forum

Notice: Any messages purporting to come from this site telling you that your password has expired, or that you need to verify your details, confirm your email, resolve issues, making threats, or asking for money, are spam. We do not email users with any such messages. If you have lost your password you can obtain a new one by using the password reset link.

Due to spam on this forum, all posts now need moderator approval.

 Entire forum ➜ MUSHclient ➜ Tips and tricks ➜ Making an alias with an optional argument.

Making an alias with an optional argument.

It is now over 60 days since the last post. This thread is closed.     Refresh page


Posted by Magnum   Canada  (580 posts)  Bio
Date Tue 30 Jul 2002 05:18 PM (UTC)

Amended on Tue 30 Jul 2002 05:22 PM (UTC) by Magnum

Message
Occasionally, I will want to make an alias where I want the argument (wildcard) to be optional. Currently, this would require two aliases, if you want to do it the simple way:

  • One alias without the wildcard: dothat
  • Another alias with the wildcard: dothat *

However, you can achieve the desired effect with an alias set to recognize regular expressions. (One of the checkboxes on the alias configuration screen).

The following alias string would accept an argument, but not require one:

^dothat([ ]+(.*))?$

Stretched out, this is what the syntax means:

 ^      : The alias must start at the beginning of a line.
 [ ]+   : Accept one or more space characters.
 (.*)   : Accept a wildcard of any length.
 (exp)? : The circle brackets are used to wrap the expression inside as an element to be acted upon.
          The ? after the brackets indicates accept 0 or more matches of that expression.
 $      : The alias line must end here. If there were more unexpected text on the line, the alias would not match.

Now, in the script file you call, it might look like this (in Visual Basic Script):

Sub dothat (Aliasname, Aliasline, Wildcards)
  Dim Argument
  Argument = Trim(Wildcards(1))
  If Argument = Empty Then Argument = "Default Value"
  World.Send "do this"
  World.Send "do this next"
  World.Send "do that " & Argument
End Sub

* The Trim function gets rid of leading or trailing space characters.

In that example, you might decide to set a default value if no argument was provided.

I've found that with clever programming, I can reduce the number of aliases I have by consolidating them into 'advanced' aliases using features like in the example above. :)

Get my plugins here: http://www.magnumsworld.com/muds/

Constantly proving I don't know what I am doing...
Magnum.
Top

Posted by Nick Gammon   Australia  (23,158 posts)  Bio   Forum Administrator
Date Reply #1 on Tue 30 Jul 2002 09:38 PM (UTC)
Message
You can probably avoid the "trim" by using Wildcards (2) which is the contents of the second set of brackets (the actual optional argument).

- Nick Gammon

www.gammon.com.au, www.mushclient.com
Top

Posted by Magnum   Canada  (580 posts)  Bio
Date Reply #2 on Wed 31 Jul 2002 05:01 AM (UTC)
Message
Actually, as soon as I finished writing that, I logged off for the night, and as I was shutting down my computer, I remembered something else from the "RegularExpressions.txt" file that I read (Which is in the MUSHclient directory).

This is actually a better regular expression to use:

^at(?:[ ]+(.*))?$

The ?: indicates that although the following element should attempt a match, it will not be returned as any part of a wildcard.

Thus, the following script works perfectly:

Sub AliasTest (AliasName, AliasLine, Wildcards)
	Dim Argument
	Argument = Wildcards(1)
	World.Note "Argument is:  '" & Argument & "'"
End Sub

This may seem trivial... but given that there is a maximum of 10 wildcards per alias or trigger, using the ?: to match a "wild" string, but not pull it as a wildcard, may prove useful. (It's good training to be as efficient a programmer as possible, in my humble opinion).

Get my plugins here: http://www.magnumsworld.com/muds/

Constantly proving I don't know what I am doing...
Magnum.
Top

Posted by Magnum   Canada  (580 posts)  Bio
Date Reply #3 on Wed 31 Jul 2002 05:14 AM (UTC)
Message
Just to make my point clearer, for those who didn't like that example, here are two aliases and accompanying subroutines that I actually use:

<aliases>
  <alias
   name="MONEY_Bank_Deposit"
   script="Bank_Deposit"
   match="^b\+(?:[ ]+(.*))?$"
   enabled="y"
   regexp="y"
   ignore_case="y"
  >
  </alias>
</aliases>
<aliases>
  <alias
   name="MONEY_Bank_Withdraw"
   script="Bank_Withdraw"
   match="^b\-(?:[ ]+(.*))?$"
   enabled="y"
   regexp="y"
   ignore_case="y"
  >
  </alias>
</aliases>

Sub Bank_Deposit (AliasName, AliasLine, arrWildcards)
	Dim Arguments
	If arrWildcards(1) = Empty Then
		Arguments = "all"
	Else
		Arguments = arrWildcards(1)
	End If
	World.Send "deposit " & Arguments
	World.Send "balance"
End Sub

Sub Bank_Withdraw (AliasName, AliasLine, arrWildcards)
	Dim Arguments
	If arrWildcards(1) = Empty Then
		Arguments = "all"
	Else
		Arguments = arrWildcards(1)
	End If
	World.Send "withdraw " & Arguments
	World.Send "balance"
End Sub

What this allows me to do, for example, is simply type "b+" if I want to deposit all the money in my inventory. On the other hand, if I only want to deposit, say 1000 coins, I could type "b+ 1000", which would do the job as expected. :)

If I really wanted to get fancy, I could even make this alias only accept numeric digits as wildcards... but I'll leave that for another time. :)

Get my plugins here: http://www.magnumsworld.com/muds/

Constantly proving I don't know what I am doing...
Magnum.
Top

The dates and times for posts above are shown in Universal Co-ordinated Time (UTC).

To show them in your local time you can join the forum, and then set the 'time correction' field in your profile to the number of hours difference between your location and UTC time.


13,992 views.

It is now over 60 days since the last post. This thread is closed.     Refresh page

Go to topic:           Search the forum


[Go to top] top

Information and images on this site are licensed under the Creative Commons Attribution 3.0 Australia License unless stated otherwise.