aboutsummaryrefslogtreecommitdiff
path: root/doc/manual/generate-manpage.nix
blob: 4709c0e2c99f3fa53038c0583567be594884a959 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
with builtins;
with import ./utils.nix;

let

  showCommand =
    { command, section, def }:
    "${section} Name\n\n"
    + "`${command}` - ${def.description}\n\n"
    + "${section} Synopsis\n\n"
    + showSynopsis { inherit command; args = def.args; }
    + (if def ? doc
       then "${section} Description\n\n" + def.doc + "\n\n"
       else "")
    + (let s = showFlags def.flags; in
       if s != ""
       then "${section} Flags\n\n${s}"
       else "")
    + (if def.examples or [] != []
       then
         "${section} Examples\n\n"
         + concatStrings (map ({ description, command }: "${description}\n\n```console\n${command}\n```\n\n") def.examples)
       else "")
    + (if def.commands or [] != []
       then concatStrings (
         map (name:
           "# Subcommand `${command} ${name}`\n\n"
           + showCommand { command = command + " " + name; section = "##"; def = def.commands.${name}; })
           (attrNames def.commands))
       else "");

  showFlags = flags:
    concatStrings
      (map (longName:
        let flag = flags.${longName}; in
        if flag.category or "" != "config"
        then
          "  - `--${longName}`"
          + (if flag ? shortName then " / `${flag.shortName}`" else "")
          + (if flag ? labels then " " + (concatStringsSep " " (map (s: "*${s}*") flag.labels)) else "")
          + "  \n"
          + "    " + flag.description + "\n\n"
        else "")
        (attrNames flags));

  showSynopsis =
    { command, args }:
    "`${command}` [*flags*...] ${concatStringsSep " "
      (map (arg: "*${arg.label}*" + (if arg ? arity then "" else "...")) args)}\n\n";

in

command:

"Title: nix\n\n"
+ showCommand { command = "nix"; section = "#"; def = command; }