Usage

On windows, before a command line argument becomes a char* in a program’s argv, it must be parsed by both cmd.exe, and by CommandLineToArgvW.

For some strings there is no way to quote them so they will parse correctly in both situations.

exception mslex.MSLexError[source]

Class for mslex errors

mslex.join(split_command: List[str], for_cmd: bool = True) str[source]

Quote and concatenate a list of strings for use as a command line in DOS or Windows.

Parameters:
  • split_command – a list of words to be quoted

  • for_cmd – quote it for cmd.exe

Returns:

quoted command string

If for_cmd is true, then this will quote the strings so the result will be parsed correctly by cmd.exe and then by CommandLineToArgvW. If false, then this will quote the strings so the result will be parsed correctly when passed directly to CommandLineToArgvW.

mslex.quote(s: str, for_cmd: bool = True) str[source]

Quote a string for use as a command line argument in DOS or Windows.

Parameters:
  • s – a string to quote

  • for_cmd – quote it for cmd.exe

Returns:

quoted string

If for_cmd is true, then this will quote the strings so the result will be parsed correctly by cmd.exe and then by CommandLineToArgvW. If false, then this will quote the strings so the result will be parsed correctly when passed directly to CommandLineToArgvW.

mslex.split(s: str, like_cmd: bool = True, check: bool = True) List[str][source]

Split a string of command line arguments like DOS and Windows do.

Parameters:
  • s – a string to parse

  • like_cmd – parse it like cmd.exe

  • check – raise an error on unquoted metacharacters

Returns:

a list of parsed words

If like_cmd is true, then this will emulate both cmd.exe and CommandLineToArgvW. Since cmd.exe is a shell, and can run external programs, this function obviously cannot emulate everything it does. However if the string passed in would be parsed by cmd as a quoted literal, without command invocations like &whoami, and without string substitutions like %PATH%, then this function will split it accurately.

f like_cmd is false, then this will split the string like CommandLineToArgvW does.

If check is true, this will raise a ValueError if cmd metacharacters occur in the string without being quoted.