Matter SDK Coverage Report
Current view: top level - lib/support - CHIPArgParser.hpp (source / functions) Coverage Total Hit
Test: SHA:cde54a537698f83889a88f28a56a6a1177e51f26 Lines: 0.0 % 1 0
Test Date: 2026-01-19 08:17:36 Functions: 0.0 % 2 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              : #include <stdio.h>
      31              : #include <stdlib.h>
      32              : 
      33              : #ifndef CHIP_CONFIG_NON_POSIX_LONG_OPT
      34              : #define CHIP_CONFIG_NON_POSIX_LONG_OPT 0
      35              : #endif
      36              : 
      37              : namespace chip {
      38              : namespace ArgParser {
      39              : 
      40              : struct OptionSet;
      41              : 
      42              : /**
      43              :  * A function that can be called to handle a set of command line options.
      44              :  */
      45              : typedef bool (*OptionHandlerFunct)(const char * progName, OptionSet * optSet, int id, const char * name, const char * arg);
      46              : 
      47              : /**
      48              :  * A function that can be called to handle any remaining, non-option command line arguments.
      49              :  */
      50              : typedef bool (*NonOptionArgHandlerFunct)(const char * progName, int argc, char * const argv[]);
      51              : 
      52              : /**
      53              :  * Defines the argument requirements for a command line option.
      54              :  */
      55              : enum OptionArgumentType
      56              : {
      57              :     kNoArgument       = 0,
      58              :     kArgumentRequired = 1,
      59              :     kArgumentOptional = 2,
      60              : };
      61              : 
      62              : /**
      63              :  * Defines a command line option.
      64              :  */
      65              : struct OptionDef
      66              : {
      67              :     const char * Name;          /**< Long name for the option */
      68              :     OptionArgumentType ArgType; /**< An enumerated value specifying whether the option takes an argument */
      69              :     uint16_t Id;                /**< An integer id for the option.  If the value falls in the range of
      70              :                                      graphical ASCII characters the value is also used as the short name
      71              :                                      for the option. */
      72              : };
      73              : 
      74              : /**
      75              :  * Defines a group of logically-related and reusable command line options.
      76              :  */
      77              : struct OptionSet
      78              : {
      79              :     OptionHandlerFunct OptionHandler; /**< Pointer to function for processing individual options */
      80              :     OptionDef * OptionDefs;           /**< NULL terminated list of option definitions structures. */
      81              :     const char * HelpGroupName;       /**< Group name under which options appear in help output */
      82              :     const char * OptionHelp;          /**< Help text describing options */
      83              : };
      84              : 
      85              : /**
      86              :  * An OptionSet where the handler is a virtual function.
      87              :  */
      88              : class OptionSetBase : public OptionSet
      89              : {
      90              : public:
      91              :     OptionSetBase();
      92            0 :     virtual ~OptionSetBase() {}
      93              :     virtual bool HandleOption(const char * progName, OptionSet * optSet, int id, const char * name, const char * arg) = 0;
      94              : 
      95              : private:
      96              :     static bool CallHandleFunct(const char * progName, OptionSet * optSet, int id, const char * name, const char * arg);
      97              : };
      98              : 
      99              : bool ParseArgs(const char * progName, int argc, char * const argv[], OptionSet * optSets[]);
     100              : bool ParseArgs(const char * progName, int argc, char * const argv[], OptionSet * optSets[],
     101              :                NonOptionArgHandlerFunct nonOptArgHandler);
     102              : bool ParseArgs(const char * progName, int argc, char * const argv[], OptionSet * optSets[],
     103              :                NonOptionArgHandlerFunct nonOptArgHandler, bool ignoreUnknown);
     104              : 
     105              : bool ParseArgsFromString(const char * progName, const char * argStr, OptionSet * optSets[]);
     106              : bool ParseArgsFromString(const char * progName, const char * argStr, OptionSet * optSets[],
     107              :                          NonOptionArgHandlerFunct nonOptArgHandler);
     108              : bool ParseArgsFromString(const char * progName, const char * argStr, OptionSet * optSets[],
     109              :                          NonOptionArgHandlerFunct nonOptArgHandler, bool ignoreUnknown);
     110              : 
     111              : bool ParseArgsFromEnvVar(const char * progName, const char * varName, OptionSet * optSets[]);
     112              : bool ParseArgsFromEnvVar(const char * progName, const char * varName, OptionSet * optSets[],
     113              :                          NonOptionArgHandlerFunct nonOptArgHandler);
     114              : bool ParseArgsFromEnvVar(const char * progName, const char * varName, OptionSet * optSets[],
     115              :                          NonOptionArgHandlerFunct nonOptArgHandler, bool ignoreUnknown);
     116              : 
     117              : void PrintOptionHelp(OptionSet * optionSets[], FILE * s);
     118              : 
     119              : extern void (*PrintArgError)(const char * msg, ...);
     120              : void DefaultPrintArgError(const char * msg, ...);
     121              : 
     122              : // Utility functions for parsing common argument value types.
     123              : bool ParseBoolean(const char * str, bool & output);
     124              : bool ParseInt(const char * str, uint8_t & output);
     125              : bool ParseInt(const char * str, uint16_t & output);
     126              : bool ParseInt(const char * str, int32_t & output);
     127              : bool ParseInt(const char * str, uint32_t & output);
     128              : bool ParseInt(const char * str, uint64_t & output);
     129              : bool ParseInt(const char * str, uint8_t & output, int base);
     130              : bool ParseInt(const char * str, uint16_t & output, int base);
     131              : bool ParseInt(const char * str, int32_t & output, int base);
     132              : bool ParseInt(const char * str, uint32_t & output, int base);
     133              : bool ParseInt(const char * str, uint64_t & output, int base);
     134              : bool ParseFabricId(const char * str, uint64_t & fabricId, bool allowReserved = false);
     135              : bool ParseSubnetId(const char * str, uint16_t & subnetId);
     136              : bool ParseHexString(const char * hexStr, uint32_t strLen, uint8_t * outBuf, uint32_t outBufSize, uint32_t & outDataLen);
     137              : 
     138              : extern OptionSet ** gActiveOptionSets;
     139              : 
     140              : /**
     141              :  * Common OptionSet for handling informational options (--help, --version).
     142              :  *
     143              :  */
     144              : class HelpOptions : public OptionSetBase
     145              : {
     146              : public:
     147              :     const char * AppName;  /**< The name of the command-line application. */
     148              :     const char * AppUsage; /**< A short string depicting the application's command-line syntax. */
     149              :     const char * AppVersion;
     150              :     const char * AppDesc; /**< A description of the application's purpose/behavior. */
     151              : 
     152              :     HelpOptions(const char * appName, const char * appUsage, const char * appVersion);
     153              :     HelpOptions(const char * appName, const char * appUsage, const char * appVersion, const char * appDesc);
     154              : 
     155              :     void PrintBriefUsage(FILE * s) const;
     156              :     void PrintLongUsage(OptionSet * optSets[], FILE * s) const;
     157              :     void PrintVersion(FILE * s) const;
     158              : 
     159              :     bool HandleOption(const char * progName, OptionSet * optSet, int id, const char * name, const char * arg) override;
     160              : };
     161              : 
     162              : } // namespace ArgParser
     163              : } // namespace chip
        

Generated by: LCOV version 2.0-1