]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
hostnamectl: add -j switch for quick json output 31019/head
authorLennart Poettering <lennart@poettering.net>
Fri, 19 Jan 2024 14:49:33 +0000 (15:49 +0100)
committerLennart Poettering <lennart@poettering.net>
Mon, 22 Jan 2024 13:39:22 +0000 (14:39 +0100)
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.

man/hostnamectl.xml
shell-completion/bash/hostnamectl
src/hostname/hostnamectl.c

index 323904909507d8bd8f3b9828f50c1491debefddb..0f50bf9e44bdc2128523c33b4489a7352204f994 100644 (file)
       <xi:include href="standard-options.xml" xpointer="help" />
       <xi:include href="standard-options.xml" xpointer="version" />
       <xi:include href="standard-options.xml" xpointer="json" />
+      <xi:include href="standard-options.xml" xpointer="j" />
     </variablelist>
   </refsect1>
 
index 63edc084aebe338bc50d72812615b3f34087675c..27a2fe63d2fb018d85dd1762bd71819a4183df72 100644 (file)
@@ -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'
     )
 
index 4b92ac2b230349adc343923ca5923b23108bdebf..4abcf40c26366c92381a15197c2546406a82dd4b 100644 (file)
@@ -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;