@@ -143,16 +143,15 @@ def validate_extensions(ctx, param, value, extensions=tuple(('.csv', '.json',)))
143143
144144
145145@about .command (cls = AboutCommand ,
146- short_help = 'Collect the inventory of .ABOUT files to a CSV/JSON/XLSX file.' )
146+ short_help = 'Collect the inventory of .ABOUT files to a CSV/JSON/XLSX file or stdout .' )
147147@click .argument ('location' ,
148148 required = True ,
149149 metavar = 'LOCATION' ,
150150 type = click .Path (
151151 exists = True , file_okay = True , dir_okay = True , readable = True , resolve_path = True ))
152152@click .argument ('output' ,
153153 required = True ,
154- metavar = 'OUTPUT' ,
155- type = click .Path (exists = False , dir_okay = False , writable = True , resolve_path = True ))
154+ metavar = 'OUTPUT' )
156155@click .option ('--exclude' ,
157156 multiple = True ,
158157 metavar = 'PATTERN' ,
@@ -176,8 +175,25 @@ def inventory(location, output, exclude, format, quiet, verbose): # NOQA
176175
177176LOCATION: Path to an ABOUT file or a directory with ABOUT files.
178177
179- OUTPUT: Path to the CSV/JSON/XLSX inventory file to create.
178+ OUTPUT: Path to the CSV/JSON/XLSX inventory file to create, or
179+ using '-' to print result on screen/to stdout (Excel-formatted output
180+ cannot be used in stdout).
180181 """
182+ # We are not using type=click.Path() to validate the output location as
183+ # it does not support `-` , which is used to print the result to stdout.
184+ if not output == '-' :
185+ parent_dir = os .path .dirname (output )
186+ if not os .path .exists (parent_dir ):
187+ msg = 'The OUTPUT directory: {parent_dir} does not exist.' .format (** locals ())
188+ msg += '\n Please correct and re-run'
189+ click .echo (msg )
190+ sys .exit (1 )
191+ else :
192+ # Check the format if output is stdout as xlsx format cannot be displayed.
193+ if format == 'excel' :
194+ msg = 'Excel-formatted output cannot be used in stdout.'
195+ click .echo (msg )
196+ sys .exit (0 )
181197 if not quiet :
182198 print_version ()
183199 click .echo ('Collecting inventory from ABOUT files...' )
@@ -188,9 +204,13 @@ def inventory(location, output, exclude, format, quiet, verbose): # NOQA
188204 errors , abouts = collect_inventory (location , exclude )
189205 write_output (abouts = abouts , location = output , format = format )
190206
207+ if output == '-' :
208+ log_file_loc = None
209+ else :
210+ log_file_loc = output + '-error.log'
191211 errors_count = report_errors (
192- errors , quiet , verbose , log_file_loc = output + '-error.log' )
193- if not quiet :
212+ errors , quiet , verbose , log_file_loc )
213+ if not quiet and not output == '-' :
194214 msg = 'Inventory collected in {output}.' .format (** locals ())
195215 click .echo (msg )
196216 sys .exit (errors_count )
0 commit comments