43
43
CommandLineError ,
44
44
UsageError )
45
45
from face .utils import isatty
46
+ from boltons .iterutils import is_scalar
46
47
47
48
import glom
48
49
from glom import Path , GlomError , Inspect
49
50
50
- # TODO: --target-format scalar = unquoted if single value, error otherwise, maybe even don't output newline
51
- # TODO: --default
51
+ # TODO: --default?
52
52
53
- def glom_cli (target , spec , indent , debug , inspect ):
53
+ def glom_cli (target , spec , indent , debug , inspect , scalar ):
54
54
"""Command-line interface to the glom library, providing nested data
55
55
access and data restructuring with the power of Python.
56
56
"""
@@ -70,7 +70,11 @@ def glom_cli(target, spec, indent, debug, inspect):
70
70
71
71
if not indent :
72
72
indent = None
73
- print (json .dumps (result , indent = indent , sort_keys = True ))
73
+
74
+ if scalar and is_scalar (result ):
75
+ print (result , end = '' )
76
+ else :
77
+ print (json .dumps (result , indent = indent , sort_keys = True ))
74
78
return
75
79
76
80
@@ -86,7 +90,10 @@ def get_command():
86
90
87
91
cmd .add ('--indent' , int , missing = 2 ,
88
92
doc = 'number of spaces to indent the result, 0 to disable pretty-printing' )
89
-
93
+
94
+ cmd .add ('--scalar' , parse_as = True ,
95
+ doc = "if the result is a single value (not a collection), output it"
96
+ " without quotes or whitespace, for easier usage in scripts" )
90
97
cmd .add ('--debug' , parse_as = True , doc = 'interactively debug any errors that come up' )
91
98
cmd .add ('--inspect' , parse_as = True , doc = 'interactively explore the data' )
92
99
return cmd
0 commit comments