my issue is that I want to perform something similar to what httpie does. Example
cli foo:='["http","pie"]'
What I have done in my code is this
@click.command()
@click.argument('options', nargs=1)
def dummy(options):
key, value = options.split(':=')
click.echo({key: json.loads(value)})
For test, I have tried the following commands in my shell:
$ ibc dummy star:="{"foo":"bar"}"
and
$ ibc dummy star:={"foo":"bar"}
(I have a Windows 10 machine)
I get a json.JSONDecodeError
. Can somebody helps me to debug this issue?
The stack trace is the following:
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)
This is likely because your strings aren't properly escaped. For example, when you enter "{"foo":"bar"}"
, Python just sees a string "{"
followed by the unclear letters foo
then by another string ":"
and so on. Normally in Python, if you want to include quotes in a string, you can either put single quotes in double quotes (like "He said 'hello' to me"
), put double quotes in single quotes (like 'He said "hello" to me'
), or escape the quotes with a backslash (like "he said \"hello\" to me"
). In this case, you have to use the last option since it's expecting double quotes in both places:
$ibc dummy star:="{\"foo\":\"bar\"}"