diff options
author | Kenneth Myhra <kennethmyhra@gmail.com> | 2021-11-25 22:29:12 +0100 |
---|---|---|
committer | Andreas Kling <kling@serenityos.org> | 2021-11-27 11:14:16 +0100 |
commit | 8c4625e3b1a2bbcb66e315268b01c6047e900912 (patch) | |
tree | 5caa8d79b290ec052cbbdb52b8a0eac35eeda537 /Userland/Utilities/asctl.cpp | |
parent | 880888fcc71abde81cfbe0d583b31f333a02f184 (diff) | |
download | serenity-8c4625e3b1a2bbcb66e315268b01c6047e900912.zip |
asctl: Port to LibMain :^)
Diffstat (limited to 'Userland/Utilities/asctl.cpp')
-rw-r--r-- | Userland/Utilities/asctl.cpp | 33 |
1 files changed, 17 insertions, 16 deletions
diff --git a/Userland/Utilities/asctl.cpp b/Userland/Utilities/asctl.cpp index 28749d5f47..19780c775c 100644 --- a/Userland/Utilities/asctl.cpp +++ b/Userland/Utilities/asctl.cpp @@ -13,6 +13,7 @@ #include <LibCore/ArgsParser.h> #include <LibCore/EventLoop.h> #include <LibCore/File.h> +#include <LibMain/Main.h> #include <math.h> #include <stdio.h> #include <sys/ioctl.h> @@ -24,31 +25,31 @@ enum AudioVariable : u32 { }; // asctl: audio server control utility -int main(int argc, char** argv) +ErrorOr<int> serenity_main(Main::Arguments arguments) { Core::EventLoop loop; auto audio_client = Audio::ClientConnection::construct(); String command = String::empty(); - Vector<StringView> arguments; + Vector<StringView> command_arguments; bool human_mode = false; Core::ArgsParser args_parser; args_parser.set_general_help("Send control signals to the audio server and hardware."); args_parser.add_option(human_mode, "Print human-readable output", "human-readable", 'h'); args_parser.add_positional_argument(command, "Command, either (g)et or (s)et\n\n\tThe get command accepts a list of variables to print.\n\tThey are printed in the given order.\n\tIf no value is specified, all are printed.\n\n\tThe set command accepts a any number of variables\n\tfollowed by the value they should be set to.\n\n\tPossible variables are (v)olume, (m)ute, sample(r)ate.\n", "command"); - args_parser.add_positional_argument(arguments, "Arguments for the command", "args", Core::ArgsParser::Required::No); - args_parser.parse(argc, argv); + args_parser.add_positional_argument(command_arguments, "Arguments for the command", "args", Core::ArgsParser::Required::No); + args_parser.parse(arguments); if (command.equals_ignoring_case("get") || command == "g") { // Get variables Vector<AudioVariable> values_to_print; - if (arguments.is_empty()) { + if (command_arguments.is_empty()) { values_to_print.append(AudioVariable::Volume); values_to_print.append(AudioVariable::Mute); values_to_print.append(AudioVariable::SampleRate); } else { - for (auto& variable : arguments) { + for (auto& variable : command_arguments) { if (variable.is_one_of("v"sv, "volume"sv)) values_to_print.append(AudioVariable::Volume); else if (variable.is_one_of("m"sv, "mute"sv)) @@ -95,25 +96,25 @@ int main(int argc, char** argv) } else if (command.equals_ignoring_case("set") || command == "s") { // Set variables HashMap<AudioVariable, Variant<int, bool>> values_to_set; - for (size_t i = 0; i < arguments.size(); ++i) { - if (i == arguments.size() - 1) { + for (size_t i = 0; i < command_arguments.size(); ++i) { + if (i == command_arguments.size() - 1) { warnln("Error: value missing for last variable"); return 1; } - auto& variable = arguments[i]; + auto& variable = command_arguments[i]; if (variable.is_one_of("v"sv, "volume"sv)) { - auto volume = arguments[++i].to_int(); + auto volume = command_arguments[++i].to_int(); if (!volume.has_value()) { - warnln("Error: {} is not an integer volume", arguments[i - 1]); + warnln("Error: {} is not an integer volume", command_arguments[i - 1]); return 1; } if (volume.value() < 0 || volume.value() > 100) { - warnln("Error: {} is not between 0 and 100", arguments[i - 1]); + warnln("Error: {} is not between 0 and 100", command_arguments[i - 1]); return 1; } values_to_set.set(AudioVariable::Volume, volume.value()); } else if (variable.is_one_of("m"sv, "mute"sv)) { - auto& mute_text = arguments[++i]; + auto& mute_text = command_arguments[++i]; bool mute; if (mute_text.equals_ignoring_case("true") || mute_text == "1") { mute = true; @@ -125,14 +126,14 @@ int main(int argc, char** argv) } values_to_set.set(AudioVariable::Mute, mute); } else if (variable.is_one_of("r"sv, "samplerate"sv)) { - auto sample_rate = arguments[++i].to_int(); + auto sample_rate = command_arguments[++i].to_int(); if (!sample_rate.has_value()) { - warnln("Error: {} is not an integer sample rate", arguments[i - 1]); + warnln("Error: {} is not an integer sample rate", command_arguments[i - 1]); return 1; } values_to_set.set(AudioVariable::SampleRate, sample_rate.value()); } else { - warnln("Error: Unrecognized variable {}", arguments[i]); + warnln("Error: Unrecognized variable {}", command_arguments[i]); return 1; } } |