[Dctrl-tools-devel] Bug#525525: [PATCH] grep-dctrl: add --compact option
Jeff King
peff at peff.net
Thu Nov 3 15:57:32 UTC 2011
I noticed the new "always output a stanza" behavior in dctrl-tools 2.19.
I really dislike it, but I can at least buy the machine-parsing
consistency argument. So I wrote the patch below, which at least adds an
option for human viewers to get the original behavior.
-- >8 --
Subject: [PATCH] grep-dctrl: add --compact option
When only one field is being shown, grep-dctrl's original
behavior was to "compact" the output to remove the
empty-line stanza separator. This made the output more
pleasant for a human viewer, but inconsistent with the
multiple-field case for machine parsing.
Version 2.19 made things more consistent by unconditionally
showing the stanza separator. This patch adds a --compact
flag which lets human viewers restore the old behavior.
Signed-off-by: Jeff King <peff at peff.net>
---
I tried to match the indentation conventions of the file, but it seems
that some lines are indented with pure spaces, and some with tabs. The
size of the spacing is right, but I don't always match the surrounding
lines for tabs vs spaces. I can re-roll the patch if you want me to use
one or the other.
A few alternatives I considered were:
1. Instead of --compact, you could actually manually specify a
stanza separator. I didn't do this, because I want it to
auto-select between the 1-field and many-fields cases (and that
option would be easy to add later if somebody wants it).
2. I considered making --compact the default, which would un-break any
scripts depending on the old behavior that are now broken by 2.19's
behavior. However, in the long run, the non-compact behavior is
more consistent for machine-parsing. I don't know how you want to
handle backwards script-compatibility in general.
3. For multi-line fields like "description", I actually think the
stanzas can help readability. Auto-selecting whether to output
a separator in compact mode based on the field type might be nice.
But that could easily come in a later patch if we want it.
grep-dctrl/grep-dctrl.c | 16 +++++++++++-----
1 files changed, 11 insertions(+), 5 deletions(-)
diff --git a/grep-dctrl/grep-dctrl.c b/grep-dctrl/grep-dctrl.c
index 1526e0a..902a33d 100644
--- a/grep-dctrl/grep-dctrl.c
+++ b/grep-dctrl/grep-dctrl.c
@@ -61,7 +61,8 @@ enum {
OPT_GE,
OPT_MMAP,
OPT_IGN_ERRS,
- OPT_PATTERN
+ OPT_PATTERN,
+ OPT_COMPACT
};
#undef BANNER
@@ -142,6 +143,7 @@ void banner(bool automatic)
{ "ignore-parse-errors", OPT_IGN_ERRS, 0, 0, N_("Ignore parse errors") },
{ "pattern", OPT_PATTERN, N_("PATTERN"), 0, N_("Specify the pattern to search for") },
{ "whole-pkg", 'w', 0, 0, N_("Match only whole package names (this implies -e)") },
+ { "compact", OPT_COMPACT, 0, 0, N_("Omit stanza separator when only one field is shown") },
{ 0 }
};
@@ -211,6 +213,8 @@ struct arguments {
struct ifile fname[MAX_FNAMES];
/**/
size_t show_fields[MAX_FIELDS];
+ /* compact output? */
+ bool compact;
};
#define IS_SHOW_FIELD(field_app_data) ((field_app_data) & 1)
@@ -438,6 +442,10 @@ static error_t parse_opt (int key, char * arg, struct argp_state * state)
debug_message("parse_opt: --config-file", 0);
args->rcname = strdup(arg);
break;
+ case OPT_COMPACT:
+ debug_message("parse_opt: compact", 0);
+ args->compact = 1;
+ break;
default:
return ARGP_ERR_UNKNOWN;
}
@@ -919,10 +927,8 @@ int main (int argc, char * argv[])
show_field(&args, ¶, fa);
}
}
- /* let's see how many users howl in pain after
- deactivating this conditional (see BTS #525525)
-
- if (args.num_show_fields > 1)*/ puts("");
+ if (!args.compact || args.num_show_fields > 1)
+ puts("");
}
fsaf_close(fp);
--
1.7.7.2.4.gfd7b9
More information about the Dctrl-tools-devel
mailing list