LCOV - code coverage report
Current view: top level - Utils - CommandLineArgs.cpp (source / functions) Coverage Total Hit
Test: OdbDesign Test Coverage Lines: 47.9 % 71 34
Test Date: 2026-11-21 03:51:50 Functions: 28.6 % 14 4
Legend: Lines: hit not hit

            Line data    Source code
       1              : #include "CommandLineArgs.h"
       2              : #include "CommandLineArgs.h"
       3              : #include "CommandLineArgs.h"
       4              : #include "CommandLineArgs.h"
       5              : #include "CommandLineArgs.h"
       6              : #include "CommandLineArgs.h"
       7              : #include "CommandLineArgs.h"
       8              : #include "CommandLineArgs.h"
       9              : #include "CommandLineArgs.h"
      10              : #include "CommandLineArgs.h"
      11              : #include "CommandLineArgs.h"
      12              : #include <exception>
      13              : #include <stdexcept>
      14              : #include "str_utils.h"
      15              : #include <sstream>
      16              : #include <iostream>
      17              : #include <cstring>
      18              : 
      19              : 
      20              : namespace Utils
      21              : {
      22            7 :         CommandLineArgs::CommandLineArgs(int argc, char* argv[])
      23              :         {
      24              :                 // save arguments in std::string vector         
      25            7 :                 std::copy(argv, argv + argc, std::back_inserter(m_vecArguments));
      26            7 :                 parse();
      27            7 :         }
      28              : 
      29            0 :         CommandLineArgs::CommandLineArgs(const std::vector<std::string>& vecArgv)
      30            0 :                 : m_vecArguments(vecArgv)
      31              :         {
      32            0 :                 parse();
      33            0 :         }
      34              : 
      35            0 :         bool CommandLineArgs::boolArg(const std::string& name, bool defaultValue) const
      36              :         {
      37            0 :                 auto strB = getArgValue(name);
      38            0 :                 if (strB.length() > 0)
      39              :                 {
      40              :                         bool b;
      41            0 :                         std::istringstream(strB) >> std::boolalpha >> b;
      42            0 :                         return b;
      43              :                 }
      44              : 
      45            0 :                 return defaultValue;
      46            0 :         }
      47              : 
      48            0 :         std::string CommandLineArgs::stringArg(const std::string& name, const std::string& defaultValue) const
      49              :         {
      50            0 :                 auto str = getArgValue(name);
      51            0 :                 if (str.length() > 0)
      52              :                 {
      53            0 :                         return str;
      54              :                 }
      55            0 :                 return defaultValue;
      56            0 :         }
      57              : 
      58            7 :         int CommandLineArgs::intArg(const std::string& name, int defaultValue) const
      59              :         {
      60            7 :                 auto strI = getArgValue(name);
      61            7 :                 if (strI.length() > 0)
      62              :                 {
      63            5 :                         return std::stoi(strI);
      64              :                 }
      65            2 :                 return defaultValue;
      66            7 :         }
      67              : 
      68            0 :         double CommandLineArgs::doubleArg(const std::string& name, double defaultValue) const
      69              :         {
      70            0 :                 if (const auto strD = getArgValue(name); !strD.empty())
      71              :                 {
      72            0 :                         return std::stod(strD);
      73            0 :                 }
      74            0 :                 return defaultValue;
      75              :         }
      76              : 
      77            0 :         std::filesystem::path CommandLineArgs::executable() const
      78              :         {
      79            0 :                 return std::filesystem::path(getArgValue(EXECUTABLE_ARG_NAME));
      80              :         }
      81              : 
      82            0 :         std::filesystem::path CommandLineArgs::executableDirectory() const
      83              :         {
      84            0 :                 return executable().parent_path();
      85              :         }
      86              : 
      87            0 :         std::string CommandLineArgs::executableName() const
      88              :         {
      89            0 :                 return executable().filename().string();
      90              :         }
      91              : 
      92            0 :         bool CommandLineArgs::isWindows() const
      93              :         {
      94            0 :                 auto pos = executableName().find(EXE_EXTENSION);
      95            0 :                 return (pos != std::string::npos && 
      96            0 :                                 pos == executableName().length()-strlen(EXE_EXTENSION));
      97              :         }
      98              : 
      99            0 :         bool CommandLineArgs::isLinux() const
     100              :         {
     101            0 :                 return !isWindows();
     102              :         }
     103              : 
     104            7 :         std::string CommandLineArgs::getArgValue(const std::string& name) const
     105              :         {
     106            7 :                 auto findIt = m_mapArguments.find(name);
     107           11 :                 if (findIt == m_mapArguments.end()) return "";
     108            5 :                 return findIt->second;               
     109              :         }
     110              : 
     111            7 :         void Utils::CommandLineArgs::parse()
     112              :         {
     113           19 :                 for (std::size_t i = 0; i < m_vecArguments.size(); i++)
     114              :                 {
     115           12 :                         auto& arg = m_vecArguments[i];
     116           12 :                         if (arg.length() > 0)
     117              :                         {
     118           12 :                                 if (i == 0)
     119              :                                 {
     120           21 :                                         m_mapArguments["executable"] = arg;
     121              :                                 }
     122            5 :                                 else if (arg[0] == '-' || arg[0] == '/')
     123              :                                 {
     124            5 :                                         Utils::str_ltrim(arg, '-');
     125            5 :                                         Utils::str_ltrim(arg, '/');
     126              : 
     127            5 :                                         std::string nextArg;
     128            5 :                                         if (i + 1 < m_vecArguments.size())
     129              :                                         {
     130            4 :                                                 nextArg = m_vecArguments[i + 1];
     131              :                                         }
     132              : 
     133            5 :                                         if (nextArg.length() > 0 && 
     134            9 :                                                 nextArg[0] != '-' && 
     135            4 :                                                 nextArg[0] != '/')
     136              :                                         {
     137            4 :                                                 m_mapArguments[arg] = nextArg;
     138            4 :                                                 i++;
     139              :                                         }
     140              :                                         else
     141              :                                         {
     142            1 :                                                 m_mapArguments[arg] = "true";
     143              :                                         }
     144              : 
     145              :                                         //if (i + 1 < m_vecArguments.size() &&
     146              :                                         //      m_vecArguments[i + 1].length() > 0 &&
     147              :                                         //      (m_vecArguments[i + 1][0] != '-' || m_vecArguments[i + 1][0] != '/'))
     148              :                                         //{
     149              :                                         //      
     150              :                                         //      m_mapArguments[arg] = m_vecArguments[i+++1];                                            
     151              :                                         //}
     152              :                                         //else
     153              :                                         //{                                             
     154              :                                         //      m_mapArguments[arg] = "true";
     155              :                                         //}                                             
     156            5 :                                 }                               
     157              :                         }
     158              :                 }
     159            7 :         }
     160            0 :         void Utils::CommandLineArgs::printUsage() const
     161              :         {
     162            0 :                 std::cout << getUsageString();
     163            0 :         }
     164              : }
        

Generated by: LCOV version 2.0-1