qmp-shell: Fix example with objects as values
PATCH 1/1 qmp-shell: Fix example with objects as values
The example shown that is suppose to let a user passes an object/array as argument doesn't work. The quotes get removed by shlex.split() and then both JSON parser complains. Fix the example by adding quotes and add examples with boolean and array.
--- scripts/qmp/qmp-shell | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-)
scripts/qmp/qmp-shell +5 -1
@@ -33,7 +33,11 @@ # key=value pairs also support Python or JSON object literal subset notations, # without spaces. Dictionaries/objects {} are supported as are arrays []. # -# example-command arg-name1={'key':'value','obj'={'prop':"value"}} +# JSON: +# example-command arg-name1='{"key":"value","obj":{"prop":"value"}}' +# example-command arg-name1='{"key":"value","obj":[1,true,"three"]}' +# free style: +# example-command arg-name1="{'key':'value',\"obj\":[1,True,\"three\"]}" # # Both JSON and Python formatting should work, including both styles of # string literal quotes. Both paradigms of literal values should work,
Signature · 1 line
Anthony PERARD
Quoted 15 lines · Anthony PERARD
The example shown that is suppose to let a user passes an object/array as argument doesn't work. The quotes get removed by
shlex.split()and then both JSON parser complains. Fix the example by adding quotes and add examples with boolean and array.Signed-off-by Anthony PERARD <[email protected]>--- scripts/qmp/qmp-shell | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-)scripts/qmp/qmp-shell +0
-0
scripts/qmp/qmp-shell line 36
Quoted 4 lines
scripts/qmp/qmp-shell +0
-1@@ -33,7 +33,11 @@ # key=value pairs also support Python or JSON object literal subset notations, # without spaces. Dictionaries/objects {} are supported as are arrays []. # -# example-command arg-name1={'key':'value','obj'={'prop':"value"}}
Yes, I can replicate that with a command such as:
(QEMU) object-add qom-type="rng-random" id="rng1" props={"filename":
"/dev/hwrng"}
That results in:
Error while parsing command line: Expected a key=value pair, got '/dev/hwrng}'
command format: <command-name> [arg-name1=arg1] ... [arg-nameN=argN]
scripts/qmp/qmp-shell line 38
Quoted 3 lines
scripts/qmp/qmp-shell +3
-0+# JSON: +# example-command arg-name1='{"key":"value","obj":{"prop":"value"}}' +# example-command arg-name1='{"key":"value","obj":[1,true,"three"]}'
The usage fix is to have the dictionary quoted, indeed:
(QEMU) object-add qom-type="rng-random" id="rng4" props="{'filename':
'/dev/hwrng'}"
{"return": {}}
scripts/qmp/qmp-shell line 40
Quoted 2 lines
scripts/qmp/qmp-shell +2
-0+# free style: +# example-command arg-name1="{'key':'value',\"obj\":[1,True,\"three\"]}"
I just find the "free style" confusing... IMO how to be more creative about escaping is optional, and adds noise to the most common use case.
Thanks,
- Cleber.
Quoted 4 lines
scripts/qmp/qmp-shell +0
-0# # Both JSON and Python formatting should work, including both styles of # string literal quotes. Both paradigms of literal values should work,
Signature · 4 lines
Cleber Rosa [ Sr Software Engineer - Virtualization Team - Red Hat ] [ Avocado Test Framework - avocado-framework.github.io ] [ 7ABB 96EB 8B46 B94D 5E0F E9BB 657E 8D33 A5F2 09F3 ]
Quoted 13 lines · Cleber Rosa
Quoted 10 lines · Anthony PERARD
The example shown that is suppose to let a user passes an object/array as argument doesn't work. The quotes get removed by
shlex.split()and then both JSON parser complains. Fix the example by adding quotes and add examples with boolean and array.Signed-off-by Anthony PERARD <[email protected]>--- scripts/qmp/qmp-shell | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-)
Quoted 7 lines
scripts/qmp/qmp-shell line 34
Quoted 7 lines
scripts/qmp/qmp-shell +0
-0@@ -33,7 +33,11 @@ # key=value pairs also support Python or JSON object literal subset notations, # without spaces. Dictionaries/objects {} are supported as are arrays [].
The "without spaces" part is outdated. Now we do support spaces as long as the argument is quoted.
Quoted 5 lines
scripts/qmp/qmp-shell line 36
Quoted 2 lines
scripts/qmp/qmp-shell +0
-1# -# example-command arg-name1={'key':'value','obj'={'prop':"value"}}Yes, I can replicate that with a command such as:
Quoted 8 lines
(QEMU) object-add qom-type="rng-random" id="rng1" props={"filename":
"/dev/hwrng"}That results in:
Error while parsing command line: Expected a key=value pair, got '
/dev/hwrng}'
command format: <command-name> [arg-name1=arg1] ... [arg-nameN=argN]
The example has no spaces, and you seem to have a whitespace before "/dev/rng" above.
The problem here is that this worked before started using shlex. qmp-shell from QEMU 3.0 worked:
(QEMU) object-add qom-type=rng-random id="rng1" props={"filename":"/dev/hwrng"} {"return": {}}
Now we get this:
(QEMU) object-add qom-type=rng-random id="rng1" props={"filename":"/dev/hwrng"} {"error": {"class": "GenericError", "desc": "Invalid parameter type for 'props', expected: dict"}}
This means we lost the ability to write JSON without any extra quotes to gain the ability to have arguments with spaces. How hard it would be to fix that?
Quoted 7 lines
scripts/qmp/qmp-shell line 38
Quoted 4 lines
scripts/qmp/qmp-shell +3
-0+# JSON: +# example-command arg-name1='{"key":"value","obj":{"prop":"value"}}' +# example-command arg-name1='{"key":"value","obj":[1,true,"three"]}'The usage fix is to have the dictionary quoted, indeed:
Quoted 8 lines
(QEMU) object-add qom-type="rng-random" id="rng4" props="{'filename':
'/dev/hwrng'}"
{"return": {}}scripts/qmp/qmp-shell line 40
Quoted 2 lines
scripts/qmp/qmp-shell +2
-0+# free style: +# example-command arg-name1="{'key':'value',\"obj\":[1,True,\"three\"]}"I just find the "free style" confusing... IMO how to be more creative about escaping is optional, and adds noise to the most common use case.
The example seems to be a simple demonstration of how escaping works, but maybe we shouldn't encourage people to try weird tricks with escaping. Especially if we want to eventually make unquoted JSON data work again.
Signature · 1 line
Eduardo