This seems like you're mashing two different functions together. Instead of making this function accept a boolean argument that controls whether it returns a command or a list of directories, can you make two different functions, one of which returns a command, and one of which returns a list of directories?