From patchwork Tue Jan 22 03:03:54 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Laurent Pinchart X-Patchwork-Id: 321 Return-Path: Received: from perceval.ideasonboard.com (perceval.ideasonboard.com [213.167.242.64]) by lancelot.ideasonboard.com (Postfix) with ESMTPS id 7767B60B2D for ; Tue, 22 Jan 2019 04:03:59 +0100 (CET) Received: from pendragon.bb.dnainternet.fi (dfj612yhrgyx302h3jwwy-3.rev.dnainternet.fi [IPv6:2001:14ba:21f5:5b00:ce28:277f:58d7:3ca4]) by perceval.ideasonboard.com (Postfix) with ESMTPSA id DFFE453E for ; Tue, 22 Jan 2019 04:03:58 +0100 (CET) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=ideasonboard.com; s=mail; t=1548126239; bh=UlTyjp7izPD42PX99ZYBFaC/KzXEx7UNdaaRPEh2xxk=; h=From:To:Subject:Date:In-Reply-To:References:From; b=u7f3pKExpgUIdxIFISRCC8AUBRPiWf5Z3XXnXBe/z7bMjJ3Ka+DMKQKzwOB6TXPgP sbYgNAPMDgzytsalP0VJxS2aEwdNZmkOpZ9z1ZMbGoGdc8MVToG3a4tL+mYqb83JWr wuDVAiAvOklKqjgkZm+5ndJD5vPgt3iUs9CAkBPs= From: Laurent Pinchart To: libcamera-devel@lists.libcamera.org Date: Tue, 22 Jan 2019 05:03:54 +0200 Message-Id: <20190122030354.9131-1-laurent.pinchart@ideasonboard.com> X-Mailer: git-send-email 2.19.2 In-Reply-To: <20190121153331.19430-1-niklas.soderlund@ragnatech.se> References: <20190121153331.19430-1-niklas.soderlund@ragnatech.se> MIME-Version: 1.0 Subject: [libcamera-devel] [PATCH] cam: Extract option parser to separate file X-BeenThere: libcamera-devel@lists.libcamera.org X-Mailman-Version: 2.1.23 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Tue, 22 Jan 2019 03:03:59 -0000 And turn it into an OptionsParser object. Signed-off-by: Laurent Pinchart --- Hi Niklas, On top of your cam utility patch, a bit of cleanup and argument parsing refactoring. With this on top the base patch is good for me, feel free to push the combination (in which case please don't forget to add your SoB to this patch) - possibly after waiting for more review. src/cam/{cam.cpp => main.cpp} | 117 +++++---------------- src/cam/meson.build | 3 +- src/cam/options.cpp | 192 ++++++++++++++++++++++++++++++++++ src/cam/options.h | 62 +++++++++++ 4 files changed, 285 insertions(+), 89 deletions(-) rename src/cam/{cam.cpp => main.cpp} (22%) create mode 100644 src/cam/options.cpp create mode 100644 src/cam/options.h diff --git a/src/cam/cam.cpp b/src/cam/main.cpp similarity index 22% rename from src/cam/cam.cpp rename to src/cam/main.cpp index 0f795be78106..22211670c625 100644 --- a/src/cam/cam.cpp +++ b/src/cam/main.cpp @@ -2,139 +2,80 @@ /* * Copyright (C) 2019, Google Inc. * - * main.cpp - cam-ctl a tool to interact with the library + * main.cpp - cam - The libcamera swiss army knife */ -#include -#include #include #include #include #include -#define ARRAY_SIZE(a) (sizeof(a) / sizeof(a[0])) +#include "options.h" -using namespace std; using namespace libcamera; -enum Option { +OptionsParser::Options options; + +enum { OptCamera = 'c', OptHelp = 'h', OptList = 'l', - OptLast = 0, -}; - -struct OptionInfo { - Option id; - const char *name; - const char *arguments; - const char *description; }; -static struct OptionInfo option_info[] = { - { OptCamera, "camera", "", "Specify which camera to operate on" }, - { OptHelp, "help", nullptr, "Display this help message" }, - { OptList, "list", nullptr, "List all cameras" }, - { OptLast, nullptr, nullptr, nullptr }, -}; - -std::map options; - -void usage() +static int parseOptions(int argc, char *argv[]) { - struct OptionInfo *info; + OptionsParser parser; - cout << "Options:" << endl; - for (info = option_info; info->id != OptLast; info++) { - string arg(info->name); + parser.addOption(OptCamera, "Specify which camera to operate on", + "camera", OptionsParser::ArgumentRequired, + "camera"); + parser.addOption(OptHelp, "Display this help message", "help"); + parser.addOption(OptList, "List all cameras", "list"); - if (info->arguments) - arg += string(" ") + info->arguments; + options = std::move(parser.parse(argc, argv)); + if (!options.valid()) + return -EINVAL; - cout << " -" << static_cast(info->id) << " --" << - setw(20) << left << arg << " - " << - info->description << endl; - } -} - -int parseOptions(int argc, char **argv) -{ - char short_options[ARRAY_SIZE(option_info) * 2 + 1]; - struct option long_options[ARRAY_SIZE(option_info)]; - struct OptionInfo *info; - unsigned ids = 0, idl = 0; - - memset(short_options, 0, sizeof(short_options)); - memset(long_options, 0, sizeof(long_options)); - - for (info = option_info; info->id != OptLast; info++) { - short_options[ids++] = info->id; - if (info->arguments) - short_options[ids++] = ':'; - - long_options[idl].name = info->name; - long_options[idl].has_arg = - info->arguments ? required_argument : no_argument; - long_options[idl].flag = 0; - long_options[idl].val = info->id; - idl++; - } - - while (true) { - int c = getopt_long(argc, argv, short_options, long_options, nullptr); - - if (c == -1) - break; - - if (!isalpha(c)) - return EXIT_FAILURE; - - options[static_cast