diff options
author | Robin Burchell <robin+git@viroteck.net> | 2019-05-17 15:06:06 +0200 |
---|---|---|
committer | Andreas Kling <awesomekling@gmail.com> | 2019-05-17 15:49:37 +0200 |
commit | b92fa59832bd69d52ccef045031b66521ab7a5ed (patch) | |
tree | 632006a78b05e55698d35843fd0d176c2061edd9 /Userland | |
parent | 56aad835add439dd3326d1a5bb8c3d24e9613888 (diff) | |
download | serenity-b92fa59832bd69d52ccef045031b66521ab7a5ed.zip |
Userland: Port pape to use ArgsParser, and minor cleanups
Diffstat (limited to 'Userland')
-rw-r--r-- | Userland/pape.cpp | 77 |
1 files changed, 31 insertions, 46 deletions
diff --git a/Userland/pape.cpp b/Userland/pape.cpp index 283a5222fb..0458d84c07 100644 --- a/Userland/pape.cpp +++ b/Userland/pape.cpp @@ -3,59 +3,17 @@ #include <errno.h> #include <string.h> #include <stdlib.h> -#include <getopt.h> #include <fcntl.h> #include <dirent.h> #include <AK/AKString.h> +#include <AK/ArgsParser.h> #include <AK/StringBuilder.h> #include <AK/Vector.h> #include <AK/FileSystemPath.h> #include <LibGUI/GDesktop.h> #include <LibGUI/GApplication.h> -static bool flag_show_all = false; -static int show_all(); -static int show_current(); -static int set_pape(const char*); - -static void usage() -{ - printf("usage: pape [-a] [name]\n"); -} - -int main(int argc, char** argv) -{ - GApplication app(argc, argv); - - int opt; - while ((opt = getopt(argc, argv, "a")) != -1) { - switch (opt) { - case 'a': - flag_show_all = true; - break; - default: - usage(); - return 0; - } - } - - if (flag_show_all) - return show_all(); - - if (argc == 1) { - show_current(); - return 0; - } - - if (optind >= argc) { - usage(); - return 0; - } - - return set_pape(argv[optind]); -} - -int show_all() +static int handle_show_all() { DIR* dirp = opendir("/res/wallpapers"); if (!dirp) { @@ -71,13 +29,13 @@ int show_all() return 0; } -int show_current() +static int handle_show_current() { printf("%s\n", GDesktop::the().wallpaper().characters()); return 0; } -int set_pape(const char* name) +static int handle_set_pape(const String& name) { StringBuilder builder; builder.append("/res/wallpapers/"); @@ -88,4 +46,31 @@ int set_pape(const char* name) return 1; } return 0; +}; + +int main(int argc, char** argv) +{ + GApplication app(argc, argv); + + AK::ArgsParser args_parser("pape"); + + args_parser.add_arg("a", "show all wallpapers"); + args_parser.add_arg("c", "show current wallpaper"); + args_parser.set_single_value("name"); + + AK::ArgsParserResult args = args_parser.parse(argc, (const char**)argv); + + if (args.is_present("a")) + return handle_show_all(); + else if (args.is_present("c")) + return handle_show_current(); + + Vector<String> values = args.get_single_values(); + if (values.size() != 1) { + args_parser.print_usage(); + return 0; + } + + return handle_set_pape(values[0]); } + |