From 141eae4f7a173de22695759a7237042f56beebfe Mon Sep 17 00:00:00 2001 From: Christian Hesse Date: Tue, 6 May 2014 21:47:28 +0200 Subject: use getopt for command line parsing --- mpd-notification.c | 40 +++++++++++++--------------------------- 1 file changed, 13 insertions(+), 27 deletions(-) diff --git a/mpd-notification.c b/mpd-notification.c index 7ad3aa0..0ea2a5c 100644 --- a/mpd-notification.c +++ b/mpd-notification.c @@ -62,35 +62,21 @@ int main(int argc, char ** argv) { printf("%s: Started with PID %d\n", program, getpid()); # endif - for (i = 1; i < argc; i++) { - switch ((int)argv[i][0]) { - case '-': - switch ((int)argv[i][1]) { - case 'p': - mpd_port = atoi(argv[i] + 2); - printf("%s: using port %d\n", program, mpd_port); - break; - case 'h': - fprintf(stderr, "usage: %s [-pPORT] [-h] [-HHOST]\n", program); - return EXIT_SUCCESS; - case 'H': - mpd_host = argv[i] + 2; - if (strlen(mpd_host) == 0) { - fprintf(stderr, "No host given!\n"); - return EXIT_FAILURE; - } - printf("%s: using host %s\n", program, mpd_host); - break; - default: - fprintf(stderr, "%s: unknown option: '%s'\n", program, argv[i]); - return EXIT_FAILURE; - } + /* get command line options */ + while ((i = getopt(argc, argv, "hH:p:")) != -1) + switch (i) { + case 'h': + fprintf(stderr, "usage: %s [-h] [-H HOST] [-p PORT]\n", program); + return EXIT_SUCCESS; + case 'p': + mpd_port = atoi(optarg); + printf("%s: using port %d\n", program, mpd_port); + break; + case 'H': + mpd_host = optarg; + printf("%s: using host %s\n", program, mpd_host); break; - default: - fprintf(stderr, "%s: unknown command line argument: '%s'\n", program, argv[i]); - return EXIT_FAILURE; } - } conn = mpd_connection_new(mpd_host, mpd_port, mpd_timeout); -- cgit v1.2.3-70-g09d2