mirror of
https://github.com/RGBCube/serenity
synced 2025-07-27 07:37:46 +00:00
Userland: Port pape to use ArgsParser, and minor cleanups
This commit is contained in:
parent
56aad835ad
commit
b92fa59832
1 changed files with 31 additions and 46 deletions
|
@ -3,59 +3,17 @@
|
||||||
#include <errno.h>
|
#include <errno.h>
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
#include <getopt.h>
|
|
||||||
#include <fcntl.h>
|
#include <fcntl.h>
|
||||||
#include <dirent.h>
|
#include <dirent.h>
|
||||||
#include <AK/AKString.h>
|
#include <AK/AKString.h>
|
||||||
|
#include <AK/ArgsParser.h>
|
||||||
#include <AK/StringBuilder.h>
|
#include <AK/StringBuilder.h>
|
||||||
#include <AK/Vector.h>
|
#include <AK/Vector.h>
|
||||||
#include <AK/FileSystemPath.h>
|
#include <AK/FileSystemPath.h>
|
||||||
#include <LibGUI/GDesktop.h>
|
#include <LibGUI/GDesktop.h>
|
||||||
#include <LibGUI/GApplication.h>
|
#include <LibGUI/GApplication.h>
|
||||||
|
|
||||||
static bool flag_show_all = false;
|
static int handle_show_all()
|
||||||
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()
|
|
||||||
{
|
{
|
||||||
DIR* dirp = opendir("/res/wallpapers");
|
DIR* dirp = opendir("/res/wallpapers");
|
||||||
if (!dirp) {
|
if (!dirp) {
|
||||||
|
@ -71,13 +29,13 @@ int show_all()
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
int show_current()
|
static int handle_show_current()
|
||||||
{
|
{
|
||||||
printf("%s\n", GDesktop::the().wallpaper().characters());
|
printf("%s\n", GDesktop::the().wallpaper().characters());
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
int set_pape(const char* name)
|
static int handle_set_pape(const String& name)
|
||||||
{
|
{
|
||||||
StringBuilder builder;
|
StringBuilder builder;
|
||||||
builder.append("/res/wallpapers/");
|
builder.append("/res/wallpapers/");
|
||||||
|
@ -88,4 +46,31 @@ int set_pape(const char* name)
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
return 0;
|
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]);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue