#!/usr/bin/python
# -*- coding: utf-8 -*-

# prints for each GRASS module list of option keys and descriptions as CSV table

from grass.script import task as gtask
from grass.script.core import get_commands
from grass.exceptions import ScriptError

def get_label(p):
    label = p.get('label')
    if label:
        return label
    return p.get('description')

def main():
    print 'Module' + '€' + 'flags_parms' + '€' + 'Description'
    for cmd in sorted(get_commands()[0]):
        try:
            task = gtask.grassTask(cmd)
        except ScriptError as e:
            import sys
            sys.exit(e)
        if task.get_name() == 'g.parser': # special case
            print task.get_name()
        for f in task.get_options()['flags']:
            if len(f.get('name')) > 1: # skip --overwrite, ...
                continue
            print task.get_name() + '€' + f.get('name') + '€' + get_label(f)
        for p in task.get_options()['params']:
            print task.get_name() + '€' + p.get('name') + '€' + get_label(p)

if __name__ == "__main__":
    main()
