python argparse遇到"$"后停止解析 [英] python argparse stops parsing after it encounters '$'

查看:88
本文介绍了python argparse遇到"$"后停止解析的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用argparse解析命令行

从argparse导入

  ArgumentParserargparser = ArgumentParser(prog ="parse",description ="desc")create.add_argument(-name",dest ="name",required = True,help =元素名称")args = argparser.parse_args()打印(参数) 

当我使用以下命令执行此操作

  python argparser.py --name"input $ output $" 

输出为:

 ('args:',命名空间(name ='input $')) 

预期输出:

 ('args:',命名空间(name ='input $ output $')) 

您能帮我弄清楚我在做什么错吗?为什么argparse在遇到特殊字符后会停止解析?

解决方案

这是因为大多数shell将以$开头的字符串视为变量,并且用双引号引起来时,shell会尝试用其值替换它.

打开终端/控制台并在外壳中键入它(这对bash和fish都有效):

  echo"hi $ test"#尝试对变量'test'进行插值时打印喜echo'hi $ test'#打印hi $ test不对单引号进行插值 

这发生在外壳启动应用程序进程之前.所以我认为,在调用应用程序时,您需要传递用单引号引起来的字符串,或者用反斜杠转义 $ .

  echo"hi \ $ test"#因为$被转义,所以打印hi $ test 

如果要查看Python实际从shell接收的参数作为参数,请直接检查 sys.argv (这是 argparse 和其他类似模块读取命令的地方)行参数).

  import sys打印sys.argv 

在此问题的特定情况下,发生的事情是您的shell解析了 input $ output $ 并尝试对变量 $ output 进行插值,但是没有这样的情况定义了变量,因此将其替换为空字符串.因此,实际上作为参数传递给Python的是 input $ (最后一个美元符号保留在那里,因为它只是一个美元符号,不能是变量的名称).

I am trying to parse a command line using argparse

from argparse import ArgumentParser

argparser = ArgumentParser(prog="parse", description="desc")

create.add_argument("--name",dest="name",required=True,help="Name for element")
args = argparser.parse_args()
print(args)

When I execute this with below command

python argparser.py  --name "input$output$"

The output is:

('args:', Namespace(name='input$'))

Expected Output:

('args:', Namespace(name='input$output$'))

Can you please help figure out what am I doing wrong ? Why argparse stops parsing after encountering a special char?

解决方案

This is because most shells consider strings starting with $ as a variable, and when quoted with double quotes, the shell tries to replace it with its value.

Jut open a terminal/console and type this in the shell (this works in both bash and fish):

echo "hi$test"  # prints hi trying to interpolate the variables 'test'
echo 'hi$test' # prints hi$test no interpolation for single quotes

This happens before the shell starts application processes. So I think when calling your application, you'd need to pass in the string quoted by single quotes, or escape the $ with a backslash.

echo "hi\$test" # prints hi$test since $ is escaped

If you want to see what Python actually receives from the shell as an argument, directly inspect sys.argv (that's where argparse and other modules a like read the command line arguments).

import sys
print sys.argv

In this specific case in the question, what happens is that your shell parses the input$output$ and tries to interpolate the variable $output, but there no such variable defined, so it gets replaced by an empty string. So what is actually being passed to Python as the argument is input$ (the last dollar sign stays in there because it's just a single dollar sign and can not be the name of a variable).

这篇关于python argparse遇到"$"后停止解析的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆