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.
- 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_cmdis true, then this will quote the strings so the result will be parsed correctly bycmd.exeand then byCommandLineToArgvW. If false, then this will quote the strings so the result will be parsed correctly when passed directly toCommandLineToArgvW.
- 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_cmdis true, then this will quote the strings so the result will be parsed correctly bycmd.exeand then byCommandLineToArgvW. If false, then this will quote the strings so the result will be parsed correctly when passed directly toCommandLineToArgvW.
- 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.exeucrt – parse like UCRT
check – raise an error on unquoted metacharacters
- Returns:
a list of parsed words
If
like_cmdis true, then this will emulate bothcmd.exeandCommandLineToArgvW. Sincecmd.exeis 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_cmdis false, then this will split the string likeCommandLineToArgvWdoes.If
checkis true, this will raise aValueErrorif cmd metacharacters occur in the string without being quoted.If
ucrtis true, this will parse like a modern C runtime. If it is false, then it will parse likemsvcrt.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 ofsis 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
sis a valid command name, then it cannot contain any quotes, so this is the same asCommandLineToArgvW(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
GetCommandLineWto individual arguments found inargv, except it does not treatargv[0]specially.