From 13e6811630284d7f4bf5c5ef83be4de0226f063f Mon Sep 17 00:00:00 2001 From: Stefan Wahren Date: Tue, 15 May 2018 20:20:34 +0200 Subject: [PATCH] mbus-serial-scan: Refactor cmdline parsing --- bin/mbus-serial-scan.c | 73 ++++++++++++++++-------------------------- 1 file changed, 27 insertions(+), 46 deletions(-) diff --git a/bin/mbus-serial-scan.c b/bin/mbus-serial-scan.c index a93d876..4785fba 100755 --- a/bin/mbus-serial-scan.c +++ b/bin/mbus-serial-scan.c @@ -11,6 +11,7 @@ #include #include +#include #include static int debug = 0; @@ -56,58 +57,38 @@ main(int argc, char **argv) char *device; int address, retries = 0; long baudrate = 9600; - int ret; + int opt, ret; - if (argc == 2) + while ((opt = getopt(argc, argv, "db:r:")) != -1) { - device = argv[1]; + switch (opt) + { + case 'd': + debug = 1; + break; + case 'b': + baudrate = atol(optarg); + break; + case 'r': + retries = atoi(optarg); + break; + default: + fprintf(stderr,"usage: %s [-d] [-b BAUDRATE] [-r RETRIES] device\n", + argv[0]); + + return 0; + } } - else if (argc == 3 && strcmp(argv[1], "-d") == 0) - { - debug = 1; - device = argv[2]; - } - else if (argc == 4 && strcmp(argv[1], "-b") == 0) - { - baudrate = atol(argv[2]); - device = argv[3]; - } - else if (argc == 4 && strcmp(argv[1], "-r") == 0) - { - retries = atoi(argv[2]); - device = argv[3]; - } - else if (argc == 5 && strcmp(argv[1], "-d") == 0 && strcmp(argv[2], "-b") == 0) - { - debug = 1; - baudrate = atol(argv[3]); - device = argv[4]; - } - else if (argc == 5 && strcmp(argv[1], "-d") == 0 && strcmp(argv[2], "-r") == 0) - { - debug = 1; - retries = atoi(argv[3]); - device = argv[4]; - } - else if (argc == 6 && strcmp(argv[1], "-b") == 0 && strcmp(argv[3], "-r") == 0) - { - baudrate = atol(argv[2]); - retries = atoi(argv[4]); - device = argv[5]; - } - else if (argc == 7 && strcmp(argv[1], "-d") == 0 && strcmp(argv[2], "-b") == 0 && strcmp(argv[4], "-r") == 0) - { - debug = 1; - baudrate = atol(argv[3]); - retries = atoi(argv[5]); - device = argv[6]; - } - else - { - fprintf(stderr,"usage: %s [-d] [-b BAUDRATE] [-r RETRIES] device\n", argv[0]); + + if (optind >= argc) { + fprintf(stderr,"usage: %s [-d] [-b BAUDRATE] [-r RETRIES] device\n", + argv[0]); + return 0; } + device = argv[optind]; + if ((handle = mbus_context_serial(device)) == NULL) { fprintf(stderr,"Scan failed: Could not initialize M-Bus context: %s\n", mbus_error_str());