summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorLinus Groh <mail@linusgroh.de>2020-08-05 22:37:59 +0200
committerAndreas Kling <kling@serenityos.org>2020-08-06 20:41:13 +0200
commit14db94f44be766af786cdf05bff8edc82f8f9da5 (patch)
tree0dab6a25669708091aba38468e20442a40546ec6
parentc8d690d840545a159805bde3c4f636818e2ce86e (diff)
downloadserenity-14db94f44be766af786cdf05bff8edc82f8f9da5.zip
Userland: Use Core::ArgsParser for 'chgrp'
-rw-r--r--Userland/chgrp.cpp24
1 files changed, 13 insertions, 11 deletions
diff --git a/Userland/chgrp.cpp b/Userland/chgrp.cpp
index d3e5538468..cf5885d41c 100644
--- a/Userland/chgrp.cpp
+++ b/Userland/chgrp.cpp
@@ -26,6 +26,7 @@
#include <AK/Optional.h>
#include <AK/String.h>
+#include <LibCore/ArgsParser.h>
#include <grp.h>
#include <pwd.h>
#include <stdio.h>
@@ -40,33 +41,34 @@ int main(int argc, char** argv)
return 1;
}
- if (argc < 2) {
- printf("usage: chgrp <gid> <path>\n");
- return 0;
- }
+ const char* gid_arg = nullptr;
+ const char* path = nullptr;
+
+ Core::ArgsParser args_parser;
+ args_parser.add_positional_argument(gid_arg, "Group ID", "gid");
+ args_parser.add_positional_argument(path, "Path to file", "path");
+ args_parser.parse(argc, argv);
gid_t new_gid = -1;
- auto gid_arg = String(argv[1]);
- if (gid_arg.is_empty()) {
+ if (String(gid_arg).is_empty()) {
fprintf(stderr, "Empty gid option\n");
return 1;
}
- auto number = gid_arg.to_uint();
-
+ auto number = String(gid_arg).to_uint();
if (number.has_value()) {
new_gid = number.value();
} else {
- auto* group = getgrnam(gid_arg.characters());
+ auto* group = getgrnam(gid_arg);
if (!group) {
- fprintf(stderr, "Unknown group '%s'\n", gid_arg.characters());
+ fprintf(stderr, "Unknown group '%s'\n", gid_arg);
return 1;
}
new_gid = group->gr_gid;
}
- int rc = chown(argv[2], -1, new_gid);
+ int rc = chown(path, -1, new_gid);
if (rc < 0) {
perror("chgrp");
return 1;