shwestrick / smlfmt

A custom parser/auto-formatter for Standard ML
MIT License
69 stars 15 forks source link

Unusual command line interface #96

Closed ii8 closed 10 months ago

ii8 commented 10 months ago

Currently there seems to be no way to write the output to a different file. For example:

smlfmt --preview-only a.sml > b.sml

will produce an invalid b.sml that doesn't compile. And further the default behavior without passing arguments is to overwrite the given file after asking for interactive confirmation.

The typical behavior for command line text transformation tools like this is to use stdin and stdout by default, not ask for confirmation and not overwrite by default. Here are some examples:

# The ubiquitous sed tool takes input from a given file or stdin and outputs to stdout, unless `-i` is passed for "in place"
sed s/a/b/ > output.txt
sed s/a/b/ input.txt > output.txt
sed -i s/a/b/ file.txt

# The haskell code formatter ormolu also prints to stdout, `--mode inplace` is used to modify the file in place
ormolu Module.hs > FormattedModule.hs
ormolu --mode inplace Module.hs

# The popular minify tool has a `-o` option to specify an output file instead of stdout
minify script.js > script.min.js
minify -o script.js script.js

# djot markup language processor parses input from stdin, unless input files are listed, and always outputs to stdout.
djot a.dj b.dj c.dj > result.html

smlfmt is at odds with a convention that, in my opinion, is quite reasonable.

shwestrick commented 10 months ago

smlfmt does accept stdin/stdout:

cat a.sml | smlfmt > b.sml

# or, alternatively:
smlfmt < a.sml > b.sml

One nice thing about the in-place-by-default behavior is that it's consistent between .sml and .mlb input. And, in-place updates seem like they would be the common case for a tool like smlfmt. (This is certainly the case for me, but not sure about other people.)

Honestly I don't have a super strong preference either way, but I'm a bit wary of changing things.

ii8 commented 10 months ago

Oh, indeed smlfmt < a.sml > b.sml works but using smlfmt > b.sml and pasting doesn't, that's why I thought it doesn't work in general.

It just seemed a bit strange to me, but if you don't think it should be changed this issue can be closed.

shwestrick commented 10 months ago

Okay sounds good! For now, I think I'd prefer not to change it, just in case someone is relying on the existing interface.

I'll keep in it mind though; if we need to make a backwards-incompatible change in the future anyway, then perhaps this can be changed then too.