Print common flags in --help

This commit is contained in:
Ana Hobden 2022-10-07 09:07:22 -07:00
parent ac0fb38e8a
commit 069409d167
3 changed files with 15 additions and 11 deletions

View File

@ -1,11 +1,11 @@
{ command }: { toplevel }:
with builtins; with builtins;
with import ./utils.nix; with import ./utils.nix;
let let
showCommand = { command, details, filename }: showCommand = { command, details, filename, toplevel }:
let let
result = '' result = ''
> **Warning** \ > **Warning** \
@ -57,14 +57,15 @@ let
maybeOptions = if details.flags == {} then "" else '' maybeOptions = if details.flags == {} then "" else ''
# Options # Options
${showOptions details.flags} ${showOptions details.flags toplevel.flags}
''; '';
showOptions = options: showOptions = options: commonOptions:
let let
allOptions = options // commonOptions;
showCategory = cat: '' showCategory = cat: ''
${if cat != "" then "**${cat}:**" else ""} ${if cat != "" then "**${cat}:**" else ""}
${listOptions (filterAttrs (n: v: v.category == cat) options)} ${listOptions (filterAttrs (n: v: v.category == cat) allOptions)}
''; '';
listOptions = opts: concatStringsSep "\n" (attrValues (mapAttrs showOption opts)); listOptions = opts: concatStringsSep "\n" (attrValues (mapAttrs showOption opts));
showOption = name: option: showOption = name: option:
@ -76,30 +77,33 @@ let
${option.description} ${option.description}
''; '';
categories = sort builtins.lessThan (unique (map (cmd: cmd.category) (attrValues options))); categories = sort builtins.lessThan (unique (map (cmd: cmd.category) (attrValues allOptions)));
in concatStrings (map showCategory categories); in concatStrings (map showCategory categories);
in squash result; in squash result;
appendName = filename: name: (if filename == "nix" then "nix3" else filename) + "-" + name; appendName = filename: name: (if filename == "nix" then "nix3" else filename) + "-" + name;
processCommand = { command, details, filename }: processCommand = { command, details, filename, toplevel }:
let let
cmd = { cmd = {
inherit command; inherit command;
name = filename + ".md"; name = filename + ".md";
value = showCommand { inherit command details filename; }; value = showCommand { inherit command details filename toplevel; };
}; };
subcommand = subCmd: processCommand { subcommand = subCmd: processCommand {
command = command + " " + subCmd; command = command + " " + subCmd;
details = details.commands.${subCmd}; details = details.commands.${subCmd};
filename = appendName filename subCmd; filename = appendName filename subCmd;
inherit toplevel;
}; };
in [ cmd ] ++ concatMap subcommand (attrNames details.commands or {}); in [ cmd ] ++ concatMap subcommand (attrNames details.commands or {});
parsedToplevel = builtins.fromJSON toplevel;
manpages = processCommand { manpages = processCommand {
command = "nix"; command = "nix";
details = builtins.fromJSON command; details = parsedToplevel;
filename = "nix"; filename = "nix";
toplevel = parsedToplevel;
}; };
tableOfContents = let tableOfContents = let

View File

@ -50,7 +50,7 @@ $(d)/src/SUMMARY.md: $(d)/src/SUMMARY.md.in $(d)/src/command-ref/new-cli
$(d)/src/command-ref/new-cli: $(d)/nix.json $(d)/generate-manpage.nix $(bindir)/nix $(d)/src/command-ref/new-cli: $(d)/nix.json $(d)/generate-manpage.nix $(bindir)/nix
@rm -rf $@ @rm -rf $@
$(trace-gen) $(nix-eval) --write-to $@ --expr 'import doc/manual/generate-manpage.nix { command = builtins.readFile $<; }' $(trace-gen) $(nix-eval) --write-to $@ --expr 'import doc/manual/generate-manpage.nix { toplevel = builtins.readFile $<; }'
$(d)/src/command-ref/conf-file.md: $(d)/conf-file.json $(d)/generate-options.nix $(d)/src/command-ref/conf-file-prefix.md $(bindir)/nix $(d)/src/command-ref/conf-file.md: $(d)/conf-file.json $(d)/generate-options.nix $(d)/src/command-ref/conf-file-prefix.md $(bindir)/nix
@cat doc/manual/src/command-ref/conf-file-prefix.md > $@.tmp @cat doc/manual/src/command-ref/conf-file-prefix.md > $@.tmp

View File

@ -187,7 +187,7 @@ static void showHelp(std::vector<std::string> subcommand, MultiCommand & topleve
*vUtils); *vUtils);
auto attrs = state.buildBindings(16); auto attrs = state.buildBindings(16);
attrs.alloc("command").mkString(toplevel.toJSON().dump()); attrs.alloc("toplevel").mkString(toplevel.toJSON().dump());
auto vRes = state.allocValue(); auto vRes = state.allocValue();
state.callFunction(*vGenerateManpage, state.allocValue()->mkAttrs(attrs), *vRes, noPos); state.callFunction(*vGenerateManpage, state.allocValue()->mkAttrs(attrs), *vRes, noPos);