Subject: [PATCH 3/7] parseopt: OPT_COLUMN to set struct column_layout.mode

From: Nguyễn Thái Ngọc Duy <[email-masked]>
Date: Tue, 8 Feb 2011 22:22:17 +0700

Signed-off-by: Nguyễn Thái Ngọc Duy <[email-masked]>
---
 column.c        |   18 ++++++++++++++++++
 column.h        |    3 +++
 parse-options.h |    2 ++
 3 files changed, 23 insertions(+), 0 deletions(-)

diff --git a/column.c b/column.c
index 4b92fa5..c4a90d5 100644
--- a/column.c
+++ b/column.c
@@ -175,3 +175,21 @@ void display_columns(const struct column_layout *c, int padding, const char *ind
 		die("BUG: invalid mode %d", c->mode & COL_MODE);
 	}
 }
+
+int parseopt_column_callback(const struct option *opt, const char *arg, int unset)
+{
+	struct column_layout *cp = opt->value;
+	if (unset) {
+		cp->mode = COL_PLAIN;
+		return 0;
+	}
+	if (!arg || !strcmp(arg, "column")) {
+		cp->mode |= COL_COLUMN_FIRST;
+		return 0;
+	}
+	if (!strcmp(arg, "dense")) {
+		cp->mode |= COL_DENSE;
+		return 0;
+	}
+	return error("unsupported style '%s'", arg);
+}
diff --git a/column.h b/column.h
index 34435b0..7f0ae74 100644
--- a/column.h
+++ b/column.h
@@ -17,4 +17,7 @@ struct column_layout {
 
 extern void display_columns(const struct column_layout *c, int padding, const char *indent);
 
+struct option;
+extern int parseopt_column_callback(const struct option *opt, const char *arg, int unset);
+
 #endif
diff --git a/parse-options.h b/parse-options.h
index 31ec5d2..2fed453 100644
--- a/parse-options.h
+++ b/parse-options.h
@@ -145,6 +145,8 @@ struct option {
 #define OPT_COLOR_FLAG(s, l, v, h) \
 	{ OPTION_CALLBACK, (s), (l), (v), "when", (h), PARSE_OPT_OPTARG, \
 		parse_opt_color_flag_cb, (intptr_t)"always" }
+#define OPT_COLUMN(s, l, v, h) \
+	{ OPTION_CALLBACK, (s), (l), (v), "style", (h), PARSE_OPT_OPTARG, parseopt_column_callback }

 /* parse_options() will filter out the processed options and leave the
-- 
1.7.2.2