LCOV - code coverage report
Current view: top level - util - getopt.c (source / functions) Hit Total Coverage
Test: GNU Taler exchange coverage report Lines: 8 10 80.0 %
Date: 2021-08-30 06:43:37 Functions: 2 2 100.0 %
Legend: Lines: hit not hit

          Line data    Source code
       1             : /*
       2             :   This file is part of TALER
       3             :   Copyright (C) 2014-2020 Taler Systems SA
       4             : 
       5             :   TALER is free software; you can redistribute it and/or modify it under the
       6             :   terms of the GNU General Public License as published by the Free Software
       7             :   Foundation; either version 3, or (at your option) any later version.
       8             : 
       9             :   TALER is distributed in the hope that it will be useful, but WITHOUT ANY
      10             :   WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
      11             :   A PARTICULAR PURPOSE.  See the GNU General Public License for more details.
      12             : 
      13             :   You should have received a copy of the GNU General Public License along with
      14             :   TALER; see the file COPYING.  If not, see <http://www.gnu.org/licenses/>
      15             : */
      16             : /**
      17             :  * @file getopt.c
      18             :  * @brief Helper functions for parsing Taler-specific command-line arguments
      19             :  * @author Florian Dold
      20             :  */
      21             : #include "platform.h"
      22             : #include "taler_util.h"
      23             : 
      24             : 
      25             : /**
      26             :  * Set an option with an amount from the command line.  A pointer to
      27             :  * this function should be passed as part of the 'struct
      28             :  * GNUNET_GETOPT_CommandLineOption' array to initialize options of
      29             :  * this type.
      30             :  *
      31             :  * @param ctx command line processing context
      32             :  * @param scls additional closure (will point to the `struct TALER_Amount`)
      33             :  * @param option name of the option
      34             :  * @param value actual value of the option as a string.
      35             :  * @return #GNUNET_OK if parsing the value worked
      36             :  */
      37             : static int
      38           2 : set_amount (struct GNUNET_GETOPT_CommandLineProcessorContext *ctx,
      39             :             void *scls,
      40             :             const char *option,
      41             :             const char *value)
      42             : {
      43           2 :   struct TALER_Amount *amount = scls;
      44             : 
      45             :   (void) ctx;
      46           2 :   if (GNUNET_OK !=
      47           2 :       TALER_string_to_amount (value,
      48             :                               amount))
      49             :   {
      50           0 :     fprintf (stderr,
      51             :              _ ("Failed to parse amount in option `%s'\n"),
      52             :              option);
      53           0 :     return GNUNET_SYSERR;
      54             :   }
      55             : 
      56           2 :   return GNUNET_OK;
      57             : }
      58             : 
      59             : 
      60             : /**
      61             :  * Allow user to specify an amount on the command line.
      62             :  *
      63             :  * @param shortName short name of the option
      64             :  * @param name long name of the option
      65             :  * @param argumentHelp help text for the option argument
      66             :  * @param description long help text for the option
      67             :  * @param[out] amount set to the amount specified at the command line
      68             :  */
      69             : struct GNUNET_GETOPT_CommandLineOption
      70           4 : TALER_getopt_get_amount (char shortName,
      71             :                          const char *name,
      72             :                          const char *argumentHelp,
      73             :                          const char *description,
      74             :                          struct TALER_Amount *amount)
      75             : {
      76           4 :   struct GNUNET_GETOPT_CommandLineOption clo = {
      77             :     .shortName =  shortName,
      78             :     .name = name,
      79             :     .argumentHelp = argumentHelp,
      80             :     .description = description,
      81             :     .require_argument = 1,
      82             :     .processor = &set_amount,
      83             :     .scls = (void *) amount
      84             :   };
      85             : 
      86           4 :   return clo;
      87             : }

Generated by: LCOV version 1.14