Add argument parser 26/95226/1
authorAdam Malinowski <a.malinowsk2@partner.samsung.com>
Fri, 10 Jun 2016 06:03:49 +0000 (08:03 +0200)
committerAdam Malinowski <a.malinowsk2@partner.samsung.com>
Wed, 2 Nov 2016 12:38:23 +0000 (13:38 +0100)
Change-Id: If96f4671a8d8226dd4c7e0eeaa0ce1740beb36d4

packaging/screenshooter.spec
src/CMakeLists.txt
src/main.c

index f8e38d6..59ec8c9 100644 (file)
@@ -7,6 +7,7 @@ License:        Apache-2.0
 URL:            http://www.tizen.org
 Source0:        %{name}_%{version}.tar.gz
 BuildRequires:  cmake
+BuildRequires:  pkgconfig(popt)
 
 BuildRoot:  %{_tmppath}/%{name}_%{version}-build
 
index 891b200..61fb96d 100644 (file)
@@ -20,6 +20,7 @@ FIND_PACKAGE(PkgConfig)
 
 PKG_CHECK_MODULES(SCREENSHOOTER_DEP
     REQUIRED
+    popt
     )
 
 SET(SCREENSHOOTER_PATH
index 9112e55..ee02998 100644 (file)
  * @brief       Main source file of screenshooter
  */
 
+#include <popt.h>
 #include <stdlib.h>
 
+typedef enum {
+  Capture,
+  Version,
+  Error,
+  Exit
+} Command;
+
+Command parseArguments(int argc, const char **argv, int *width, int *height, char **filename) {
+  Command ret = Capture;
+  poptContext optCon;
+  int c;
+
+  struct poptOption optionsTable[] = {
+    // Commands
+    { "version", 'v', POPT_ARG_NONE, NULL, 'v', "show version of this binary and exit", NULL },
+    // Options
+    { "width", 'w', POPT_ARG_INT, width, 'w', "set width of captured image", NULL },
+    { "height", 'h', POPT_ARG_INT, height, 'h', "set height of captured image", NULL },
+    { "file", 'f', POPT_ARG_STRING, filename, 'f', "file name to store captured image in", NULL },
+    POPT_AUTOHELP
+    { NULL, 0, 0, NULL, 0, NULL, NULL }
+  };
+
+  optCon = poptGetContext(NULL, argc, argv, optionsTable, 0);
+  poptSetOtherOptionHelp(optCon, "command");
+  if (argc < 2) {
+    poptPrintUsage(optCon, stderr, 0);
+    ret = Error;
+  }
+
+  if (ret != Exit) {
+    while ((c = poptGetNextOpt(optCon)) >= 0) {
+      switch (c) {
+        case 'v':
+          ret = Version;
+          break;
+      }
+    }
+
+    if (c < -1) {
+      fprintf(stderr, "%s: %s\n", poptBadOption(optCon, POPT_BADOPTION_NOALIAS), poptStrerror(c));
+      ret = Error;
+    }
+  }
+
+  poptFreeContext(optCon);
+
+  return ret;
+}
+
 int main(int argc, const char **argv) {
-    (void) argc;
-    (void) argv;
+  int width, height, ret = EXIT_SUCCESS;
+  char *filename;
+
+  Command command = parseArguments(argc, argv, &width, &height, &filename);
+  switch (command) {
+    case Capture:
+      // TODO: add cupturing function
+      break;
+    case Version:
+      // TODO: add version function
+      break;
+    case Error:
+      ret = EXIT_FAILURE;
+    case Exit:
+      break;
+  }
 
-    return EXIT_SUCCESS;
+  return ret;
 }