diff mbox

[1/4] btrfs-progs: Introduce get_unit_mode_from_arg for common use

Message ID 1ccb29dccee9a6133a53d0bb8c817e316fa462fd.1440682373.git.zhaolei@cn.fujitsu.com (mailing list archive)
State Superseded
Headers show

Commit Message

Zhaolei Aug. 27, 2015, 1:38 p.m. UTC
We are using separate code for parse unit mode in current code,
result is each command have different argument for unit mode:

 # btrfs filesystem show --help
   ...
   --raw              raw numbers in bytes
   --human-readable   human friendly numbers, base 1024 (default)
   --iec              use 1024 as a base (KiB, MiB, GiB, TiB)
   --si               use 1000 as a base (kB, MB, GB, TB)
   --kbytes           show sizes in KiB, or kB with --si
   --mbytes           show sizes in MiB, or MB with --si
   --gbytes           show sizes in GiB, or GB with --si
   --tbytes           show sizes in TiB, or TB with --si
   ...
 #
 # btrfs filesystem df --help
   ...
   -b|--raw           raw numbers in bytes
   -h|--human-readable
                      human friendly numbers, base 1024 (default)
   -H                 human friendly numbers, base 1000
   --iec              use 1024 as a base (KiB, MiB, GiB, TiB)
   --si               use 1000 as a base (kB, MB, GB, TB)
   -k|--kbytes        show sizes in KiB, or kB with --si
   -m|--mbytes        show sizes in MiB, or MB with --si
   -g|--gbytes        show sizes in GiB, or GB with --si
   -t|--tbytes        show sizes in TiB, or TB with --si
   ...
 #

This patch introduce common function for to arguments for setting
unit, and a common help message, to make every tool in btrfs having
same unit argument.

The merit are:
1: Unify current each tool's arguments for unit
2: Make tools in future easy to implement such argument
3: Changes(enhancement) in common function have effect on all
   relative tools

Signed-off-by: Zhao Lei <zhaolei@cn.fujitsu.com>
---
 utils.c | 70 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
 utils.h | 14 +++++++++++++
 2 files changed, 84 insertions(+)
diff mbox

Patch

diff --git a/utils.c b/utils.c
index aa9c2c9..6af840b 100644
--- a/utils.c
+++ b/utils.c
@@ -2946,3 +2946,73 @@  int arg_copy_path(char *dest, const char *src, int destlen)
 
 	return 0;
 }
+
+unsigned int get_unit_mode_from_arg(int *argc, char *argv[])
+{
+	unsigned int unit_mode = UNITS_DEFAULT;
+	int arg_i;
+	int arg_end;
+
+	for (arg_i = 0; arg_i < *argc; arg_i++) {
+		if (!strcmp(argv[arg_i], "--raw")) {
+			unit_mode = UNITS_RAW;
+			argv[arg_i] = NULL;
+			continue;
+		}
+
+		if (!strcmp(argv[arg_i], "-h") ||
+		    !strcmp(argv[arg_i], "--human-readable")) {
+			unit_mode = UNITS_HUMAN_BINARY;
+			argv[arg_i] = NULL;
+			continue;
+		}
+		if (!strcmp(argv[arg_i], "-H")) {
+			unit_mode = UNITS_HUMAN_DECIMAL;
+			argv[arg_i] = NULL;
+			continue;
+		}
+
+		if (!strcmp(argv[arg_i], "--iec")) {
+			units_set_mode(&unit_mode, UNITS_BINARY);
+			argv[arg_i] = NULL;
+			continue;
+		}
+		if (!strcmp(argv[arg_i], "--si")) {
+			units_set_mode(&unit_mode, UNITS_DECIMAL);
+			argv[arg_i] = NULL;
+			continue;
+		}
+
+		if (!strcmp(argv[arg_i], "--kbytes")) {
+			units_set_base(&unit_mode, UNITS_KBYTES);
+			argv[arg_i] = NULL;
+			continue;
+		}
+		if (!strcmp(argv[arg_i], "--mbytes")) {
+			units_set_base(&unit_mode, UNITS_MBYTES);
+			argv[arg_i] = NULL;
+			continue;
+		}
+		if (!strcmp(argv[arg_i], "--gbytes")) {
+			units_set_base(&unit_mode, UNITS_GBYTES);
+			argv[arg_i] = NULL;
+			continue;
+		}
+		if (!strcmp(argv[arg_i], "--tbytes")) {
+			units_set_base(&unit_mode, UNITS_TBYTES);
+			argv[arg_i] = NULL;
+			continue;
+		}
+	}
+
+	for (arg_i = 0, arg_end = 0; arg_i < *argc; arg_i++) {
+		if (!argv[arg_i])
+			continue;
+		argv[arg_end] = argv[arg_i];
+		arg_end++;
+	}
+
+	*argc = arg_end;
+
+	return unit_mode;
+}
diff --git a/utils.h b/utils.h
index 10d68e9..7b5c72b 100644
--- a/utils.h
+++ b/utils.h
@@ -245,4 +245,18 @@  int btrfs_check_nodesize(u32 nodesize, u32 sectorsize);
 
 const char *get_argv0_buf(void);
 
+#define HELPINFO_OUTPUT_UNIT \
+	"--raw              raw numbers in bytes", \
+	"-h|--human-readable", \
+	"                   human friendly numbers, base 1024 (default)", \
+	"-H                 human friendly numbers, base 1000", \
+	"--iec              use 1024 as a base (KiB, MiB, GiB, TiB)", \
+	"--si               use 1000 as a base (kB, MB, GB, TB)", \
+	"--kbytes           show sizes in KiB, or kB with --si", \
+	"--mbytes           show sizes in MiB, or MB with --si", \
+	"--gbytes           show sizes in GiB, or GB with --si", \
+	"--tbytes           show sizes in TiB, or TB with --si"
+
+unsigned int get_unit_mode_from_arg(int *argc, char *argv[]);
+
 #endif