Skip site navigation (1)Skip section navigation (2)
Date:      Sat, 5 Jul 2014 03:17:57 +0000 (UTC)
From:      Eitan Adler <eadler@FreeBSD.org>
To:        src-committers@freebsd.org, svn-src-all@freebsd.org, svn-src-head@freebsd.org
Subject:   svn commit: r268277 - head/usr.bin/units
Message-ID:  <201407050317.s653HwJV062179@svn.freebsd.org>

next in thread | raw e-mail | index | archive | help
Author: eadler
Date: Sat Jul  5 03:17:57 2014
New Revision: 268277
URL: http://svnweb.freebsd.org/changeset/base/268277

Log:
  units(1): Add 'terse' support
  	terse output is used when calling units from another script.

Modified:
  head/usr.bin/units/units.1
  head/usr.bin/units/units.c

Modified: head/usr.bin/units/units.1
==============================================================================
--- head/usr.bin/units/units.1	Sat Jul  5 02:38:53 2014	(r268276)
+++ head/usr.bin/units/units.1	Sat Jul  5 03:17:57 2014	(r268277)
@@ -26,6 +26,10 @@ If not, print
 .Qo
 Units data file not found
 .Qc
+.It Fl t No , Fl -terse
+Only print the result.  This is used when calling
+.Nm
+from other programs for easy to parse results.
 .It Fl v No , Fl -verbose
 Print the units in the conversion output.
 Be more verbose in general.

Modified: head/usr.bin/units/units.c
==============================================================================
--- head/usr.bin/units/units.c	Sat Jul  5 02:38:53 2014	(r268276)
+++ head/usr.bin/units/units.c	Sat Jul  5 03:17:57 2014	(r268277)
@@ -78,6 +78,7 @@ static char NULLUNIT[] = "";
 static int unitcount;
 static int prefixcount;
 static bool verbose = false;
+static bool terse = false;
 static const char * havestr;
 static const char * wantstr;
 
@@ -657,14 +658,16 @@ showanswer(struct unittype * have, struc
 		printf("conformability error\n");
 		if (verbose)
 			printf("\t%s = ", havestr);
-		else
+		else if (!terse)
 			printf("\t");
 		showunit(have);
-		if (verbose)
-			printf("\t%s = ", wantstr);
-		else
-			printf("\t");
-		showunit(want);
+		if (!terse) {
+			if (verbose)
+				printf("\t%s = ", wantstr);
+			else
+				printf("\t");
+			showunit(want);
+		}
 	}
 	else if (have->offset != want->offset) {
 		if (want->quantity)
@@ -684,12 +687,14 @@ showanswer(struct unittype * have, struc
 		ans = have->factor / want->factor;
 		if (verbose)
 			printf("\t%s = %.8g * %s\n", havestr, ans, wantstr);
-		else
+		else if (terse) 
+			printf("%.8g\n", ans);
+		else 
 			printf("\t* %.8g\n", ans);
 
 		if (verbose)
 			printf("\t%s = (1 / %.8g) * %s\n", havestr, 1/ans,  wantstr);
-		else
+		else if (!terse)
 			printf("\t/ %.8g\n", 1/ans);
 	}
 }
@@ -707,8 +712,9 @@ static struct option longopts[] = {
 	{"help", no_argument, NULL, 'h'},
 	{"file", required_argument, NULL, 'f'},
 	{"quiet", no_argument, NULL, 'q'},
-	{"verbose", no_argument, NULL, 'v'},
+	{"terse", no_argument, NULL, 't'},
 	{"unitsfile", no_argument, NULL, 'U'},
+	{"verbose", no_argument, NULL, 'v'},
 	{"version", no_argument, NULL, 'V'},
 	{ 0, 0, 0, 0 }
 };
@@ -729,7 +735,7 @@ main(int argc, char **argv)
 
 	quiet = false;
 	readfile = false;
-	while ((optchar = getopt_long(argc, argv, "+hf:qvUV", longopts, NULL)) != -1) {
+	while ((optchar = getopt_long(argc, argv, "+hf:qtvUV", longopts, NULL)) != -1) {
 		switch (optchar) {
 		case 'f':
 			readfile = true;
@@ -741,6 +747,9 @@ main(int argc, char **argv)
 		case 'q':
 			quiet = true;
 			break;
+		case 't':
+			terse = true;
+			break;
 		case 'v':
 			verbose = true;
 			break;
@@ -825,5 +834,5 @@ main(int argc, char **argv)
 
 	history_end(inhistory);
 	el_end(el);
-	return(0);
+	return (0);
 }



Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?201407050317.s653HwJV062179>