From: Lennart Poettering Date: Fri, 19 Jan 2024 14:49:33 +0000 (+0100) Subject: hostnamectl: add -j switch for quick json output X-Git-Tag: v256-rc1~1071^2 X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=refs%2Fpull%2F31019%2Fhead;p=thirdparty%2Fsystemd.git hostnamectl: add -j switch for quick json output We already support -j as shortcut for JSON mode in various tools. Let's add one more. We probably should add this systematically (at least where it doesn't conflict with an existing -j switch with other purpose). But I am too lazy to add that now. --- diff --git a/man/hostnamectl.xml b/man/hostnamectl.xml index 32390490950..0f50bf9e44b 100644 --- a/man/hostnamectl.xml +++ b/man/hostnamectl.xml @@ -201,6 +201,7 @@ + diff --git a/shell-completion/bash/hostnamectl b/shell-completion/bash/hostnamectl index 63edc084aeb..27a2fe63d2f 100644 --- a/shell-completion/bash/hostnamectl +++ b/shell-completion/bash/hostnamectl @@ -35,7 +35,7 @@ _hostnamectl() { local i verb comps local cur=${COMP_WORDS[COMP_CWORD]} prev=${COMP_WORDS[COMP_CWORD-1]} local -A OPTS=( - [STANDALONE]='-h --help --version --transient --static --pretty --no-ask-password' + [STANDALONE]='-h --help --version --transient --static --pretty --no-ask-password -j' [ARG]='-H --host -M --machine --json' ) diff --git a/src/hostname/hostnamectl.c b/src/hostname/hostnamectl.c index 4b92ac2b230..4abcf40c263 100644 --- a/src/hostname/hostnamectl.c +++ b/src/hostname/hostnamectl.c @@ -616,6 +616,7 @@ static int help(void) { " --pretty Only set pretty hostname\n" " --json=pretty|short|off\n" " Generate JSON output\n" + " -j Same as --json=pretty on tty, --json=short otherwise\n" "\nSee the %s for details.\n", program_invocation_short_name, ansi_highlight(), @@ -658,7 +659,7 @@ static int parse_argv(int argc, char *argv[]) { assert(argc >= 0); assert(argv); - while ((c = getopt_long(argc, argv, "hH:M:", options, NULL)) >= 0) + while ((c = getopt_long(argc, argv, "hH:M:j", options, NULL)) >= 0) switch (c) { @@ -701,6 +702,10 @@ static int parse_argv(int argc, char *argv[]) { break; + case 'j': + arg_json_format_flags = JSON_FORMAT_PRETTY_AUTO|JSON_FORMAT_COLOR_AUTO; + break; + case '?': return -EINVAL;