From 14db94f44be766af786cdf05bff8edc82f8f9da5 Mon Sep 17 00:00:00 2001 From: Linus Groh Date: Wed, 5 Aug 2020 22:37:59 +0200 Subject: Userland: Use Core::ArgsParser for 'chgrp' --- Userland/chgrp.cpp | 24 +++++++++++++----------- 1 file 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 #include +#include #include #include #include @@ -40,33 +41,34 @@ int main(int argc, char** argv) return 1; } - if (argc < 2) { - printf("usage: chgrp \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; -- cgit v1.2.3