单击 python 我如何从命令行读取 json 之类的参数
click python how can i read json like argument from command line
我的问题是我想执行与 httpie 类似的操作。例子
cli foo:='["http","pie"]'
我在代码中所做的是这样的
@click.command()
@click.argument('options', nargs=1)
def dummy(options):
key, value = options.split(':=')
click.echo({key: json.loads(value)})
为了测试,我在 shell 中尝试了以下命令:
$ ibc dummy star:="{"foo":"bar"}"
和
$ ibc dummy star:={"foo":"bar"}
(我有一台Windows10机)
我得到一个 json.JSONDecodeError
。有人可以帮我调试这个问题吗?
堆栈跟踪如下:
Traceback (most recent call last):
File "C:\Users\rolla\.virtualenvs\infoblox_client-tZbB1-ed\Scripts\ibc-script.py", line 11, in <module>
load_entry_point('infoblox-client', 'console_scripts', 'ibc')()
File "c:\users\rolla\.virtualenvs\infoblox_client-tzbb1-ed\lib\site-packages\click\core.py", line 764, in __call__
return self.main(*args, **kwargs)
File "c:\users\rolla\.virtualenvs\infoblox_client-tzbb1-ed\lib\site-packages\click\core.py", line 717, in main
rv = self.invoke(ctx)
File "c:\users\rolla\.virtualenvs\infoblox_client-tzbb1-ed\lib\site-packages\click\core.py", line 1137, in invoke
return _process_result(sub_ctx.command.invoke(sub_ctx))
File "c:\users\rolla\.virtualenvs\infoblox_client-tzbb1-ed\lib\site-packages\click\core.py", line 956, in invoke
return ctx.invoke(self.callback, **ctx.params)
File "c:\users\rolla\.virtualenvs\infoblox_client-tzbb1-ed\lib\site-packages\click\core.py", line 555, in invoke
return callback(*args, **kwargs)
File "d:\projets\python\infoblox_client\infoblox\scripts\__init__.py", line 31, in dummy
click.echo({key: json.loads(value)})
File "C:\Users\rolla\AppData\Local\Programs\Python\Python37-32\Lib\json\__init__.py", line 348, in loads
return _default_decoder.decode(s)
File "C:\Users\rolla\AppData\Local\Programs\Python\Python37-32\Lib\json\decoder.py", line 337, in decode
obj, end = self.raw_decode(s, idx=_w(s, 0).end())
File "C:\Users\rolla\AppData\Local\Programs\Python\Python37-32\Lib\json\decoder.py", line 353, in raw_decode
obj, end = self.scan_once(s, idx)
json.decoder.JSONDecodeError: Expecting property name enclosed in double quotes: line 1 column 2 (char 1)
这可能是因为您的字符串没有正确转义。例如,当您输入 "{"foo":"bar"}"
时,Python 只会看到一个字符串 "{"
,后跟不明确的字母 foo
,然后是另一个字符串 ":"
,依此类推。通常在 Python 中,如果要在字符串中包含引号,可以将单引号放在双引号中(如 "He said 'hello' to me"
),将双引号放在单引号中(如 'He said "hello" to me'
) ,或使用反斜杠转义引号(如 "he said \"hello\" to me"
)。在这种情况下,您必须使用最后一个选项,因为它在两个地方都需要双引号:
$ibc dummy star:="{\"foo\":\"bar\"}"
我的问题是我想执行与 httpie 类似的操作。例子
cli foo:='["http","pie"]'
我在代码中所做的是这样的
@click.command()
@click.argument('options', nargs=1)
def dummy(options):
key, value = options.split(':=')
click.echo({key: json.loads(value)})
为了测试,我在 shell 中尝试了以下命令:
$ ibc dummy star:="{"foo":"bar"}"
和
$ ibc dummy star:={"foo":"bar"}
(我有一台Windows10机)
我得到一个 json.JSONDecodeError
。有人可以帮我调试这个问题吗?
堆栈跟踪如下:
Traceback (most recent call last):
File "C:\Users\rolla\.virtualenvs\infoblox_client-tZbB1-ed\Scripts\ibc-script.py", line 11, in <module>
load_entry_point('infoblox-client', 'console_scripts', 'ibc')()
File "c:\users\rolla\.virtualenvs\infoblox_client-tzbb1-ed\lib\site-packages\click\core.py", line 764, in __call__
return self.main(*args, **kwargs)
File "c:\users\rolla\.virtualenvs\infoblox_client-tzbb1-ed\lib\site-packages\click\core.py", line 717, in main
rv = self.invoke(ctx)
File "c:\users\rolla\.virtualenvs\infoblox_client-tzbb1-ed\lib\site-packages\click\core.py", line 1137, in invoke
return _process_result(sub_ctx.command.invoke(sub_ctx))
File "c:\users\rolla\.virtualenvs\infoblox_client-tzbb1-ed\lib\site-packages\click\core.py", line 956, in invoke
return ctx.invoke(self.callback, **ctx.params)
File "c:\users\rolla\.virtualenvs\infoblox_client-tzbb1-ed\lib\site-packages\click\core.py", line 555, in invoke
return callback(*args, **kwargs)
File "d:\projets\python\infoblox_client\infoblox\scripts\__init__.py", line 31, in dummy
click.echo({key: json.loads(value)})
File "C:\Users\rolla\AppData\Local\Programs\Python\Python37-32\Lib\json\__init__.py", line 348, in loads
return _default_decoder.decode(s)
File "C:\Users\rolla\AppData\Local\Programs\Python\Python37-32\Lib\json\decoder.py", line 337, in decode
obj, end = self.raw_decode(s, idx=_w(s, 0).end())
File "C:\Users\rolla\AppData\Local\Programs\Python\Python37-32\Lib\json\decoder.py", line 353, in raw_decode
obj, end = self.scan_once(s, idx)
json.decoder.JSONDecodeError: Expecting property name enclosed in double quotes: line 1 column 2 (char 1)
这可能是因为您的字符串没有正确转义。例如,当您输入 "{"foo":"bar"}"
时,Python 只会看到一个字符串 "{"
,后跟不明确的字母 foo
,然后是另一个字符串 ":"
,依此类推。通常在 Python 中,如果要在字符串中包含引号,可以将单引号放在双引号中(如 "He said 'hello' to me"
),将双引号放在单引号中(如 'He said "hello" to me'
) ,或使用反斜杠转义引号(如 "he said \"hello\" to me"
)。在这种情况下,您必须使用最后一个选项,因为它在两个地方都需要双引号:
$ibc dummy star:="{\"foo\":\"bar\"}"