特定参数导致 argparse 错误地解析参数

Specific argument causes argparse to parse arguments incorrectly

我在脚本中使用 python argparse,到目前为止运行良好。但是,将特定文件路径作为参数传递会导致解析器失败。

这是我的 argparse 设置:

parser = argparse.ArgumentParser(prog="writeup_converter.py", description="Takes a folder of Obsidian markdown files and copies them across to a new location, automatically copying any attachments. Options available include converting to a new set of Markdown files, removing and adding prefixes to attachments, and converting for use on a website")

#positional arguments
parser.add_argument("source_folder", help="The folder of markdown files to copy from.")
parser.add_argument("source_attachments", help="The attachments folder in your Obsidian Vault that holds attachments in the notes.")
parser.add_argument("target_folder", help="The place to drop your converted markdown files")
parser.add_argument("target_attachments", help="The place to drop your converted attachments. Must be set as your attachments folder in Obsidian (or just drop them in the root of your vault if you hate yourself)")

#optional flags
parser.add_argument("-r", "--remove_prefix", help="Prefix to remove from all your attachment file paths.")
parser.add_argument("-v", "--verbose", action="store_true", help="Verbose mode. Gives details of which files are being copied. Disabled by default in case of large directories")
parser.add_argument("-w", "--website", help="Use website formatting when files are copied. Files combined into one markdown file with HTML elements, specify the name of this file after the flag")
parser.add_argument("-l", "--asset_rel_path", help="Relative path for site assets e.g. /assets/images/blogs/..., include this or full system path will be added to links")

print(sys.argv)
exit()

#parse arguments
args = parser.parse_args()

我添加了 printexit 用于调试目的。以前当我 运行 使用此配置的程序时,它运行良好 - 但是这组参数会产生一个奇怪的错误:

PS D:\OneDrive\Documents\writeup-converter> python .\writeup_converter.py -v -r Cybersecurity "..\Personal-Vault\Cybersecurity\SESH21-22 Sessions\Shells Session Writeups\" "..\Personal-Vault\Attachments\" "..\Cybersecurity-Notes\Writeups\SESH\DVWA\" "..\Cybersecurity-Notes\Attachments\"
usage: writeup_converter.py [-h] [-r REMOVE_PREFIX] [-v] [-w WEBSITE] [-l ASSET_REL_PATH] source_folder source_attachments target_folder target_attachments
writeup_converter.py: error: the following arguments are required: source_attachments, target_folder, target_attachments

似乎无法识别肯定存在的位置参数。我添加了那些调试语句以根据 Python:

查看参数的状态
['.\writeup_converter.py', '-v', '-r', 'Cybersecurity', '..\Personal-Vault\Cybersecurity\SESH\2021-22 Sessions\Shells Session Writeups" ..\Personal-Vault\Attachments\ ..\Cybersecurity-Notes\Writeups\SESH\DVWA\ ..\Cybersecurity-Notes\Attachments\']

如您所见,四个位置参数已合并为一个。进一步试验我发现第一个参数特别导致了这个问题:

PS D:\OneDrive\Documents\writeup-converter> python .\writeup_converter.py a b c d
['.\writeup_converter.py', 'a', 'b', 'c', 'd']
PS D:\OneDrive\Documents\writeup-converter> python .\writeup_converter.py "a b" b c d
['.\writeup_converter.py', 'a b', 'b', 'c', 'd']
PS D:\OneDrive\Documents\writeup-converter> python .\writeup_converter.py "a\ b" b c d
['.\writeup_converter.py', 'a\ b', 'b', 'c', 'd']
PS D:\OneDrive\Documents\writeup-converter> python .\writeup_converter.py "a\ b" "b" c d
['.\writeup_converter.py', 'a\ b', 'b', 'c', 'd']
PS D:\OneDrive\Documents\writeup-converter> python .\writeup_converter.py "..\Personal-Vault\Cybersecurity\SESH21-22 Sessions\Shells Session Writeups\" "b" c d
['.\writeup_converter.py', '..\Personal-Vault\Cybersecurity\SESH\2021-22 Sessions\Shells Session Writeups" b c d']

如您所见,在使用字符串 "..\Personal-Vault\Cybersecurity\SESH21-22 Sessions\Shells Session Writeups\" 之前,参数被正确解析。我无法找出原因,所以任何想法将不胜感激。 Python 和 CMD.

中都会出现此行为

发布这篇文章大约十秒钟后,由于 Stack Overflow 语法突出显示,我意识到了这个错误 - 路径中的反斜杠正在转义引号。转义这会导致 argparse 行为正确:

PS D:\OneDrive\Documents\writeup-converter> python .\writeup_converter.py -v -r Cybersecurity "..\Personal-Vault\Cybersecurity\SESH21-22 Sessions\Shells Session Writeups\" ..\Personal-Vault\Attachments\ ..\Cybersecurity-Notes\Writeups\SESH\DVWA\ ..\Cybersecurity-Notes\Attachments\
['.\writeup_converter.py', '-v', '-r', 'Cybersecurity', '..\Personal-Vault\Cybersecurity\SESH\2021-22 Sessions\Shells Session Writeups\', '..\Personal-Vault\Attachments\', '..\Cybersecurity-Notes\Writeups\SESH\DVWA\', '..\Cybersecurity-Notes\Attachments\']