Skip to content

Commit

Permalink
Enhanced command-line version of pyright to allow file or directory n…
Browse files Browse the repository at this point in the history
…ames to be passed via stdin if `-` option is used in the command line. This addresses microsoft#5342.
  • Loading branch information
msfterictraut committed Jun 20, 2023
1 parent cd257f7 commit 5568b4d
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 2 deletions.
1 change: 1 addition & 0 deletions docs/command-line.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ Pyright can be run as either a VS Code extension or as a node-based command-line
| --version | Print pyright version and exit |
| --warnings | Use exit code of 1 if warnings are reported |
| -w, --watch | Continue to run and watch for changes (5) |
| - | Read file or directory list from stdin |

(1) If specific files are specified on the command line, it overrides the files or directories specified in the pyrightconfig.json or pyproject.toml file.

Expand Down
23 changes: 21 additions & 2 deletions packages/pyright-internal/src/pyright.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import { timingStats } from './common/timing';

import chalk from 'chalk';
import commandLineArgs, { CommandLineOptions, OptionDefinition } from 'command-line-args';
import * as fs from 'fs';

import { PackageTypeReport, TypeKnownStatus } from './analyzer/packageTypeReport';
import { PackageTypeVerifier } from './analyzer/packageTypeVerifier';
Expand Down Expand Up @@ -218,7 +219,24 @@ async function processArgs(): Promise<ExitStatus> {

// Assume any relative paths are relative to the working directory.
if (args.files && Array.isArray(args.files)) {
options.fileSpecs = args.files;
let fileSpecList = args.files;

// Has the caller indicated that the file list will be supplied by stdin?
if (args.files.length === 1 && args.files[0] === '-') {
try {
const stdText = fs.readFileSync(process.stdin.fd, 'utf-8');
fileSpecList = stdText
.trim()
.split(' ')
.map((s) => s.trim())
.filter((s) => !s);
} catch (e) {
console.error('Invalid file list specified by stdin input.');
return ExitStatus.ParameterError;
}
}

options.fileSpecs = fileSpecList;
options.fileSpecs = options.fileSpecs.map((f) => combinePaths(process.cwd(), f));
} else {
options.fileSpecs = [];
Expand Down Expand Up @@ -729,7 +747,8 @@ function printUsage() {
' --verifytypes <PACKAGE> Verify type completeness of a py.typed package\n' +
' --version Print Pyright version and exit\n' +
' --warnings Use exit code of 1 if warnings are reported\n' +
' -w,--watch Continue to run and watch for changes\n'
' -w,--watch Continue to run and watch for changes\n' +
' - Read files from stdin\n'
);
}

Expand Down

0 comments on commit 5568b4d

Please sign in to comment.