-
Notifications
You must be signed in to change notification settings - Fork 0
/
commandline.c++
152 lines (133 loc) · 5.4 KB
/
commandline.c++
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
/**
The MIT License (MIT)
Copyright (c) 2013 Ruben Van Boxem
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
**/
/**
* architekton - commandline.c++
* Function implementations.
**/
#include "architekton/commandline.h++"
#include "architekton/debug.h++"
#include "architekton/error.h++"
#include "architekton/file.h++"
#include "architekton/options.h++"
#include "architekton/utility.h++"
#include <string>
using std::string;
namespace architekton
{
#ifdef _WIN32
void parse_commandline(int,
char*[],
options& options)
{
int argc;
auto argv = CommandLineToArgvW(GetCommandLineW(), &argc);
if(argv == nullptr)
throw error("Call to CommandLineToArgvW failed.");
string_vector args;
args.reserve(static_cast<string_vector::size_type>(argc));
for(int i=1; i<argc; ++i) // skip argv[0]
{
args.push_back(convert_to_utf8(argv[i]));
}
#else
void parse_commandline(int argc,
char* argv[],
options& options)
{
string_vector args(argv+1, argv+argc); // skip argv[0]
#endif
debug_print(debug::commandline, "Step 1: Commandline arguments.");
bool first_dashless_argument = true;
for(auto&& arg : args)
{
if(arg[0] == '-')
{
if(arg[1] == '-') // arguments should never be empty (min. length 2)
{
const auto key_value = split(arg, '=', 2);
debug_print(debug::commandline, "Project argument (--key[=value]): key=\'", key_value.first, "\'', value=\'", key_value.second, "\'.");
}
else
{
const auto key_value = split(arg, '=', 1);
set_option(key_value.first, key_value.second, options);
}
}
else
{
debug_print(debug::commandline, "Dashless argument: \'", arg, "\'.");
if(first_dashless_argument)
{
first_dashless_argument = false;
convert_path_seperators(arg); // if it were a target name, it won't contain slashes anyway
if(directory_exists(arg))
{
debug_print(debug::commandline, "Possible source directory: \'", arg, "\'.");
auto project_files = find_files(arg, "*.architekton.txt");
if(project_files.empty())
{
debug_print(debug::commandline, "No project file found in \'", arg, "\', checking \'.\'.");
project_files = find_files(".", "*.architekton.txt");
if(project_files.empty())
throw error("No project file found. Please specify the path to a *.architekton.txt file to be built.");
// get rid of goto
goto target_to_build;
}
if(project_files.size() > 1)
throw error("Multiple *.architekton.txt files found. Please specify the filename for the project to be built. Files found:", project_files);
// We now have exactly one project file
options.main_project_file = project_files.begin()->name;
debug_print(debug::commandline, "Main project file set: \'", options.main_project_file, "\'.");
continue;
}
else if(file_exists(arg))
{
debug_print(debug::commandline, "Possible project file: \'", arg, "\'.");
auto project_files = find_files(arg);
// failure here would mean a bug in file_exists or find_files that needs fixing
options.main_project_file = project_files.begin()->name;
continue;
}
}
target_to_build:
debug_print(debug::commandline, "Target to build: \'", arg, "\'.");
if(!options.targets_to_build.insert(arg).second)
print("Warning: target \'", arg, "\' specified twice on the commandline.");
}
}
// reality check
if(options.main_project_file.empty())
throw error("No project file found. Please specify a path to an *.architekton.txt file.");
}
void set_option(const string &key,
const string &value,
options &options)
{
debug_print(debug::commandline, "Architekton argument (-key[=value]): key=\'", key, "\', value=\'", value, "\'.");
if(key == "debug")
debug_print(debug::commandline, "\'debug\' option currently unhandled.");
else if(key == "blueprints")
{
if(!directory_exists(value))
throw error("Specified \'blueprints\' directory does not exist: \'" + value + "\'.");
options.blueprint_directories[0] = value;
}
}
} // namespace architekton