diff options
author | David Sterba <dsterba@suse.com> | 2016-01-14 10:30:35 +0100 |
---|---|---|
committer | David Sterba <dsterba@suse.com> | 2016-01-14 11:07:10 +0100 |
commit | a6cc8ea10ad84e4b7d1303ed048e644f0dd7ae04 (patch) | |
tree | 028fb30b5e2bcaac84f849b441ca9c3284371fe7 /utils.c | |
parent | c26dc1ef339c373c59a7c1626bb21d64811476ee (diff) |
btrfs-progs: introduce helper for parsing args without options
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>
Diffstat (limited to 'utils.c')
-rw-r--r-- | utils.c | 28 |
1 files changed, 28 insertions, 0 deletions
@@ -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); + } + } +} |