Userland: Port pape to use ArgsParser, and minor cleanups

This commit is contained in:
Robin Burchell 2019-05-17 15:06:06 +02:00 committed by Andreas Kling
parent 56aad835ad
commit b92fa59832
Notes: sideshowbarker 2024-07-19 14:04:11 +09:00

View file

@ -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]);
}