diff: add support for --help and --version

Add support for --help and --version to be compatible with gnu diff.

gnu diff --help writes to stdout, do the same to be compatible

Reviewed by:	bapt, pstef, debrup, Pau Amma
Sponsored by:	Klara Inc.
Differential Revision:	https://reviews.freebsd.org/D34508
This commit is contained in:
Tom Jones 2022-03-10 16:15:39 +00:00
parent 0784121c96
commit 8cf449db88
2 changed files with 35 additions and 5 deletions

View File

@ -30,7 +30,7 @@
.\" @(#)diff.1 8.1 (Berkeley) 6/30/93
.\" $FreeBSD$
.\"
.Dd June 19, 2020
.Dd March 10, 2022
.Dt DIFF 1
.Os
.Sh NAME
@ -207,6 +207,9 @@
.Op Fl -width
.Fl y | Fl -side-by-side
.Ar file1 file2
.Nm diff
.Op Fl -help
.Op Fl -version
.Sh DESCRIPTION
The
.Nm
@ -282,6 +285,8 @@ Identical output to that of the
flag, but in reverse order.
It cannot be digested by
.Xr ed 1 .
.It Fl -help
This option prints a summary to stdout and exits with status 0.
.It Fl n
Produces a script similar to that of
.Fl e ,
@ -308,6 +313,8 @@ However, unlike with
.Fl c ,
all lines to be changed (added and/or removed) are present in
a single section.
.It Fl -version
This option prints a version string to stdout and exits with status 0.
.It Fl y Fl -side-by-side
Output in two columns with a marker between them.
The marker can be one
@ -655,6 +662,12 @@ Differences were found.
.It >1
An error occurred.
.El
.Pp
The
.Fl -help
and
.Fl -version
options exit with a status of 0.
.Sh EXAMPLES
Compare
.Pa old_dir

View File

@ -38,8 +38,10 @@ __FBSDID("$FreeBSD$");
#include "diff.h"
#include "xmalloc.h"
static const char diff_version[] = "FreeBSD diff 20220309";
bool lflag, Nflag, Pflag, rflag, sflag, Tflag, cflag;
bool ignore_file_case, suppress_common, color, noderef;
static bool help = false;
int diff_format, diff_context, status;
int tabsize = 8, width = 130;
static int colorflag = COLORFLAG_NEVER;
@ -58,11 +60,13 @@ enum {
OPT_IGN_FN_CASE,
OPT_NO_IGN_FN_CASE,
OPT_NORMAL,
OPT_HELP,
OPT_HORIZON_LINES,
OPT_CHANGED_GROUP_FORMAT,
OPT_SUPPRESS_COMMON,
OPT_COLOR,
OPT_NO_DEREFERENCE,
OPT_VERSION,
};
static struct option longopts[] = {
@ -97,6 +101,7 @@ static struct option longopts[] = {
{ "exclude-from", required_argument, 0, 'X' },
{ "side-by-side", no_argument, NULL, 'y' },
{ "ignore-file-name-case", no_argument, NULL, OPT_IGN_FN_CASE },
{ "help", no_argument, NULL, OPT_HELP},
{ "horizon-lines", required_argument, NULL, OPT_HORIZON_LINES },
{ "no-dereference", no_argument, NULL, OPT_NO_DEREFERENCE},
{ "no-ignore-file-name-case", no_argument, NULL, OPT_NO_IGN_FN_CASE },
@ -106,6 +111,7 @@ static struct option longopts[] = {
{ "changed-group-format", required_argument, NULL, OPT_CHANGED_GROUP_FORMAT},
{ "suppress-common-lines", no_argument, NULL, OPT_SUPPRESS_COMMON },
{ "color", optional_argument, NULL, OPT_COLOR },
{ "version", no_argument, NULL, OPT_VERSION},
{ NULL, 0, 0, '\0'}
};
@ -294,6 +300,10 @@ main(int argc, char **argv)
diff_format = D_GFORMAT;
group_format = optarg;
break;
case OPT_HELP:
help = true;
usage();
break;
case OPT_HORIZON_LINES:
break; /* XXX TODO for compatibility with GNU diff3 */
case OPT_IGN_FN_CASE:
@ -335,6 +345,9 @@ main(int argc, char **argv)
rflag = true;
noderef = true;
break;
case OPT_VERSION:
printf("%s\n", diff_version);
exit(0);
default:
usage();
break;
@ -569,9 +582,9 @@ print_status(int val, char *path1, char *path2, const char *entry)
}
static void
usage(void)
usage()
{
(void)fprintf(stderr,
(void)fprintf(help ? stdout : stderr,
"usage: diff [-aBbdilpTtw] [-c | -e | -f | -n | -q | -u] [--ignore-case]\n"
" [--no-ignore-case] [--normal] [--strip-trailing-cr] [--tabsize]\n"
" [-I pattern] [-F pattern] [-L label] file1 file2\n"
@ -590,9 +603,13 @@ usage(void)
" [--ignore-blank-lines] [--ignore-case] [--minimal]\n"
" [--no-ignore-file-name-case] [--strip-trailing-cr]\n"
" [--suppress-common-lines] [--tabsize] [--text] [--width]\n"
" -y | --side-by-side file1 file2\n");
" -y | --side-by-side file1 file2\n"
" diff [--help] [--version]\n");
exit(2);
if (help)
exit(0);
else
exit(2);
}
static void