-
Notifications
You must be signed in to change notification settings - Fork 56
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
Can't read a file on standard input #239
Comments
Hi @qubidt, thanks for the suggestion! Yes, this sounds like a good idea if it enables use of Darker in yet another development environment. Since you mentioned ALE, do you know if it assumes a name for an explicit filepath argument which would make it particularly easy to interoperate with Darker? Among the argument names you mentioned, I think (update: Note that Black has an |
If I'm parsing this question correctly, yes. ALE has built in support for many formatters, (among them isort and black). Most of them support a stdin mode (some require some equivalent of a For the ones that can't operate on stream input it seems to write the buffer to a temporary file which it then calls the formatter on: This obviously wouldn't work for darker because we need to know which file's git history to compare against.
Terrific. If I have some time this (or next) week I'll try to work on this, if no one else gets to it first. |
And this would be our best approach, I think. But does ALE then need to be modified in order to add Darker as a supported formatter, along with the name of its
VSCode uses temporary files as well, and Darker 1.3.2 knows to translate a temporary file path like How does ALE name and place the temporary files? |
I think the stdin mode should only be valid when (edit: see discussion about |
Oh, and @qubidt, isn't there the option to do without an This would of course make Darker invocation a bit different from the other tools you mentioned, but on the other hand it would fit Darker's existing CLI better. I mean, if an Examples of happy cases: $ darker --stdin-filepath=src/mypkg/foo.py # missing `PATH`s, assumed same as `--stdin-filepath`
$ darker --stdin-filepath=src/mypkg/foo.py src/mypkg/foo.py # `PATH` equivalent to `--stdin-filepath`
$ darker --stdin src/mypkg/foo.py # unique `PATH`, implied as the input filepath Problematic cases: $ darker --stdin-filepath=src/mypkg/foo.py src/ # `PATH` is a directory, what to do with all files within?
$ darker --stdin-filepath=src/mypkg/foo.py src/mypkg/*.py # `PATH` expands to multiple files, what to do?
$ darker --stdin src/ # what if `PATH` directory has multiple `.py` files? (update: Note that Black has an |
clang-format behaves thusly: $ clang-format # clang-format blocks, waiting for input from stdin
$ clang-format main.cpp # content of main.cpp is formatted and written to stdout
$ clang-format -i main.cpp # main.cpp is formatted in place
$ clang-format < main.cpp # main.cpp is formatted (can't tell if it simply assumes cpp or if it does any
# parsing) and written to stdout
$ clang-format -i --assume-filename test.cpp < main.cpp
error: cannot use -i when reading from stdin.
# all of these cases ignore the stdin and --assume-filename argument
$ clang-format mod.cpp < main.cpp
$ clang-format --assume-filename test.cpp mod.cpp < main.cpp
$ clang-format -i --assume-filename test.cpp mod.cpp < main.cpp prettier: $ prettier < javascript.js
[error] No parser and no file path given, couldn't infer a parser.
$ prettier # I guess prettier detects if stdin is a tty
Usage: prettier [options] [file/dir/glob ...]
...
$ prettier foo.js # formatted written to stdout
$ prettier -w foo.js # formatted in-place
$ prettier --stdin-filepath bar.js < foo.js # formatted js written to stdout
$ prettier -w --stdin-filepath bar.js < foo.js # formatted js written to stdout (-w is ignored,
# whether # the hinted filepath exists or not)
$ prettier --stdin-filepath bar.js baz.js < foo.js # formatted baz.js is written to stdout, stdin is ignored
$ prettier -w --stdin-filepath bar.js baz.js < foo.js # # baz.js is formatted in-place, stdin is ignored To summarize, both tools only read from stdin when no positional-arg paths are provided.
This is why it makes more sense to me to make the parameter explicitly take an argument, which, unlike the
This kind of stuff can be tricky to implement in in argparse tho so maintainability should def be considered when picking a CLI design.
All of these should error out. That's just my immediate observations tho. I'm not super acquainted with either black or darker. (I use them a decent amount but mostly through automated hooks, I rarely work with them thru the CLI or programatically). I'm not strongly opinionated about this and I obviously don't know darker that well so I'll of course defer to you when it comes to the design. Sorry I haven't touched this yet, btw. I got busier and this fell off my plate |
Thanks @qubidt for your detailed examples and reasoning. It all makes sense to me. I think we have two good candidates for the command line design:
$ darker --stdin [...other options...] src/mypkg/foo.py
$ darker --stdin-filepath=src/mypkg/foo.py [...other options but no positional arguments...] (also, I edited both my and your earlier comments to rename (update: Note that Black has an |
Black actually has
So I guess |
In #306, I've been working on the design with The
|
command line | parsed revision range |
---|---|
darker f.py |
HEAD..:WORKTREE: |
With the proposed --stdin --revision=<rev1..rev2> path.py
or --stdin-filename=path.py --revision=<rev1..rev2>
designs, defaults would be:
revision argument | parsed revision range |
---|---|
--revision=HEAD..:STDIN: |
HEAD..:STDIN: |
--revision=..:STDIN: |
HEAD..:STDIN: |
--revision=.. |
HEAD..:STDIN: |
(no --revision= ) |
HEAD..:STDIN: |
--revision=main..:STDIN: |
main..:STDIN: |
--revision=main.. |
main..:STDIN: |
--revision=:WORKTREE:..:STDIN: |
:WORKTREE:..:STDIN: |
--revision=:WORKTREE:.. |
:WORKTREE:..:STDIN: |
--revision=..HEAD |
❗ ERROR ❗ |
Hybrid design
We could actually make defaults work both ways:
--revision=<rev1>..:STDIN: path.py
would imply--stdin [...] path.py
or--stdin-filename=path.py
--stdin [...] path.py
or--stdin-filename=path.py
would replace an emptyrev2
with:STDIN:
, and error out if a differentrev2
was specified
What about the configuration file?
Would we accept stdin = true
or stdin-filename = "path.py"
in pyproject.toml
?
Would those be overridable on the command line with --no-stdin
or --no-stdin-filename
?
Should --revision=..:STDIN: --no-stdin
or --revision=..:STDIN: --no-stdin-filename
cause an error?
Is it possible to use darker on a stdin stream, or if I assume correctly that it's not, could this be implemented? Of course darker relies on having access to the file path and git history to assess which formatted blocks to keep, but this can be handled by taking an explicit filepath as an argument.
Prior art to reference would be clang-format's
--assume-filename
, isort's--filename
, prettier's--stdin-filepath
, among others.The motivation would be for improved integration with tooling (e.g. w/ ALE)
The text was updated successfully, but these errors were encountered: