diff options
author | Linus Groh <mail@linusgroh.de> | 2020-08-05 21:24:48 +0200 |
---|---|---|
committer | Andreas Kling <kling@serenityos.org> | 2020-08-06 20:41:13 +0200 |
commit | 31a4a2cc2a4e9f35d812b802936f0171a97dee8b (patch) | |
tree | 8e67af266ac20f4f207f10cd511660ed66b5156e | |
parent | 718a45ef717bda8de3f6b0b6989e878944f5c744 (diff) | |
download | serenity-31a4a2cc2a4e9f35d812b802936f0171a97dee8b.zip |
Userland: Use Core::ArgsParser for 'hostname'
-rw-r--r-- | Userland/hostname.cpp | 39 |
1 files changed, 21 insertions, 18 deletions
diff --git a/Userland/hostname.cpp b/Userland/hostname.cpp index 2d36772081..68c6dfe030 100644 --- a/Userland/hostname.cpp +++ b/Userland/hostname.cpp @@ -24,6 +24,7 @@ * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */ +#include <LibCore/ArgsParser.h> #include <errno.h> #include <stdio.h> #include <string.h> @@ -31,24 +32,26 @@ int main(int argc, char** argv) { - if (argc == 1) { - char buffer[HOST_NAME_MAX]; - int rc = gethostname(buffer, sizeof(buffer)); - if (rc < 0) { - printf("gethostname() error: %s\n", strerror(errno)); - return 1; - } - printf("%s\n", buffer); - } - else if (argc == 2) { - if (strlen(argv[1]) >= HOST_NAME_MAX) { - printf("hostname must be less than %i characters\n", HOST_NAME_MAX); - return 1; - } - else { - sethostname(argv[1], strlen(argv[1])); - } - } + const char* hostname = nullptr; + Core::ArgsParser args_parser; + args_parser.add_positional_argument(hostname, "Hostname to set", "hostname", Core::ArgsParser::Required::No); + args_parser.parse(argc, argv); + + if (!hostname) { + char buffer[HOST_NAME_MAX]; + int rc = gethostname(buffer, sizeof(buffer)); + if (rc < 0) { + perror("gethostname"); + return 1; + } + printf("%s\n", buffer); + } else { + if (strlen(hostname) >= HOST_NAME_MAX) { + fprintf(stderr, "Hostname must be less than %i characters\n", HOST_NAME_MAX); + return 1; + } + sethostname(hostname, strlen(hostname)); + } return 0; } |