Reply
 
Thread Tools Display Modes
Old 11-17-12, 09:01 AM   #1
gmarco
Premium Member
 
gmarco's Avatar
AddOn Author - Click to view addons
Join Date: Dec 2009
Posts: 97
Parsing argument list

I'd like to write an addon that use such usage syntax:

/remgank - This help
/remgank add <player_name> <note> - Add a ganker
/remgank del <player_name> - Remove a ganker
/remgank lst - List gankers


I begin with:

Lua Code:
  1. SlashCmdList["REMGANK"] = function(args)
  2.     local args=SecureCmdOptionParse(args:trim());
  3.    
  4.     cmd, name, note = args:match("(%w+)%s+(%w+)%s+(.*)")
  5.     cmd, name = args:match("(%w+)%s+(%w)") 
  6.     cmd = args:match("(%w+)")
  7.  
  8.     if (cmd == "" or cmd == nil) then
  9.         print(prgname .. " commands");
  10.         print("/remgank - This help");
  11.         print("/remgank add <player_name> <note> - Add a ganker");
  12.         print("/remgank del <player_name> - Remove a ganker");
  13.         print("/remgank lst - List those silly gankers");              
  14.         return;
  15.     end
  16.  
  17.     if cmd:lower() == "add" then
  18.  
  19.         ....

Is there a better way to parse the cmd line when we doesn't know the number of args before ?
It should be somewhere a global array for them I think ... but I tried all the afternoon without any success in finding a more beautifull way to parse :-/

Thanks for any tips/clues if any.
gmarco is offline   Reply With Quote
Old 11-17-12, 09:26 AM   #2
Vlad
A Molten Giant
 
Vlad's Avatar
AddOn Author - Click to view addons
Join Date: Dec 2005
Posts: 753
Just an idea:

Code:
local arg1, arg2, arg3 = string.split(" ", args, 3)

if arg1 == "add" then
  -- add "arg2" (player name) with note "arg3" (may be empty) to the list
elseif arg1 == "del" then
  -- remove "arg2" (player name) from list
elseif arg1 == "list" then
  -- list gankers
else
  -- help
end
__________________
Profile: Curse | Wowhead
Vlad is offline   Reply With Quote
Old 11-17-12, 02:52 PM   #3
Akryn
A Firelord
AddOn Author - Click to view addons
Join Date: Mar 2008
Posts: 479
If you want a table from a space-delimited string, you can do this:

Code:
args = {strsplit(" ", args)}
Akryn is offline   Reply With Quote
Old 11-18-12, 10:53 AM   #4
gmarco
Premium Member
 
gmarco's Avatar
AddOn Author - Click to view addons
Join Date: Dec 2009
Posts: 97
The problem was that the 3rd arg that was not only a single word but "the rest of the cmd line" :-)

So probably a lot of substrings being the "note field" .

BTW the method suggest by Vlad works great.
If someone would give a look at the addon result ... look at the end of the this thread:

http://www.wowinterface.com/forums/s...063#post269063
gmarco is offline   Reply With Quote
Reply

Go BackWoWInterface » Developer Discussions » Lua/XML Help » Parsing argument list

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off