From 069340179e91202b2d3adc5ea1e3023ba2a51691 Mon Sep 17 00:00:00 2001
From: Eelco Dolstra <edolstra@gmail.com>
Date: Tue, 18 Aug 2020 15:15:35 +0200
Subject: [PATCH] Improve nix.1 manpage generator

---
 doc/manual/generate-manpage.jq | 72 +++++++++++++++++-----------------
 src/nix/main.cc                |  5 +++
 2 files changed, 40 insertions(+), 37 deletions(-)

diff --git a/doc/manual/generate-manpage.jq b/doc/manual/generate-manpage.jq
index cf06da0c8..d3cf1c601 100644
--- a/doc/manual/generate-manpage.jq
+++ b/doc/manual/generate-manpage.jq
@@ -1,42 +1,40 @@
 def show_flags:
-
-.flags
-| map_values(select(.category != "config"))
-| to_entries
-| map(
-    "  - `--" + .key + "`"
-    + (if .value.shortName then " / `" + .value.shortName + "`" else "" end)
-    + (if .value.labels then " " + (.value.labels | map("*" + . + "*") | join(" ")) else "" end)
-    + "  \n"
-    + "    " + .value.description + "\n\n")
-| join("")
-;
+  .flags
+  | map_values(select(.category != "config"))
+  | to_entries
+  | map(
+      "  - `--" + .key + "`"
+      + (if .value.shortName then " / `" + .value.shortName + "`" else "" end)
+      + (if .value.labels then " " + (.value.labels | map("*" + . + "*") | join(" ")) else "" end)
+      + "  \n"
+      + "    " + .value.description + "\n\n")
+  | join("")
+  ;
 
 def show_synopsis:
+  "`" + .command + "` " + (.args | map("*" + .label + "*" + (if has("arity") then "" else "..." end)) | join(" ")) + "\n\n"
+  ;
 
-"`" + .command + "` " + (.args | map("*" + .label + "*" + (if has("arity") then "" else "..." end)) | join(" ")) + "\n"
-
-;
+def show_command:
+  . as $top |
+  .section + " Name\n\n"
+  + "`" + .command + "` - " + .def.description + "\n\n"
+  + .section + " Synopsis\n\n"
+  + ({"command": .command, "args": .def.args} | show_synopsis)
+  + (if (.def.flags | length) > 0 then
+      .section + " Flags\n\n"
+      + (.def | show_flags)
+    else "" end)
+  + (if (.def.examples | length) > 0 then
+      .section + " Examples\n\n"
+      + (.def.examples | map(.description + "\n\n```console\n" + .command + "\n```\n" ) | join("\n"))
+      + "\n"
+     else "" end)
+  + (if .def.commands then .def.commands | to_entries | map(
+      "# Subcommand `" + ($top.command + " " + .key) + "`\n\n"
+      + ({"command": ($top.command + " " + .key), "section": "##", "def": .value} | show_command)
+    ) | join("") else "" end)
+  ;
 
-"# Synopsis\n\n"
-+ ({"command": "nix", "args": .args} | show_synopsis)
-+ "\n"
-+ "# Common flags\n\n"
-+ show_flags
-+ (.commands | to_entries | map(
-    "# Operation `" + .key + "`\n\n"
-    + "## Synopsis\n\n"
-    + ({"command": ("nix " + .key), "args": .value.args} | show_synopsis)
-    + "\n"
-    + "## Description\n\n"
-    + .value.description + "\n\n"
-    + (if (.value.flags | length) > 0 then
-        "## Flags\n\n"
-        + (.value | show_flags)
-      else "" end)
-    + (if (.value.examples | length) > 0 then
-        "## Examples\n\n"
-        + (.value.examples | map("- " + .description + "\n\n  ```console\n  " + .command + "\n  ```\n" ) | join("\n"))
-        + "\n"
-       else "" end)
-  ) | join(""))
+"Title: nix\n\n"
++ ({"command": "nix", "section": "#", "def": .} | show_command)
diff --git a/src/nix/main.cc b/src/nix/main.cc
index fa04bebea..0c4fd46fd 100644
--- a/src/nix/main.cc
+++ b/src/nix/main.cc
@@ -142,6 +142,11 @@ struct NixArgs : virtual MultiCommand, virtual MixCommonArgs
         printHelp(programName, std::cout);
         throw Exit();
     }
+
+    std::string description() override
+    {
+        return "a tool for reproducible and declarative configuration management";
+    }
 };
 
 void mainWrapped(int argc, char * * argv)
-- 
GitLab