首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >特定的参数导致argument解析不正确地解析参数。

特定的参数导致argument解析不正确地解析参数。
EN

Stack Overflow用户
提问于 2021-12-27 21:22:28
回答 1查看 31关注 0票数 0

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

以下是我的Here解析设置:

代码语言:javascript
复制
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。以前,当我使用此配置运行程序时,它运行得很好--但是这组参数会产生一个奇怪的错误:

代码语言:javascript
复制
PS D:\OneDrive\Documents\writeup-converter> 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\"
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:

代码语言:javascript
复制
['.\\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\\']

正如您所看到的,这四个位置参数已经合并为一个。通过进一步的实验,我发现第一个论点具体导致了这个问题:

代码语言:javascript
复制
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\SESH\2021-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\SESH\2021-22 Sessions\Shells Session Writeups\"之前,将正确地解析参数。我想不出原因,所以任何想法都会很感激。这种行为发生在Python和CMD中。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2021-12-27 21:25:32

在发布这篇文章的大约10秒后,我意识到了这个错误,这要归功于堆栈溢出语法的突出显示--路径中的反斜杠是在转义引号。对此进行转义,将导致this解析的行为正确:

代码语言:javascript
复制
PS D:\OneDrive\Documents\writeup-converter> 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\
['.\\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\\']
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/70500553

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档