summaryrefslogtreecommitdiffstats
path: root/tests/nft-parsing-test.c
diff options
context:
space:
mode:
authorAna Rey <anarey@gmail.com>2014-04-04 11:22:37 +0200
committerPablo Neira Ayuso <pablo@netfilter.org>2014-04-07 10:44:58 +0200
commite64195952667833fbaab4e32992ea9f88c6d8807 (patch)
tree04fd38962314fb4410b90a483337fb0058b483a6 /tests/nft-parsing-test.c
parent14ef99d4a0171b6d5f208516656af14a24b715ed (diff)
tests: Use getopt_long to parse the command-line arguments.
Use getopt_long to parse the command-line arguments and prepare it to add new arguments in next patches. Signed-off-by: Ana Rey <anarey@gmail.com> Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
Diffstat (limited to 'tests/nft-parsing-test.c')
-rw-r--r--tests/nft-parsing-test.c57
1 files changed, 49 insertions, 8 deletions
diff --git a/tests/nft-parsing-test.c b/tests/nft-parsing-test.c
index 1786cb6..a35c064 100644
--- a/tests/nft-parsing-test.c
+++ b/tests/nft-parsing-test.c
@@ -4,6 +4,7 @@
#include <dirent.h>
#include <limits.h>
#include <errno.h>
+#include <getopt.h>
#include <libmnl/libmnl.h> /*nlmsghdr*/
#include <libnftnl/ruleset.h>
@@ -161,7 +162,7 @@ failparsing:
return -1;
}
-int main(int argc, char *argv[])
+static int execute_test(const char *dir_name)
{
DIR *d;
struct dirent *dent;
@@ -169,12 +170,7 @@ int main(int argc, char *argv[])
int ret = 0, exit_code = 0;
struct nft_parse_err *err;
- if (argc != 2) {
- fprintf(stderr, "Usage: %s <directory>\n", argv[0]);
- exit(EXIT_FAILURE);
- }
-
- d = opendir(argv[1]);
+ d = opendir(dir_name);
if (d == NULL) {
perror("opendir");
exit(EXIT_FAILURE);
@@ -193,7 +189,7 @@ int main(int argc, char *argv[])
strcmp(dent->d_name, "..") == 0)
continue;
- snprintf(path, sizeof(path), "%s/%s", argv[1], dent->d_name);
+ snprintf(path, sizeof(path), "%s/%s", dir_name, dent->d_name);
if (strcmp(&dent->d_name[len-4], ".xml") == 0) {
if ((ret = test_xml(path, err)) == 0) {
@@ -219,3 +215,48 @@ int main(int argc, char *argv[])
return 0;
}
+
+static void show_help(const char *name)
+{
+ printf(
+"Usage: %s [option]\n"
+"\n"
+"Options:\n"
+" -d/--dir <directory> Check test files from <directory>.\n"
+"\n",
+ name);
+}
+
+int main(int argc, char *argv[])
+{
+ int val;
+ int ret = 0;
+ int option_index = 0;
+ static struct option long_options[] = {
+ { "dir", required_argument, 0, 'd' },
+ { 0 }
+ };
+
+ if (argc != 3) {
+ show_help(argv[0]);
+ exit(EXIT_FAILURE);
+ }
+
+ while (1) {
+ val = getopt_long(argc, argv, "d:", long_options,
+ &option_index);
+
+ if (val == -1)
+ break;
+
+ switch (val) {
+ case 'd':
+ ret = execute_test(optarg);
+ break;
+ default:
+ show_help(argv[0]);
+ break;
+ }
+ }
+ return ret;
+}