import sys

from graphene.traversal import *
from graphene.commands.command import Command
from graphene.utils import PrettyPrinter

class MatchCommand(Command):
    def __init__(self, node_chain, query_chain, return_chain):
        self.nc = node_chain
        self.qc = query_chain or ()
        self.rc = return_chain or ()

    def __repr__(self):
        lst = ["\t%s" % chain for chain in self.nc]
        return "[Match\n%s\n]" % "\n".join(lst)

    def execute(self, storage_manager, output=sys.stdout):
        # TODO: handle relationships, passes a True argument for node flag
        first_match = self.nc[0]
        alias = first_match.name
        type_data, type_schema = storage_manager.get_node_data(first_match.type)
        qc = Query.parse_chain(storage_manager, self.qc, type_schema, alias)

        if len(self.rc) == 0:
            return_filter = lambda x: x
        else:
            indexes = []
            schema_names = [name for tt, name, ttype in type_schema]
            for r in self.rc:
                ident, name = r
                if (ident == alias or ident is None) and name in schema_names:
                    indexes.append(schema_names.index(name))
            return_filter = lambda l: [l[i] for i in indexes]

        header = return_filter([name.upper() for tt, name, ttype in type_schema])
        iterator = NodeIterator(storage_manager, type_data, type_schema, alias, qc)
        values = []
        for node in iterator:
            values.append(return_filter(node.properties))
        if len(values) == 0:
            output.write("No nodes found.\n")
            return []
        PrettyPrinter.print_table(values, header, output)
        return values

@@ -827,32 +827,107 @@
 (self.rc) == 0:%0A
+            # If there's no return statement, we do nothing to the columns%0A
             retu
@@ -1140,276 +1140,1035 @@
     
-if (ident == alias or ident is None) and name in schema_names:%0A                    indexes.append(schema_names.index(name))%0A            return_filter = lambda l: %5Bl%5Bi%5D for i in indexes%5D%0A%0A        header = return_filter(%5Bname.upper() for tt, name, ttype in type_schema%5D)
+# If there is an identifier given (e.g. %60MATCH (a:A)%60), then we%0A                # check that if one was given with the return query that it%0A                # matches the one we are looking at with the current schema.%0A                # That is, if our selection is %60MATCH (a:A)%60, where A has fields%0A                # b or c, then %60RETURN b, c%60 and %60RETURN a.b, a.c%60 both work and%0A                # do the same thing, but %60RETURN a%60 and %60RETURN t.b%60 do not.%0A                if (ident == alias or ident is None) and name in schema_names:%0A                    indexes.append(schema_names.index(name))%0A            return_filter = lambda l: %5Bl%5Bi%5D for i in indexes%5D%0A%0A        # Filter out the header columns with respect to the RETURN clause, if%0A        # one was provided%0A        header = return_filter(%5Bname.upper() for tt, name, ttype in type_schema%5D)%0A%0A        # We use a NodeIterator here to loop through the table. TODO: Determine%0A        # whether we want to put return filtering in the iterator or just do it%0A        # afterwards
 %0A   
@@ -2232,24 +2232,56 @@
 type_schema,
+%0A                               
  alias, qc)%0A
@@ -2280,16 +2280,17 @@
 as, qc)%0A
+%0A
         

Add comments to MATCH command logic