package solutions.tretter.githugandroid data class GitHelpInvocation( val topic: String, val command: String, ) fun parseGitHelpInvocation(command: String): GitHelpInvocation? { val tokens = GitSandboxEngine.tokenizeCommand(command) if (tokens.size >= 2 && tokens[0] == "man") { val topic = when { tokens[1] == "git" -> tokens.getOrNull(2) ?: "git" tokens[1].startsWith("git-") -> tokens[1].removePrefix("git-") tokens[1].startsWith("git") && tokens[1].length > 3 -> tokens[1].removePrefix("git") else -> tokens[1] } .takeIf { it.matches(Regex("[A-Za-z0-9_-]+")) } ?: return null return GitHelpInvocation(topic = topic, command = command) } if (tokens.size < 3 || tokens[0] != "git") return null val topic = when { tokens[1] == "help" -> tokens[2] tokens[1] == "--help" -> tokens[2] tokens.drop(2).any { it == "--help" } -> tokens[1] else -> return null }.takeIf { it.matches(Regex("[A-Za-z0-9_-]+")) } ?: return null return GitHelpInvocation(topic = topic, command = command) }