[Home] [Downloads] [Search] [Help/forum]

Gammon Forum

See www.mushclient.com/spam for dealing with forum spam. Please read the MUSHclient FAQ!

[Folder]  Entire forum
-> [Folder]  MUSHclient
. -> [Folder]  Lua
. . -> [Subject]  parsing of command-line style arguments
Home  |  Users  |  Search  |  FAQ
Username:
Register forum user name
Password:
Forgotten password?

parsing of command-line style arguments

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


Posted by Victorious   (85 posts)  [Biography] bio
Date Tue 28 Jun 2016 12:16 PM (UTC)

Amended on Tue 28 Jun 2016 12:18 PM (UTC) by Victorious

Message
I want to have a command for a plugin that has the following form:
add <quantity> <spell name> [additional options] where additional options is a space-separated list of command-line style switches that may be specified.
For example, valid input would be "add 10 magic missile", or "add 10 magic missile -d someStringWhichMayHaveSpaces -s someOtherString". Is there a nice way of doing this using regex when the switches can come in any order, and may not even be present? Valid switches are -d, -p, -s, -v, -w.
[Go to top] top

Posted by Nick Gammon   Australia  (21,322 posts)  [Biography] bio   Forum Administrator
Date Reply #1 on Tue 28 Jun 2016 08:07 PM (UTC)
Message
What I would do here is use a simple wildcard, and then use Lua pattern-matching to parse the arguments.

For example: http://www.gammon.com.au/scripts/doc.php?lua=string.gmatch

- Nick Gammon

www.gammon.com.au, www.mushclient.com
[Go to top] top

Posted by Victorious   (85 posts)  [Biography] bio
Date Reply #2 on Wed 29 Jun 2016 01:48 PM (UTC)
Message
You suggest iterating over each word of the string?

Another idea i had was to have n regular expressions where n is the number of switches i support, something like "^.+ -d (.+)$" to pick out the argument for -d. The problem is that I need it to capture everything after -d, but stop just before encountering any of the other switches (e.g, "-w").
[Go to top] top

Posted by Nick Gammon   Australia  (21,322 posts)  [Biography] bio   Forum Administrator
Date Reply #3 on Wed 29 Jun 2016 09:36 PM (UTC)

Amended on Thu 30 Jun 2016 05:55 AM (UTC) by Nick Gammon

Message
This is what I am talking about:


<aliases>
  <alias
   match="^add (?P&lt;quantity&gt;\d*) *(?P&lt;spell&gt;[^-]+)(?P&lt;args&gt;.*)$"
   enabled="y"
   regexp="y"
   send_to="12"
   sequence="100"
  >
  <send>

quantity = tonumber ("%&lt;quantity&gt;")
print ("quantity = ", quantity)
spell = Trim ("%&lt;spell&gt;")
print ("spell = ", spell)
args = "%&lt;args&gt;"
print ("args = ", args)

for arg, value in string.gmatch (args, "%-(%a)([^-]+)") do
  value = Trim (value)
  print ("Got argument", arg)
  print ("Got value", value)
end -- for

</send>
  </alias>
</aliases>


Template:pasting For advice on how to copy the above, and paste it into MUSHclient, please see Pasting XML.


The regexp without all the XML around it (needed for pasting the whole alias) is:


^add (?P<quantity>\d*) *(?P<spell>[^-]+)(?P<args>.*)$


That is:


  • "add "
  • Zero or more digits, stored in capture "quantity"
  • Zero or more spaces after the digits (we already have a space after "add")
  • The spell name is anything except a hyphen - however at least one thing - stored in capture "spell"
  • The arguments are everything after the spell name (if anything), stored in capture "args"


The pattern for the arguments (in Lua regexp) is:


  • A hyphen
  • A letter
  • One or more of anything except a hyphen






Test string:


add 10 magic missile -d some String Which May Have Spaces -s someOtherString


Output:


quantity =  10
spell =  magic missile
args =  -d some String Which May Have Spaces -s someOtherString
Got argument d
Got value some String Which May Have Spaces
Got argument s
Got value someOtherString


Test:


add magic missile


Output:


quantity =  nil
spell =  magic missile
args =  


You can test for quantity == nil to see if quantity was supplied or not.

- Nick Gammon

www.gammon.com.au, www.mushclient.com
[Go to top] top

Posted by Victorious   (85 posts)  [Biography] bio
Date Reply #4 on Sun 03 Jul 2016 07:49 AM (UTC)
Message
Is it possible to allow for arguments to switches to contain the - character?
[Go to top] top

Posted by Nick Gammon   Australia  (21,322 posts)  [Biography] bio   Forum Administrator
Date Reply #5 on Sun 03 Jul 2016 09:04 PM (UTC)
Message
Yes you can, but you have to rework it a bit:


<aliases>
  <alias
   match="^add (?P&lt;quantity&gt;\d*) *(?P&lt;spell&gt;[^-]+)(?P&lt;args&gt;.*)$"
   enabled="y"
   regexp="y"
   send_to="12"
   sequence="100"
  >
  <send>

quantity = tonumber ("%&lt;quantity&gt;")
print ("quantity = ", quantity)
spell = Trim ("%&lt;spell&gt;")
print ("spell = ", spell)
args = "%&lt;args&gt;"
print ("args = ", args)

while true do
  -- find first argument
  st, en, arg = string.find (args, "%-(%a+)")

  -- see if any found
  if not st then
    break  -- all done!
  end -- if

  -- find next argument, if any
  st2 = string.find (args, " %-", en)
  if not st2 then
    st2 = -1
  end -- if not found

  value = Trim (args:sub (en + 1, st2))
  print ("Got argument", arg)
  print ("Got value", value)

  args = args:sub (st2)  -- remove this argument
end -- while


</send>
  </alias>
</aliases>


Template:pasting For advice on how to copy the above, and paste it into MUSHclient, please see Pasting XML.


Test:


add 10 magic missile -d some String Which May Have Spaces -s some-Other-String -foo bar


Output:


quantity =  10
spell =  magic missile
args =  -d some String Which May Have Spaces -s some-Other-String -foo bar
Got argument d
Got value some String Which May Have Spaces
Got argument s
Got value some-Other-String
Got argument foo
Got value bar


I've made it this time so you can have a longer word (eg. "-foo") as the parameter name. Of course, then you have to have a space after it to delimit it from what the argument is.

- Nick Gammon

www.gammon.com.au, www.mushclient.com
[Go to top] top

Posted by Victorious   (85 posts)  [Biography] bio
Date Reply #6 on Thu 14 Jul 2016 01:50 PM (UTC)
Message
This looks perfect - thanks a lot :)
[Go to top] 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.


2,991 views.

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

Go to topic:           Search the forum


[Go to top] top

Quick links: MUSHclient. MUSHclient help. Forum shortcuts. Posting templates. Lua modules. Lua documentation.

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

[Home]


Written by Nick Gammon - 5K   profile for Nick Gammon on Stack Exchange, a network of free, community-driven Q&A sites   Marriage equality

Comments to: Gammon Software support
[RH click to get RSS URL] Forum RSS feed ( https://gammon.com.au/rss/forum.xml )

[Best viewed with any browser - 2K]    [Hosted at FutureQuest]