LCOV - code coverage report
Current view: top level - lib/support - CHIPArgParser.hpp (source / functions) Hit Total Coverage
Test: lcov_final.info Lines: 0 1 0.0 %
Date: 2024-02-15 08:20:41 Functions: 0 2 0.0 %

          Line data    Source code
       1             : /*
       2             :  *
       3             :  *    Copyright (c) 2020 Project CHIP Authors
       4             :  *    Copyright (c) 2017 Nest Labs, Inc.
       5             :  *    All rights reserved.
       6             :  *
       7             :  *    Licensed under the Apache License, Version 2.0 (the "License");
       8             :  *    you may not use this file except in compliance with the License.
       9             :  *    You may obtain a copy of the License at
      10             :  *
      11             :  *        http://www.apache.org/licenses/LICENSE-2.0
      12             :  *
      13             :  *    Unless required by applicable law or agreed to in writing, software
      14             :  *    distributed under the License is distributed on an "AS IS" BASIS,
      15             :  *    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
      16             :  *    See the License for the specific language governing permissions and
      17             :  *    limitations under the License.
      18             :  */
      19             : 
      20             : /**
      21             :  *    @file
      22             :  *      Support functions for parsing command-line arguments.
      23             :  *
      24             :  */
      25             : 
      26             : #pragma once
      27             : 
      28             : #include <lib/core/CHIPCore.h>
      29             : 
      30             : #if CHIP_CONFIG_ENABLE_ARG_PARSER
      31             : 
      32             : #include <stdio.h>
      33             : #include <stdlib.h>
      34             : 
      35             : #ifndef CHIP_CONFIG_NON_POSIX_LONG_OPT
      36             : #define CHIP_CONFIG_NON_POSIX_LONG_OPT 0
      37             : #endif
      38             : 
      39             : namespace chip {
      40             : namespace ArgParser {
      41             : 
      42             : struct OptionSet;
      43             : 
      44             : /**
      45             :  * A function that can be called to handle a set of command line options.
      46             :  */
      47             : typedef bool (*OptionHandlerFunct)(const char * progName, OptionSet * optSet, int id, const char * name, const char * arg);
      48             : 
      49             : /**
      50             :  * A function that can be called to handle any remaining, non-option command line arguments.
      51             :  */
      52             : typedef bool (*NonOptionArgHandlerFunct)(const char * progName, int argc, char * const argv[]);
      53             : 
      54             : /**
      55             :  * Defines the argument requirements for a command line option.
      56             :  */
      57             : enum OptionArgumentType
      58             : {
      59             :     kNoArgument       = 0,
      60             :     kArgumentRequired = 1,
      61             :     kArgumentOptional = 2,
      62             : };
      63             : 
      64             : /**
      65             :  * Defines a command line option.
      66             :  */
      67             : struct OptionDef
      68             : {
      69             :     const char * Name;          /**< Long name for the option */
      70             :     OptionArgumentType ArgType; /**< An enumerated value specifying whether the option takes an argument */
      71             :     uint16_t Id;                /**< An integer id for the option.  If the value falls in the range of
      72             :                                      graphical ASCII characters the value is also used as the short name
      73             :                                      for the option. */
      74             : };
      75             : 
      76             : /**
      77             :  * Defines a group of logically-related and reusable command line options.
      78             :  */
      79             : struct OptionSet
      80             : {
      81             :     OptionHandlerFunct OptionHandler; /**< Pointer to function for processing individual options */
      82             :     OptionDef * OptionDefs;           /**< NULL terminated list of option definitions structures. */
      83             :     const char * HelpGroupName;       /**< Group name under which options appear in help output */
      84             :     const char * OptionHelp;          /**< Help text describing options */
      85             : };
      86             : 
      87             : /**
      88             :  * An OptionSet where the handler is a virtual function.
      89             :  */
      90             : class OptionSetBase : public OptionSet
      91             : {
      92             : public:
      93             :     OptionSetBase();
      94           0 :     virtual ~OptionSetBase() {}
      95             :     virtual bool HandleOption(const char * progName, OptionSet * optSet, int id, const char * name, const char * arg) = 0;
      96             : 
      97             : private:
      98             :     static bool CallHandleFunct(const char * progName, OptionSet * optSet, int id, const char * name, const char * arg);
      99             : };
     100             : 
     101             : bool ParseArgs(const char * progName, int argc, char * const argv[], OptionSet * optSets[]);
     102             : bool ParseArgs(const char * progName, int argc, char * const argv[], OptionSet * optSets[],
     103             :                NonOptionArgHandlerFunct nonOptArgHandler);
     104             : bool ParseArgs(const char * progName, int argc, char * const argv[], OptionSet * optSets[],
     105             :                NonOptionArgHandlerFunct nonOptArgHandler, bool ignoreUnknown);
     106             : 
     107             : bool ParseArgsFromString(const char * progName, const char * argStr, OptionSet * optSets[]);
     108             : bool ParseArgsFromString(const char * progName, const char * argStr, OptionSet * optSets[],
     109             :                          NonOptionArgHandlerFunct nonOptArgHandler);
     110             : bool ParseArgsFromString(const char * progName, const char * argStr, OptionSet * optSets[],
     111             :                          NonOptionArgHandlerFunct nonOptArgHandler, bool ignoreUnknown);
     112             : 
     113             : bool ParseArgsFromEnvVar(const char * progName, const char * varName, OptionSet * optSets[]);
     114             : bool ParseArgsFromEnvVar(const char * progName, const char * varName, OptionSet * optSets[],
     115             :                          NonOptionArgHandlerFunct nonOptArgHandler);
     116             : bool ParseArgsFromEnvVar(const char * progName, const char * varName, OptionSet * optSets[],
     117             :                          NonOptionArgHandlerFunct nonOptArgHandler, bool ignoreUnknown);
     118             : 
     119             : void PrintOptionHelp(OptionSet * optionSets[], FILE * s);
     120             : 
     121             : extern void (*PrintArgError)(const char * msg, ...);
     122             : void DefaultPrintArgError(const char * msg, ...);
     123             : 
     124             : // Utility functions for parsing common argument value types.
     125             : bool ParseBoolean(const char * str, bool & output);
     126             : bool ParseInt(const char * str, uint8_t & output);
     127             : bool ParseInt(const char * str, uint16_t & output);
     128             : bool ParseInt(const char * str, int32_t & output);
     129             : bool ParseInt(const char * str, uint32_t & output);
     130             : bool ParseInt(const char * str, uint64_t & output);
     131             : bool ParseInt(const char * str, uint8_t & output, int base);
     132             : bool ParseInt(const char * str, uint16_t & output, int base);
     133             : bool ParseInt(const char * str, int32_t & output, int base);
     134             : bool ParseInt(const char * str, uint32_t & output, int base);
     135             : bool ParseInt(const char * str, uint64_t & output, int base);
     136             : bool ParseFabricId(const char * str, uint64_t & fabricId, bool allowReserved = false);
     137             : bool ParseSubnetId(const char * str, uint16_t & subnetId);
     138             : bool ParseHexString(const char * hexStr, uint32_t strLen, uint8_t * outBuf, uint32_t outBufSize, uint32_t & outDataLen);
     139             : 
     140             : extern OptionSet ** gActiveOptionSets;
     141             : 
     142             : /**
     143             :  * Common OptionSet for handling informational options (--help, --version).
     144             :  *
     145             :  */
     146             : class HelpOptions : public OptionSetBase
     147             : {
     148             : public:
     149             :     const char * AppName;  /**< The name of the command-line application. */
     150             :     const char * AppUsage; /**< A short string depicting the application's command-line syntax. */
     151             :     const char * AppVersion;
     152             :     const char * AppDesc; /**< A description of the application's purpose/behavior. */
     153             : 
     154             :     HelpOptions(const char * appName, const char * appUsage, const char * appVersion);
     155             :     HelpOptions(const char * appName, const char * appUsage, const char * appVersion, const char * appDesc);
     156             : 
     157             :     void PrintBriefUsage(FILE * s) const;
     158             :     void PrintLongUsage(OptionSet * optSets[], FILE * s) const;
     159             :     void PrintVersion(FILE * s) const;
     160             : 
     161             :     bool HandleOption(const char * progName, OptionSet * optSet, int id, const char * name, const char * arg) override;
     162             : };
     163             : 
     164             : } // namespace ArgParser
     165             : } // namespace chip
     166             : 
     167             : #endif // CHIP_CONFIG_ENABLE_ARG_PARSER

Generated by: LCOV version 1.14