I'm writing a command-line tool that takes a file and modifies its contents. I would like the user interface to be as canonical as possible. So far, I have
$ foobar -h
usage: foobar [-h] [--version] [infile] [outfile]
Some help text.
positional arguments:
infile input LaTeX file (default: stdin)
outfile output LaTeX file (default: stdout)
optional arguments:
-h, --help show this help message and exit
--version, -v show program's version number and exit
but I noticed that tools like sed
always print to stdout
, unless given the -i
parameter in which case the input file is modified in-place. Is this a common Unix convention?
sed command inputfile | sort | uniq -c
. Some programs allow the special file name-
for stdin or stdout when used as input or output file name respectively. In-place editing likesed
's option-i
can be problematic because you might lose your original input in case of an error.-o outFile
, but default to stdout so it can be redirected or piped at the command line.infile
andoutfile
arguments. Simply let read from stdin and write to stdout. The user can use it asprog <file1 >file2
, which would also make clear thatfile2
really is an output, and not another input as withcat file1 file2
, and will save users from inadvertently overwriting files. The-i
option of sed is broken, and you should not bother with it; you can easily achieve the same effect withprog <file >file.new && mv file.new file || rm file.new
.sed -i
may trash the file in case of error wasn't that far-fetched, considering that it was exactly whatperl -i
(which was supposed to be similar tosed -i
) was doing until quite recently. That's why I felt the need to clarify it.