Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add --scalar flag to CLI #280

Merged
merged 2 commits into from
Nov 2, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion MANIFEST.in
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
include LICENSE CHANGELOG.md tox.ini requirements.txt requirements-rtd.txt .coveragerc Makefile pytest.ini .tox-coveragerc
exclude TODO.md codecov.yml .readthedocs.yaml
exclude TODO.md codecov.yml .readthedocs.yaml requirements.in
global-exclude flycheck_*

graft glom/test/data
Expand Down
17 changes: 12 additions & 5 deletions glom/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,14 +43,14 @@
CommandLineError,
UsageError)
from face.utils import isatty
from boltons.iterutils import is_scalar

import glom
from glom import Path, GlomError, Inspect

# TODO: --target-format scalar = unquoted if single value, error otherwise, maybe even don't output newline
# TODO: --default
# TODO: --default?

def glom_cli(target, spec, indent, debug, inspect):
def glom_cli(target, spec, indent, debug, inspect, scalar):
"""Command-line interface to the glom library, providing nested data
access and data restructuring with the power of Python.
"""
Expand All @@ -70,7 +70,11 @@ def glom_cli(target, spec, indent, debug, inspect):

if not indent:
indent = None
print(json.dumps(result, indent=indent, sort_keys=True))

if scalar and is_scalar(result):
print(result, end='')
else:
print(json.dumps(result, indent=indent, sort_keys=True))
return


Expand All @@ -86,7 +90,10 @@ def get_command():

cmd.add('--indent', int, missing=2,
doc='number of spaces to indent the result, 0 to disable pretty-printing')


cmd.add('--scalar', parse_as=True,
doc="if the result is a single value (not a collection), output it"
" without quotes or whitespace, for easier usage in scripts")
cmd.add('--debug', parse_as=True, doc='interactively debug any errors that come up')
cmd.add('--inspect', parse_as=True, doc='interactively explore the data')
return cmd
Expand Down
8 changes: 8 additions & 0 deletions glom/test/test_cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,14 @@ def test_cli_spec_argv_target_stdin_basic(cc):
assert res.stdout == BASIC_OUT


def test_cli_scalar(cc):
res = cc.run(['glom', 'a.b.c', '{"a": {"b": {"c": "d"}}}'])
assert res.stdout == '"d"\n'

res = cc.run(['glom', '--scalar', 'a.b.c', '{"a": {"b": {"c": "d"}}}'])
assert res.stdout == 'd'


def test_cli_spec_target_files_basic(cc, basic_spec_path, basic_target_path):
res = cc.run(['glom', '--indent', '0', '--target-file',
basic_target_path, '--spec-file', basic_spec_path])
Expand Down
Loading