main.cpp: Add a '-o'/'--output' argument

Signed-off-by: Username404 <w.iron.zombie@gmail.com>
This commit is contained in:
Username404 2023-06-12 21:31:46 +02:00
parent e20e7dabdc
commit d41d492686
Signed by: Username404-59
GPG Key ID: 7AB361FBB257A5D1
5 changed files with 17 additions and 4 deletions

View File

@ -8,7 +8,7 @@ _ybconAutoComplete() {
COMPREPLY=() COMPREPLY=()
current="${COMP_WORDS[COMP_CWORD]}" current="${COMP_WORDS[COMP_CWORD]}"
previous="${COMP_WORDS[COMP_CWORD-1]}" previous="${COMP_WORDS[COMP_CWORD-1]}"
options='-h -p -t --help --parallel --target= --newlines= --printresult --text --version --buildInfo' options='-h -p -t -o --help --parallel --target= --newlines= --printresult --text --output --version --buildInfo'
if [[ "${current}" == -* ]]; then if [[ "${current}" == -* ]]; then
YCompReply "$(compgen -W "$options" -- "$current")" YCompReply "$(compgen -W "$options" -- "$current")"
return 0 return 0

View File

@ -8,4 +8,5 @@ complete -c ybcon -l parallel -k -d "Transpile files in parallel mode"
complete -c ybcon -l target -k -f -a 'lua js py' -d "Set the transpilation target" complete -c ybcon -l target -k -f -a 'lua js py' -d "Set the transpilation target"
complete -c ybcon -l newlines -k -f -a 'on off' -d "Enable or disable new lines" complete -c ybcon -l newlines -k -f -a 'on off' -d "Enable or disable new lines"
complete -c ybcon -s p -l printresult -d "Enable printing the transpilation result to stdout" complete -c ybcon -s p -l printresult -d "Enable printing the transpilation result to stdout"
complete -c ybcon -x -s t -l text -d "Transpile text provided after this argument (implies -p)" complete -c ybcon -x -s t -l text -d "Transpile text provided after this argument (implies -p)"
complete -c ybcon -x -s o -l output -a '(__fish_complete_directories)' -d 'Output the transpiled file(s) to the specified directory'

View File

@ -10,6 +10,7 @@ _ybcon() {
--newlines='[Enable or disable new lines]:state:(on off)' \ --newlines='[Enable or disable new lines]:state:(on off)' \
{-p,--printresult}'[Enable printing the transpilation result to stdout]' \ {-p,--printresult}'[Enable printing the transpilation result to stdout]' \
{-t,--text}'[Transpile text provided after this argument (implies -p)]' \ {-t,--text}'[Transpile text provided after this argument (implies -p)]' \
{-o,--output}'[Output the transpiled file(s) to the specified directory]' \
"*:$completeyfile" "*:$completeyfile"
return 0 return 0
} }

View File

@ -21,6 +21,7 @@ usage() {
echo " --newlines=on/off Enable or disable new lines" echo " --newlines=on/off Enable or disable new lines"
echo " -p or --printresult Enable printing the transpilation result to stdout" echo " -p or --printresult Enable printing the transpilation result to stdout"
echo " -t or --text Transpile text provided after this argument (implies -p)" echo " -t or --text Transpile text provided after this argument (implies -p)"
echo " -o or --output Output the transpiled file(s) to the specified directory"
printf "\n" printf "\n"
fi fi
} }

View File

@ -19,6 +19,7 @@ int main(int argc, char* argv[]) {
using unit_result = pair<string, optional<Yerbacon::Exception>>; using unit_result = pair<string, optional<Yerbacon::Exception>>;
using unit = future<unit_result>; using unit = future<unit_result>;
map<string_view, unit> Units; map<string_view, unit> Units;
optional<filesystem::path> output_directory;
#ifdef EMSCRIPTEN #ifdef EMSCRIPTEN
is_node = MAIN_THREAD_EM_ASM_INT({ return ENVIRONMENT_IS_NODE; }); is_node = MAIN_THREAD_EM_ASM_INT({ return ENVIRONMENT_IS_NODE; });
#endif #endif
@ -41,6 +42,10 @@ int main(int argc, char* argv[]) {
} else goto invalid_argument; } else goto invalid_argument;
} }
else if (currentArgument == ArgumentShort("text")) printResult = text_provided = true; else if (currentArgument == ArgumentShort("text")) printResult = text_provided = true;
else if (currentArgument == ArgumentShort("output"))
if ((not output_directory.has_value()) and Units.empty() and (i + 1) != argc) {
output_directory = argv[i + 1]; ++i;
} else goto invalid_argument;
else if (text_provided || currentArgument.ends_with(".ybcon")) { else if (text_provided || currentArgument.ends_with(".ybcon")) {
#ifdef _OPENMP #ifdef _OPENMP
if (not parallel) omp_set_num_threads(1); if (not parallel) omp_set_num_threads(1);
@ -71,13 +76,18 @@ int main(int argc, char* argv[]) {
} }
#endif #endif
} }
Units.insert_or_assign(currentArgument, async(not parallel ? launch::deferred : launch::async, [&, currentArgument, file_path]() { Units.insert_or_assign(currentArgument, async(not parallel ? launch::deferred : launch::async, [&, currentArgument, file_path, index = Units.size() + 1]() {
unit_result resultingPair; unit_result resultingPair;
try { try {
resultingPair.first = Target::forName(target, newLines)->transpileWithTree( resultingPair.first = Target::forName(target, newLines)->transpileWithTree(
parseString(text_provided ? currentArgument : getFileContent(file_path->string())) parseString(text_provided ? currentArgument : getFileContent(file_path->string()))
); );
if (not text_provided) outputFileContent(filesystem::path(file_path.value()).replace_extension(target).string(), resultingPair.first); if (!text_provided or output_directory.has_value())
outputFileContent(
(not output_directory.has_value() ? filesystem::path(file_path.value())
: output_directory->append(
(not text_provided) ? file_path->filename().string() : to_string(index))
).replace_extension(target).string(), resultingPair.first);
} catch (const Yerbacon::Exception& error) { } catch (const Yerbacon::Exception& error) {
resultingPair.first = file_path.has_value() ? file_path->filename().string() : string(); resultingPair.first = file_path.has_value() ? file_path->filename().string() : string();
resultingPair.second.emplace(error); resultingPair.second.emplace(error);