Quantcast Parsing argument list - WoWInterface
Thread Tools Display Modes
11-17-12, 10:01 AM   #1
gmarco
A Chromatic Dragonspawn
 
gmarco's Avatar
AddOn Author - Click to view addons
Join Date: Dec 2009
Posts: 190
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.
  Reply With Quote
11-17-12, 10:26 AM   #2
Vlad
A Molten Giant
 
Vlad's Avatar
AddOn Author - Click to view addons
Join Date: Dec 2005
Posts: 788
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
  Reply With Quote
11-17-12, 03: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)}
  Reply With Quote
11-18-12, 11:53 AM   #4
gmarco
A Chromatic Dragonspawn
 
gmarco's Avatar
AddOn Author - Click to view addons
Join Date: Dec 2009
Posts: 190
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
  Reply With Quote

WoWInterface » 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