btrfs-progs: introduce helper for parsing args without options
authorDavid Sterba <dsterba@suse.com>
Thu, 14 Jan 2016 09:30:35 +0000 (10:30 +0100)
committerDavid Sterba <dsterba@suse.com>
Thu, 14 Jan 2016 10:07:10 +0000 (11:07 +0100)
All commands should support the "--" option separator. This is
transparently handled by getopt, but we don't use that everywhere.
Introduce a helper for commands that take no options (just the path).
The object file dependencies need to be adjusted a bit.

Signed-off-by: David Sterba <dsterba@suse.com>
Makefile.in
utils.c
utils.h

index 112c3ba..1bd497a 100644 (file)
@@ -70,7 +70,7 @@ objects = ctree.o disk-io.o radix-tree.o extent-tree.o print-tree.o \
          extent-cache.o extent_io.o volumes.o utils.o repair.o \
          qgroup.o raid6.o free-space-cache.o list_sort.o props.o \
          ulist.o qgroup-verify.o backref.o string-table.o task-utils.o \
-         inode.o file.o find-root.o free-space-tree.o
+         inode.o file.o find-root.o free-space-tree.o help.o
 cmds_objects = cmds-subvolume.o cmds-filesystem.o cmds-device.o cmds-scrub.o \
               cmds-inspect.o cmds-balance.o cmds-send.o cmds-receive.o \
               cmds-quota.o cmds-qgroup.o cmds-replace.o cmds-check.o \
@@ -260,9 +260,9 @@ btrfs-%: $(objects) $(libs_static) btrfs-%.o
        $(Q)$(CC) $(CFLAGS) -o $@ $(objects) $@.o $(libs_static) \
                $(LDFLAGS) $(LIBS) $($(subst -,_,$@-libs))
 
-btrfs: $(objects) btrfs.o help.o $(cmds_objects) $(libs_static)
+btrfs: $(objects) btrfs.o $(cmds_objects) $(libs_static)
        @echo "    [LD]     $@"
-       $(Q)$(CC) $(CFLAGS) -o btrfs btrfs.o help.o $(cmds_objects) \
+       $(Q)$(CC) $(CFLAGS) -o btrfs btrfs.o $(cmds_objects) \
                $(objects) $(libs_static) $(LDFLAGS) $(LIBS)
 
 btrfs.static: $(static_objects) btrfs.static.o help.static.o $(static_cmds_objects) $(static_libbtrfs_objects)
diff --git a/utils.c b/utils.c
index ad3ada0..3df8b42 100644 (file)
--- a/utils.c
+++ b/utils.c
@@ -37,6 +37,7 @@
 #include <sys/vfs.h>
 #include <sys/statfs.h>
 #include <linux/magic.h>
+#include <getopt.h>
 
 #include "kerncompat.h"
 #include "radix-tree.h"
@@ -47,6 +48,7 @@
 #include "utils.h"
 #include "volumes.h"
 #include "ioctl.h"
+#include "commands.h"
 
 #ifndef BLKDISCARD
 #define BLKDISCARD     _IO(0x12,119)
@@ -3119,3 +3121,29 @@ int string_is_numerical(const char *str)
                return 0;
        return 1;
 }
+
+/*
+ * Preprocess @argv with getopt_long to reorder options and consume the "--"
+ * option separator.
+ * Unknown short and long options are reported, optionally the @usage is printed
+ * before exit.
+ */
+void clean_args_no_options(int argc, char *argv[], const char * const *usagestr)
+{
+       static const struct option long_options[] = {
+               {NULL, 0, NULL, 0}
+       };
+
+       while (1) {
+               int c = getopt_long(argc, argv, "", long_options, NULL);
+
+               if (c < 0)
+                       break;
+
+               switch (c) {
+               default:
+                       if (usagestr)
+                               usage(usagestr);
+               }
+       }
+}
diff --git a/utils.h b/utils.h
index e522a8c..d53357a 100644 (file)
--- a/utils.h
+++ b/utils.h
@@ -274,6 +274,7 @@ const char *get_argv0_buf(void);
        "-t|--tbytes        show sizes in TiB, or TB with --si"
 
 unsigned int get_unit_mode_from_arg(int *argc, char *argv[], int df_mode);
+void clean_args_no_options(int argc, char *argv[], const char * const *usage);
 int string_is_numerical(const char *str);
 
 __attribute__ ((format (printf, 1, 2)))