Skip to content

Commit e92b1a1

Browse files
Updating Commandline Options
1 parent f6f5e88 commit e92b1a1

File tree

3 files changed

+151
-103
lines changed

3 files changed

+151
-103
lines changed

Include/runcpp2/runcpp2.hpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,10 @@ namespace runcpp2
1212
enum class CmdOptions
1313
{
1414
NONE,
15-
RESET_DEPENDENCIES,
15+
RESET_CACHE,
1616
RESET_USER_CONFIG,
1717
EXECUTABLE,
18+
HELP,
1819
COUNT
1920
};
2021

@@ -38,5 +39,4 @@ namespace runcpp2
3839
const std::vector<std::string>& runArgs);
3940
}
4041

41-
42-
#endif
42+
#endif

Src/runcpp2/main.cpp

Lines changed: 66 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -2,26 +2,33 @@
22
#include "runcpp2/StringUtil.hpp"
33
#include "runcpp2/runcpp2.hpp"
44

5+
//#include "TupleHash.hpp"
6+
57
#include "ssLogger/ssLog.hpp"
68

7-
int ParseArgs( const std::unordered_map<std::string, runcpp2::OptionInfo>& optionsMap,
9+
int ParseArgs( const std::unordered_map<std::string, runcpp2::OptionInfo>& longOptionsMap,
10+
const std::unordered_map<std::string, const runcpp2::OptionInfo&>& shortOptionsMap,
811
std::unordered_map<runcpp2::CmdOptions, std::string>& outOptions,
912
int argc,
1013
char* argv[])
1114
{
1215
int currentArgIndex = 0;
13-
runcpp2::CmdOptions currentOption = runcpp2::CmdOptions::NONE;
16+
runcpp2::CmdOptions optionForCapturingValue = runcpp2::CmdOptions::NONE;
1417

1518
for(int i = 1; i < argc; ++i)
1619
{
20+
std::string currentArg = std::string(argv[i]);
21+
1722
//Storing value for last option
18-
if(currentOption != runcpp2::CmdOptions::NONE)
23+
if(optionForCapturingValue != runcpp2::CmdOptions::NONE)
1924
{
20-
if(optionsMap.find(std::string(argv[i])) != optionsMap.end())
25+
//If the current argument matches one of the options, error out
26+
if( longOptionsMap.count(currentArg) || shortOptionsMap.count(currentArg))
2127
{
22-
for(auto it = optionsMap.begin(); it != optionsMap.end(); ++it)
28+
//Find the string for the option to error out
29+
for(auto it = longOptionsMap.begin(); it != longOptionsMap.end(); ++it)
2330
{
24-
if(it->second.Option == currentOption)
31+
if(it->second.Option == optionForCapturingValue)
2532
{
2633
ssLOG_ERROR("Missing value for option: " << it->first);
2734
return -1;
@@ -31,35 +38,44 @@ int ParseArgs( const std::unordered_map<std::string, runcpp2::OptionInfo>& opti
3138
return -1;
3239
}
3340

34-
outOptions[currentOption] = std::string(argv[i]);
35-
currentOption = runcpp2::CmdOptions::NONE;
41+
outOptions[optionForCapturingValue] = currentArg;
42+
optionForCapturingValue = runcpp2::CmdOptions::NONE;
3643
currentArgIndex = i;
3744
ssLOG_DEBUG("currentArgIndex: " << currentArgIndex);
3845
ssLOG_DEBUG("argv: " << argv[i]);
3946
continue;
4047
}
4148

4249
//Checking for options
43-
if(optionsMap.find(std::string(argv[i])) != optionsMap.end())
50+
if(longOptionsMap.count(currentArg) || shortOptionsMap.count(currentArg))
4451
{
4552
currentArgIndex = i;
4653
ssLOG_DEBUG("currentArgIndex: " << currentArgIndex);
4754
ssLOG_DEBUG("argv: " << argv[i]);
4855

49-
static_assert( (int)runcpp2::CmdOptions::COUNT == 4,
56+
static_assert( (int)runcpp2::CmdOptions::COUNT == 5,
5057
"Add a case for the new runcpp2_CmdOptions");
5158

52-
if(optionsMap.at(std::string(argv[i])).HasValue)
59+
const runcpp2::OptionInfo& currentInfo = longOptionsMap.count(currentArg) ?
60+
longOptionsMap.at(currentArg) :
61+
shortOptionsMap.at(currentArg);
62+
63+
if(currentInfo.HasValue)
5364
{
54-
currentOption = optionsMap.at(std::string(argv[i])).Option;
65+
optionForCapturingValue = currentInfo.Option;
5566
continue;
5667
}
5768
else
5869
{
59-
outOptions[optionsMap.at(std::string(argv[i])).Option] = "";
70+
outOptions[currentInfo.Option] = "";
6071
continue;
6172
}
6273
}
74+
else if(!currentArg.empty() && currentArg[0] == '-')
75+
{
76+
ssLOG_ERROR("Invalid option: " << currentArg);
77+
return -1;
78+
}
6379
else
6480
break;
6581
}
@@ -74,12 +90,14 @@ int main(int argc, char* argv[])
7490
//Parse command line options
7591
int currentArgIndex = 0;
7692
std::unordered_map<runcpp2::CmdOptions, std::string> currentOptions;
93+
//std::unordered_set<runcpp2::CmdOptions> currentOptions;
94+
7795
{
78-
std::unordered_map<std::string, runcpp2::OptionInfo> optionsMap =
96+
std::unordered_map<std::string, runcpp2::OptionInfo> longOptionsMap =
7997
{
8098
{
81-
"--reset-dependencies",
82-
runcpp2::OptionInfo(runcpp2::CmdOptions::RESET_DEPENDENCIES, false, "")
99+
"--reset-cache",
100+
runcpp2::OptionInfo(runcpp2::CmdOptions::RESET_CACHE, false)
83101
},
84102
{
85103
"--reset-user-config",
@@ -89,26 +107,51 @@ int main(int argc, char* argv[])
89107
"--executable",
90108
runcpp2::OptionInfo(runcpp2::CmdOptions::EXECUTABLE, false)
91109
},
110+
{
111+
"--help",
112+
runcpp2::OptionInfo(runcpp2::CmdOptions::HELP, false)
113+
},
92114
};
93115

94-
currentArgIndex = ParseArgs(optionsMap, currentOptions, argc, argv);
116+
std::unordered_map<std::string, const runcpp2::OptionInfo&> shortOptionsMap =
117+
{
118+
{"-r", longOptionsMap.at("--reset-cache")},
119+
{"-c", longOptionsMap.at("--reset-user-config")},
120+
{"-e", longOptionsMap.at("--executable")},
121+
{"-h", longOptionsMap.at("--help")},
122+
};
123+
124+
currentArgIndex = ParseArgs(longOptionsMap, shortOptionsMap, currentOptions, argc, argv);
95125

96126
if(currentArgIndex == -1)
97127
{
98-
ssLOG_FATAL("Invalid option");
128+
ssLOG_ERROR("Invalid option");
99129
return -1;
100130
}
101131

102132
++currentArgIndex;
103133
}
104134

135+
//Help message
136+
if(currentOptions.count(runcpp2::CmdOptions::HELP))
137+
{
138+
ssLOG_BASE("Usage: runcpp2 [options] [input_file]");
139+
ssLOG_BASE("Options:");
140+
ssLOG_BASE(" -r, --reset-cache Deletes all cache and build everything from scratch");
141+
ssLOG_BASE(" -c, --reset-user-config Replace current user config with the default one");
142+
ssLOG_BASE(" -e, --executable Builds executable instead of running the file");
143+
ssLOG_BASE(" -h, --help Show this help message");
144+
145+
return 0;
146+
}
147+
105148
//Resetting user config
106-
if(currentOptions.find(runcpp2::CmdOptions::RESET_USER_CONFIG) != currentOptions.end())
149+
if(currentOptions.count(runcpp2::CmdOptions::RESET_USER_CONFIG))
107150
{
108151
ssLOG_INFO("Resetting user config");
109152
if(!runcpp2::WriteDefaultConfig(runcpp2::GetConfigFilePath()))
110153
{
111-
ssLOG_FATAL("Failed reset user config");
154+
ssLOG_ERROR("Failed reset user config");
112155
return -1;
113156
}
114157

@@ -120,7 +163,7 @@ int main(int argc, char* argv[])
120163

121164
if(!runcpp2::ReadUserConfig(profiles, preferredProfile))
122165
{
123-
ssLOG_FATAL("Failed read user config");
166+
ssLOG_ERROR("Failed read user config");
124167
return -1;
125168
}
126169

@@ -131,7 +174,7 @@ int main(int argc, char* argv[])
131174
std::vector<std::string> scriptArgs;
132175
if(currentArgIndex >= argc)
133176
{
134-
ssLOG_FATAL("An input file is required");
177+
ssLOG_ERROR("An input file is required");
135178
return 1;
136179
}
137180

@@ -150,4 +193,4 @@ int main(int argc, char* argv[])
150193
scriptArgs);
151194

152195
return result;
153-
}
196+
}

0 commit comments

Comments
 (0)