listArgs :: Command -> Maybe [String]
listArgs (Command _ cmd) = result
   where
      result = case (foldl parse init cmd) of
         Just (_, _, args) -> Just (nub $ reverse args)
         Nothing -> Nothing
      -- First part is state machine for parse, second is buffer, third is results
      init = Just ("none", "", [])

      parse Nothing _ = Nothing
      parse (Just carry) c = parse' carry c
      parse' ("none", buffer, xs) '%' = Just ("open one", buffer, xs)
      parse' ("none", buffer, xs) _ = Just ("none", buffer, xs)
      parse' ("open one", buffer, xs) '%' = Just ("open two", buffer, xs)
      parse' ("open one", buffer, xs) _ = Just ("none", buffer, xs)
      parse' ("open two", buffer, xs) '%' = Just ("close one", buffer, xs)
      parse' ("open two", buffer, xs) c = Just ("open two", c:buffer, xs)
      parse' ("close one", buffer, xs) '%' = Just ("none", "", (reverse buffer):xs)
      parse' ("close one", buffer, xs) _ = Nothing
listArgs :: Command -> Maybe [String]
listArgs (Command _ cmd) = result
   where
      result = case (foldl parse init cmd) of
         Just (_, _, args) -> Just (nub $ reverse args)
         Nothing -> Nothing
      -- First part is state machine for parse, second is buffer, third is results
      init = Just ("none", "", [])

      parse Nothing _ = Nothing
      parse (Just carry) c = parse' carry c
      parse' ("none", buffer, xs) '%' = Just ("open one", buffer, xs)
      parse' ("none", buffer, xs) _ = Just ("none", buffer, xs)
      parse' ("open one", buffer, xs) '%' = Just ("open two", buffer, xs)
      parse' ("open one", buffer, xs) _ = Just ("none", buffer, xs)
      parse' ("open two", buffer, xs) '%' = Just ("close one", buffer, xs)
      parse' ("open two", buffer, xs) c = Just ("open two", c:buffer, xs)
      parse' ("close one", buffer, xs) '%' = Just ("none", "", (reverse buffer):xs)
      parse' ("close one", buffer, xs) _ = Nothing
listArgs (Command _ cmd) = result
   where
      result = case (foldl parse init cmd) of
         Just (_, _, args) -> Just (nub $ reverse args)
         Nothing -> Nothing
      -- First part is state machine for parse, second is buffer, third is results
      init = Just ("none", "", [])

      parse Nothing _ = Nothing
      parse (Just carry) c = parse' carry c
      parse' ("none", buffer, xs) '%' = Just ("open one", buffer, xs)
      parse' ("none", buffer, xs) _ = Just ("none", buffer, xs)
      parse' ("open one", buffer, xs) '%' = Just ("open two", buffer, xs)
      parse' ("open one", buffer, xs) _ = Just ("none", buffer, xs)
      parse' ("open two", buffer, xs) '%' = Just ("close one", buffer, xs)
      parse' ("open two", buffer, xs) c = Just ("open two", c:buffer, xs)
      parse' ("close one", buffer, xs) '%' = Just ("none", "", (reverse buffer):xs)
      parse' ("close one", buffer, xs) _ = Nothing