def testParseLeadingTrailingWhitespaces(self): command = " foo bar baz qux " self.assertEqual(["foo", "bar", "baz", "qux"], command_parser.parse_command(command)) command = "\nfoo bar baz qux\n" self.assertEqual(["foo", "bar", "baz", "qux"], command_parser.parse_command(command))
def testParseCommandWithTwoArgsContainingQuotes(self): command = "foo \"bar\" \"qux\"" self.assertEqual(["foo", "bar", "qux"], command_parser.parse_command(command)) command = "foo \"\" \"qux\"" self.assertEqual(["foo", "", "qux"], command_parser.parse_command(command))
def testParseCommandWithTwoArgsContainingBrackets(self): command = "pt foo[1, :] bar[:, 2]" self.assertEqual(["pt", "foo[1, :]", "bar[:, 2]"], command_parser.parse_command(command)) command = "pt foo[] bar[:, 2]" self.assertEqual(["pt", "foo[]", "bar[:, 2]"], command_parser.parse_command(command))
def testParseCommandsWithQuotes(self): command = "inject_value foo \"np.zeros([100, 500])\"" self.assertEqual(["inject_value", "foo", "np.zeros([100, 500])"], command_parser.parse_command(command)) # The pair of double quotes should have been stripped. command = "\"command prefix with spaces\" arg1" self.assertEqual(["command prefix with spaces", "arg1"], command_parser.parse_command(command))
def testParseCommandsWithBrackets(self): command = "pt foo[1, 2, :]" self.assertEqual(["pt", "foo[1, 2, :]"], command_parser.parse_command(command)) command = "pt foo[1, 2, :] -a" self.assertEqual(["pt", "foo[1, 2, :]", "-a"], command_parser.parse_command(command)) command = "inject_value foo [1, 2,:] 0" self.assertEqual(["inject_value", "foo", "[1, 2,:]", "0"], command_parser.parse_command(command))
def testParseNoBracketsOrQuotes(self): command = "" self.assertEqual([], command_parser.parse_command(command)) command = "a" self.assertEqual(["a"], command_parser.parse_command(command)) command = "foo bar baz qux" self.assertEqual(["foo", "bar", "baz", "qux"], command_parser.parse_command(command)) command = "foo bar\tbaz\t qux" self.assertEqual(["foo", "bar", "baz", "qux"], command_parser.parse_command(command))
def _parse_command(self, command): """Parse a command string into prefix and arguments. Args: command: (str) Command string to be parsed. Returns: prefix: (str) The command prefix. args: (list of str) The command arguments (i.e., not including the prefix). """ command = command.strip() if not command: return "", [] command_items = command_parser.parse_command(command) return command_items[0], command_items[1:]
def _parse_command(self, command): """Parse a command string into prefix and arguments. Args: command: (str) Command string to be parsed. Returns: prefix: (str) The command prefix. args: (list of str) The command arguments (i.e., not including the prefix). output_file_path: (str or None) The path to save the screen output to (if any). """ command = command.strip() if not command: return "", [], None command_items = command_parser.parse_command(command) command_items, output_file_path = command_parser.extract_output_file_path( command_items) return command_items[0], command_items[1:], output_file_path
def testParseCommandWithUnmatchedBracket(self): command = "pt foo[1, 2, :" self.assertNotEqual(["pt", "foo[1, 2, :]"], command_parser.parse_command(command))