From ec9ca6176fd1fa66273f1c9ae0f415791949d792 Mon Sep 17 00:00:00 2001 From: James McKinney <26463+jpmckinney@users.noreply.github.com> Date: Tue, 17 Oct 2023 19:09:52 -0400 Subject: [PATCH] fix: Prompt the user if additional input is expected in csvjoin, csvsql and csvstack, closes #1169 --- CHANGELOG.rst | 1 + csvkit/utilities/csvjoin.py | 10 ++++++---- csvkit/utilities/csvsql.py | 2 +- csvkit/utilities/csvstack.py | 2 +- 4 files changed, 9 insertions(+), 6 deletions(-) diff --git a/CHANGELOG.rst b/CHANGELOG.rst index 7c0943a03..43aafccaf 100644 --- a/CHANGELOG.rst +++ b/CHANGELOG.rst @@ -9,6 +9,7 @@ Unreleased * :doc:`/scripts/csvstat` adds a :code:`--non-nulls` option to only output counts of non-null values. * :doc:`/scripts/csvstat` adds a :code:`--max-precision` option to only output the most decimal places. * feat: Add a :code:`--null-value` option to commands with the :code:`--blanks` option, to convert additional values to NULL. +* fix: Prompt the user if additional input is expected (i.e. if no input file or piped data is provided) in :doc:`/scripts/csvjoin`, :doc:`/scripts/csvsql` and :doc:`/scripts/csvstack`. * fix: No longer errors if a NUL byte occurs in an input file. * Add Python 3.12 support. diff --git a/csvkit/utilities/csvjoin.py b/csvkit/utilities/csvjoin.py index dc7f71fe6..227777d6e 100644 --- a/csvkit/utilities/csvjoin.py +++ b/csvkit/utilities/csvjoin.py @@ -1,8 +1,10 @@ #!/usr/bin/env python +import sys + import agate -from csvkit.cli import CSVKitUtility, match_column_identifier +from csvkit.cli import CSVKitUtility, isatty, match_column_identifier class CSVJoin(CSVKitUtility): @@ -40,14 +42,14 @@ def add_arguments(self): help='Disable type inference when parsing CSV input.') def main(self): + if isatty(sys.stdin) and self.args.input_paths == ['-']: + self.argparser.error('You must provide an input file or piped data.') + self.input_files = [] for path in self.args.input_paths: self.input_files.append(self._open_input_file(path)) - if len(self.input_files) < 1: - self.argparser.error('You must specify at least one file to join.') - if self.args.columns: join_column_names = self._parse_join_column_names(self.args.columns) diff --git a/csvkit/utilities/csvsql.py b/csvkit/utilities/csvsql.py index a7820250c..ae59d671e 100644 --- a/csvkit/utilities/csvsql.py +++ b/csvkit/utilities/csvsql.py @@ -83,7 +83,7 @@ def add_arguments(self): help='Chunk size for batch insert into the table. Requires --insert.') def main(self): - if isatty(sys.stdin) and not self.args.input_paths: + if isatty(sys.stdin) and self.args.input_paths == ['-']: self.argparser.error('You must provide an input file or piped data.') self.input_files = [] diff --git a/csvkit/utilities/csvstack.py b/csvkit/utilities/csvstack.py index 8bb451e49..8179cdc20 100644 --- a/csvkit/utilities/csvstack.py +++ b/csvkit/utilities/csvstack.py @@ -41,7 +41,7 @@ def add_arguments(self): help='Use the filename of each input file as its grouping value. When specified, -g will be ignored.') def main(self): - if isatty(sys.stdin) and not self.args.input_paths: + if isatty(sys.stdin) and self.args.input_paths == ['-']: sys.stderr.write('No input file or piped data provided. Waiting for standard input:\n') has_groups = self.args.groups is not None or self.args.group_by_filenames