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, ucrt: bool | None = None) 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

  • ucrt – parse like UCRT

  • 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.

If ucrt is true, this will parse like a modern C runtime. If it is false, then it will parse like msvcrt.dll. If it is None, then it will raise an exception if the two methods disagree.

Note

This does not treat argv[0] specially as described in Microsoft’s documentation, because this function does not have any way of knowing if the first word of s is meant to be used as the program name. If it is, then it should be a valid path name, so it can not contain quotes, so both methods of interpretation will give the same answer.

mslex.split_msvcrt(s: str) List[str][source]

Split a string of command line options like msvcrt.dll does.

Parameters:

s – a string to parse

Returns:

a list of parsed words

This parses arguments the same way CommandLineToArgvW does, except it does not treat argv[0] specially.

Specifically, it is the same as CommandLineToArgvW("foo.exe " + s)[1:]

If the first word of s is a valid command name, then it cannot contain any quotes, so this is the same as CommandLineToArgvW(s)

mslex.split_ucrt(s: str) List[str][source]

Split a string of command line options like UCRT does.

Parameters:

s – a string to parse

Returns:

a list of parsed words

This should compute the same function that is used by a modern windows C runtime library to convert arguments in GetCommandLineW to individual arguments found in argv, except it does not treat argv[0] specially.

see: Parsing C Command Line Arguments

mslex.strip_carets_like_cmd(s: str, check: bool = True) str[source]

Interpret caret escaping like cmd.exe does.

Parameters:
  • s – a command line string

  • check – raise an error on unquoted metacharacters

Returns:

the string with any carets interpreted as an escape character