'''Short description''' At the moment plugins do use very different parameter and keywords resolving mechanisms. They destinguish very different between boths. I would like to have added a common function on wikiutil which could be used for further development of plugins parameter and keywords. For a first idea copied from the recent !ImageLink. {{{ if args: args = args.split(',') args = [arg.strip() for arg in args] else: args = [] argc = len(args) kw_count = 0 kw = {} # create a dictionary for the formatter.image call for arg in args : if '=' in arg: kw_count += 1 key, value = arg.split('=', 1) kw[str(key)] = wikiutil.escape(value, quote=1) argc -= kw_count }}} If this become a generic routine we should have the posibility to mix the order of keywords and parameters in the call. At the moment imho all plugins have a strict order of parameters and then keywords. -- ReimarBauer <> Note that there are already three different argument parsers hidden in MoinMoin code. So refactoring should take place first. We do often use in parsers and macros a similiar routine like this one. This is mostly derived from the ImageLink macro. This one is now more generic and I believe a lot of routines could use it. For further enhancement e.g. keyword inheritance unused keywords for the callers routine are saved too. I like to add this to wikiutil. -- ReimarBauer <> minor change for no keywords (kwAllowed=None) so that function could be called with one parameter -- ReimarBauer <> {{{#!python def argParser(args, kwAllowed=None, separator=','): '''returns a dict of given parameters, keywords ''' if args: args = args.split(separator) args = [arg.strip() for arg in args] else: args = [] argc = len(args) kw_count = 0 pp_count = 0 kw = {} # keywords ukw = {} # unused keywords pp = {} # positional parameters for arg in args: if '=' in arg: key, value = arg.split('=', 1) if key.lower() not in kwAllowed: ukw[str(key.lower())] = escape(value, quote=1) continue kw_count += 1 kw[str(key.lower())] = escape(value, quote=1) else: pp_count += 1 pp["%d" % pp_count] = escape(arg, quote=1) return {"keywords": kw, "unused_keywords": ukw, "positional_parameters": pp, "kw_count": kw_count, "pp_count": pp_count} }}} This is an example to show what to do with this arg parser {{{#!python # -*- coding: iso-8859-1 -*- """ Macro - TestParam to test the argParser function @copyright :Reimar Bauer @license: GNU GPL, see COPYING for details. """ from MoinMoin import wikiutil def execute(macro, args): request = macro.request kwAllowed = ['width', 'height', 'alt'] result = wikiutil.argParser(args, kwAllowed) kw_count = result['kw_count'] pp_count = result['pp_count'] request.write('valid keyword_count: %s | ' % kw_count) request.write('parameter_count: %s | ' % pp_count) if kw_count: keywords = result["keywords"] for tag in keywords.keys(): request.write("Keyword: %s, value %s | " % (tag, keywords[tag])) if pp_count: pp = result["positional_parameters"] for tag in pp.keys(): request.write("positional %s parameter: value %s | " % (tag, pp[tag])) ukw = result["unused_keywords"] for tag in ukw.keys(): request.write("unused keyword %s, value %s | " % (tag, ukw[tag])) }}} Example call of a routine using this argParser: {{{XX * {{{[[TestParam]]}}} [[TestParam]] * {{{[[TestParam(a,b)]]}}} [[TestParam(a,b)]] * {{{[[TestParam(a,b,alt='DDD')]]}}} [[TestParam(a,b,alt='DDD')]] * {{{[[TestParam(alt='DDD')]]}}} [[TestParam(alt='DDD')]] * {{{[[TestParam(height=200,width=300,alt='DDD')]]}}} [[TestParam(height=200,width=300,alt='DDD')]] * {{{[[TestParam(a,b,height=200,width=300,alt='DDD')]]}}} [[TestParam(a,b,height=200,width=300,alt='DDD')]] * {{{[[TestParam(a,b,height=200,width=300,alt='DDD',background='yellow')]]}}} [[TestParam(a,b,height=200,width=300,alt='DDD',background='yellow')]] XX}}} Result: {{attachment:argParser.png}} Would be great to have that arg parser. As far as I can see, a lot of people use similar code like above, propably taken from macro/action.py. However, as Reimar said already elsewhere, code dublication is not that fine and everything is in the code above you normally need... -- OliverSiemoneit <> Annotation: Not quite sure whether it is always good to separate between keywords and positional parameters. Maybe my suggestion for a new User.py macro is an example for that (see FeatureRequests/WelcomeNewUserPage). There you can write [[User(homepage)]] and [[User(homepage=Create homepage now)]]. But maybe that's bad coding style..However a separation there would make it more difficult to parse the args. It personally would prefer to rename "args" in "given_args" and "kwAllowed" in "allowed_args". That would be more intuitiv for me. But I don't have an overview on the naming conventions in Moin.. ---- CategoryFeatureImplemented (slightly different implemented in 1.6)