Group common options

This commit is contained in:
Eelco Dolstra 2021-01-25 19:03:13 +01:00
parent 807d963ee8
commit 36c4d6f592
11 changed files with 80 additions and 26 deletions

View file

@ -38,31 +38,39 @@ let
+ (if def ? doc + (if def ? doc
then def.doc + "\n\n" then def.doc + "\n\n"
else "") else "")
+ (let s = showFlags def.flags; in + (let s = showOptions def.flags; in
if s != "" if s != ""
then "# Flags\n\n${s}" then "# Options\n\n${s}"
else "") else "")
; ;
appendName = filename: name: (if filename == "nix" then "nix3" else filename) + "-" + name; appendName = filename: name: (if filename == "nix" then "nix3" else filename) + "-" + name;
showFlags = flags: showOptions = flags:
concatStrings let
(map (longName: categories = sort builtins.lessThan (unique (map (cmd: cmd.category) (attrValues flags)));
let flag = flags.${longName}; in in
if flag.category or "" != "config" concatStrings (map
then (cat:
" - `--${longName}`" (if cat != ""
+ (if flag ? shortName then " / `-${flag.shortName}`" else "") then "**${cat}:**\n\n"
+ (if flag ? labels then " " + (concatStringsSep " " (map (s: "*${s}*") flag.labels)) else "") else "")
+ " \n" + concatStrings
+ " " + flag.description + "\n\n" (map (longName:
else "") let
(attrNames flags)); flag = flags.${longName};
in
" - `--${longName}`"
+ (if flag ? shortName then " / `-${flag.shortName}`" else "")
+ (if flag ? labels then " " + (concatStringsSep " " (map (s: "*${s}*") flag.labels)) else "")
+ " \n"
+ " " + flag.description + "\n\n"
) (attrNames (filterAttrs (n: v: v.category == cat) flags))))
categories);
showSynopsis = showSynopsis =
{ command, args }: { command, args }:
"`${command}` [*flags*...] ${concatStringsSep " " "`${command}` [*option*...] ${concatStringsSep " "
(map (arg: "*${arg.label}*" + (if arg ? arity then "" else "...")) args)}\n\n"; (map (arg: "*${arg.label}*" + (if arg ? arity then "" else "...")) args)}\n\n";
processCommand = { command, def, filename }: processCommand = { command, def, filename }:

View file

@ -12,9 +12,12 @@ namespace nix {
MixEvalArgs::MixEvalArgs() MixEvalArgs::MixEvalArgs()
{ {
auto category = "Common evaluation options";
addFlag({ addFlag({
.longName = "arg", .longName = "arg",
.description = "Pass the value *expr* as the argument *name* to Nix functions.", .description = "Pass the value *expr* as the argument *name* to Nix functions.",
.category = category,
.labels = {"name", "expr"}, .labels = {"name", "expr"},
.handler = {[&](std::string name, std::string expr) { autoArgs[name] = 'E' + expr; }} .handler = {[&](std::string name, std::string expr) { autoArgs[name] = 'E' + expr; }}
}); });
@ -22,6 +25,7 @@ MixEvalArgs::MixEvalArgs()
addFlag({ addFlag({
.longName = "argstr", .longName = "argstr",
.description = "Pass the string *string* as the argument *name* to Nix functions.", .description = "Pass the string *string* as the argument *name* to Nix functions.",
.category = category,
.labels = {"name", "string"}, .labels = {"name", "string"},
.handler = {[&](std::string name, std::string s) { autoArgs[name] = 'S' + s; }}, .handler = {[&](std::string name, std::string s) { autoArgs[name] = 'S' + s; }},
}); });
@ -30,6 +34,7 @@ MixEvalArgs::MixEvalArgs()
.longName = "include", .longName = "include",
.shortName = 'I', .shortName = 'I',
.description = "Add *path* to the list of locations used to look up `<...>` file names.", .description = "Add *path* to the list of locations used to look up `<...>` file names.",
.category = category,
.labels = {"path"}, .labels = {"path"},
.handler = {[&](std::string s) { searchPath.push_back(s); }} .handler = {[&](std::string s) { searchPath.push_back(s); }}
}); });
@ -37,6 +42,7 @@ MixEvalArgs::MixEvalArgs()
addFlag({ addFlag({
.longName = "impure", .longName = "impure",
.description = "Allow access to mutable paths and repositories.", .description = "Allow access to mutable paths and repositories.",
.category = category,
.handler = {[&]() { .handler = {[&]() {
evalSettings.pureEval = false; evalSettings.pureEval = false;
}}, }},
@ -45,6 +51,7 @@ MixEvalArgs::MixEvalArgs()
addFlag({ addFlag({
.longName = "override-flake", .longName = "override-flake",
.description = "Override the flake registries, redirecting *original-ref* to *resolved-ref*.", .description = "Override the flake registries, redirecting *original-ref* to *resolved-ref*.",
.category = category,
.labels = {"original-ref", "resolved-ref"}, .labels = {"original-ref", "resolved-ref"},
.handler = {[&](std::string _from, std::string _to) { .handler = {[&](std::string _from, std::string _to) {
auto from = parseFlakeRef(_from, absPath(".")); auto from = parseFlakeRef(_from, absPath("."));

View file

@ -11,18 +11,21 @@ MixCommonArgs::MixCommonArgs(const string & programName)
.longName = "verbose", .longName = "verbose",
.shortName = 'v', .shortName = 'v',
.description = "Increase the logging verbosity level.", .description = "Increase the logging verbosity level.",
.category = loggingCategory,
.handler = {[]() { verbosity = (Verbosity) (verbosity + 1); }}, .handler = {[]() { verbosity = (Verbosity) (verbosity + 1); }},
}); });
addFlag({ addFlag({
.longName = "quiet", .longName = "quiet",
.description = "Decrease the logging verbosity level.", .description = "Decrease the logging verbosity level.",
.category = loggingCategory,
.handler = {[]() { verbosity = verbosity > lvlError ? (Verbosity) (verbosity - 1) : lvlError; }}, .handler = {[]() { verbosity = verbosity > lvlError ? (Verbosity) (verbosity - 1) : lvlError; }},
}); });
addFlag({ addFlag({
.longName = "debug", .longName = "debug",
.description = "Set the logging verbosity level to 'debug'.", .description = "Set the logging verbosity level to 'debug'.",
.category = loggingCategory,
.handler = {[]() { verbosity = lvlDebug; }}, .handler = {[]() { verbosity = lvlDebug; }},
}); });
@ -52,6 +55,7 @@ MixCommonArgs::MixCommonArgs(const string & programName)
addFlag({ addFlag({
.longName = "log-format", .longName = "log-format",
.description = "Set the format of log output; one of `raw`, `internal-json`, `bar` or `bar-with-logs`.", .description = "Set the format of log output; one of `raw`, `internal-json`, `bar` or `bar-with-logs`.",
.category = loggingCategory,
.labels = {"format"}, .labels = {"format"},
.handler = {[](std::string format) { setLogFormat(format); }}, .handler = {[](std::string format) { setLogFormat(format); }},
}); });
@ -66,7 +70,7 @@ MixCommonArgs::MixCommonArgs(const string & programName)
}} }}
}); });
std::string cat = "config"; std::string cat = "Options to override configuration settings";
globalConfig.convertToArgs(*this, cat); globalConfig.convertToArgs(*this, cat);
// Backward compatibility hack: nix-env already had a --system flag. // Backward compatibility hack: nix-env already had a --system flag.

View file

@ -4,6 +4,9 @@
namespace nix { namespace nix {
//static constexpr auto commonArgsCategory = "Miscellaneous common options";
static constexpr auto loggingCategory = "Logging-related options";
struct MixCommonArgs : virtual Args struct MixCommonArgs : virtual Args
{ {
string programName; string programName;
@ -16,7 +19,12 @@ struct MixDryRun : virtual Args
MixDryRun() MixDryRun()
{ {
mkFlag(0, "dry-run", "Show what this command would do without doing it.", &dryRun); addFlag({
.longName = "dry-run",
.description = "Show what this command would do without doing it.",
//.category = commonArgsCategory,
.handler = {&dryRun, true},
});
} }
}; };
@ -26,7 +34,12 @@ struct MixJSON : virtual Args
MixJSON() MixJSON()
{ {
mkFlag(0, "json", "Produce output in JSON format, suitable for consumption by another program.", &json); addFlag({
.longName = "json",
.description = "Produce output in JSON format, suitable for consumption by another program.",
//.category = commonArgsCategory,
.handler = {&json, true},
});
} }
}; };

View file

@ -195,8 +195,7 @@ nlohmann::json Args::toJSON()
j["shortName"] = std::string(1, flag->shortName); j["shortName"] = std::string(1, flag->shortName);
if (flag->description != "") if (flag->description != "")
j["description"] = flag->description; j["description"] = flag->description;
if (flag->category != "") j["category"] = flag->category;
j["category"] = flag->category;
if (flag->handler.arity != ArityAny) if (flag->handler.arity != ArityAny)
j["arity"] = flag->handler.arity; j["arity"] = flag->handler.arity;
if (!flag->labels.empty()) if (!flag->labels.empty())

View file

@ -91,7 +91,7 @@ protected:
{ } { }
}; };
/* Flags. */ /* Options. */
struct Flag struct Flag
{ {
typedef std::shared_ptr<Flag> ptr; typedef std::shared_ptr<Flag> ptr;

View file

@ -61,6 +61,7 @@ StorePathsCommand::StorePathsCommand(bool recursive)
addFlag({ addFlag({
.longName = "no-recursive", .longName = "no-recursive",
.description = "Apply operation to specified paths only.", .description = "Apply operation to specified paths only.",
.category = installablesCategory,
.handler = {&this->recursive, false}, .handler = {&this->recursive, false},
}); });
else else
@ -68,10 +69,16 @@ StorePathsCommand::StorePathsCommand(bool recursive)
.longName = "recursive", .longName = "recursive",
.shortName = 'r', .shortName = 'r',
.description = "Apply operation to closure of the specified paths.", .description = "Apply operation to closure of the specified paths.",
.category = installablesCategory,
.handler = {&this->recursive, true}, .handler = {&this->recursive, true},
}); });
mkFlag(0, "all", "Apply the operation to every store path.", &all); addFlag({
.longName = "all",
.description = "Apply the operation to every store path.",
.category = installablesCategory,
.handler = {&all, true},
});
} }
void StorePathsCommand::run(ref<Store> store) void StorePathsCommand::run(ref<Store> store)

View file

@ -23,6 +23,8 @@ static constexpr Command::Category catSecondary = 100;
static constexpr Command::Category catUtility = 101; static constexpr Command::Category catUtility = 101;
static constexpr Command::Category catNixInstallation = 102; static constexpr Command::Category catNixInstallation = 102;
static constexpr auto installablesCategory = "Options that change the interpretation of installables";
struct NixMultiCommand : virtual MultiCommand, virtual Command struct NixMultiCommand : virtual MultiCommand, virtual Command
{ {
nlohmann::json toJSON() override; nlohmann::json toJSON() override;

View file

@ -58,39 +58,47 @@ void completeFlakeInputPath(
MixFlakeOptions::MixFlakeOptions() MixFlakeOptions::MixFlakeOptions()
{ {
auto category = "Common flake-related options";
addFlag({ addFlag({
.longName = "recreate-lock-file", .longName = "recreate-lock-file",
.description = "Recreate the flake's lock file from scratch.", .description = "Recreate the flake's lock file from scratch.",
.category = category,
.handler = {&lockFlags.recreateLockFile, true} .handler = {&lockFlags.recreateLockFile, true}
}); });
addFlag({ addFlag({
.longName = "no-update-lock-file", .longName = "no-update-lock-file",
.description = "Do not allow any updates to the flake's lock file.", .description = "Do not allow any updates to the flake's lock file.",
.category = category,
.handler = {&lockFlags.updateLockFile, false} .handler = {&lockFlags.updateLockFile, false}
}); });
addFlag({ addFlag({
.longName = "no-write-lock-file", .longName = "no-write-lock-file",
.description = "Do not write the flake's newly generated lock file.", .description = "Do not write the flake's newly generated lock file.",
.category = category,
.handler = {&lockFlags.writeLockFile, false} .handler = {&lockFlags.writeLockFile, false}
}); });
addFlag({ addFlag({
.longName = "no-registries", .longName = "no-registries",
.description = "Don't allow lookups in the flake registries.", .description = "Don't allow lookups in the flake registries.",
.category = category,
.handler = {&lockFlags.useRegistries, false} .handler = {&lockFlags.useRegistries, false}
}); });
addFlag({ addFlag({
.longName = "commit-lock-file", .longName = "commit-lock-file",
.description = "Commit changes to the flake's lock file.", .description = "Commit changes to the flake's lock file.",
.category = category,
.handler = {&lockFlags.commitLockFile, true} .handler = {&lockFlags.commitLockFile, true}
}); });
addFlag({ addFlag({
.longName = "update-input", .longName = "update-input",
.description = "Update a specific flake input (ignoring its previous entry in the lock file).", .description = "Update a specific flake input (ignoring its previous entry in the lock file).",
.category = category,
.labels = {"input-path"}, .labels = {"input-path"},
.handler = {[&](std::string s) { .handler = {[&](std::string s) {
lockFlags.inputUpdates.insert(flake::parseInputPath(s)); lockFlags.inputUpdates.insert(flake::parseInputPath(s));
@ -104,6 +112,7 @@ MixFlakeOptions::MixFlakeOptions()
addFlag({ addFlag({
.longName = "override-input", .longName = "override-input",
.description = "Override a specific flake input (e.g. `dwarffs/nixpkgs`).", .description = "Override a specific flake input (e.g. `dwarffs/nixpkgs`).",
.category = category,
.labels = {"input-path", "flake-url"}, .labels = {"input-path", "flake-url"},
.handler = {[&](std::string inputPath, std::string flakeRef) { .handler = {[&](std::string inputPath, std::string flakeRef) {
lockFlags.inputOverrides.insert_or_assign( lockFlags.inputOverrides.insert_or_assign(
@ -115,6 +124,7 @@ MixFlakeOptions::MixFlakeOptions()
addFlag({ addFlag({
.longName = "inputs-from", .longName = "inputs-from",
.description = "Use the inputs of the specified flake as registry entries.", .description = "Use the inputs of the specified flake as registry entries.",
.category = category,
.labels = {"flake-url"}, .labels = {"flake-url"},
.handler = {[&](std::string flakeRef) { .handler = {[&](std::string flakeRef) {
auto evalState = getEvalState(); auto evalState = getEvalState();
@ -144,6 +154,7 @@ SourceExprCommand::SourceExprCommand()
.longName = "file", .longName = "file",
.shortName = 'f', .shortName = 'f',
.description = "Interpret installables as attribute paths relative to the Nix expression stored in *file*.", .description = "Interpret installables as attribute paths relative to the Nix expression stored in *file*.",
.category = installablesCategory,
.labels = {"file"}, .labels = {"file"},
.handler = {&file}, .handler = {&file},
.completer = completePath .completer = completePath
@ -152,6 +163,7 @@ SourceExprCommand::SourceExprCommand()
addFlag({ addFlag({
.longName = "expr", .longName = "expr",
.description = "Interpret installables as attribute paths relative to the Nix expression *expr*.", .description = "Interpret installables as attribute paths relative to the Nix expression *expr*.",
.category = installablesCategory,
.labels = {"expr"}, .labels = {"expr"},
.handler = {&expr} .handler = {&expr}
}); });
@ -159,6 +171,7 @@ SourceExprCommand::SourceExprCommand()
addFlag({ addFlag({
.longName = "derivation", .longName = "derivation",
.description = "Operate on the store derivation rather than its outputs.", .description = "Operate on the store derivation rather than its outputs.",
.category = installablesCategory,
.handler = {&operateOn, OperateOn::Derivation}, .handler = {&operateOn, OperateOn::Derivation},
}); });
} }

View file

@ -80,6 +80,7 @@ struct NixArgs : virtual MultiCommand, virtual MixCommonArgs
.longName = "print-build-logs", .longName = "print-build-logs",
.shortName = 'L', .shortName = 'L',
.description = "Print full build logs on standard error.", .description = "Print full build logs on standard error.",
.category = loggingCategory,
.handler = {[&]() {setLogFormat(LogFormat::barWithLogs); }}, .handler = {[&]() {setLogFormat(LogFormat::barWithLogs); }},
}); });

View file

@ -16,7 +16,7 @@ struct CmdCopySigs : StorePathsCommand
addFlag({ addFlag({
.longName = "substituter", .longName = "substituter",
.shortName = 's', .shortName = 's',
.description = "Use signatures from specified store.", .description = "Copy signatures from the specified store.",
.labels = {"store-uri"}, .labels = {"store-uri"},
.handler = {[&](std::string s) { substituterUris.push_back(s); }}, .handler = {[&](std::string s) { substituterUris.push_back(s); }},
}); });
@ -24,7 +24,7 @@ struct CmdCopySigs : StorePathsCommand
std::string description() override std::string description() override
{ {
return "copy path signatures from substituters (like binary caches)"; return "copy store path signatures from substituters";
} }
void run(ref<Store> store, StorePaths storePaths) override void run(ref<Store> store, StorePaths storePaths) override
@ -110,7 +110,7 @@ struct CmdSign : StorePathsCommand
std::string description() override std::string description() override
{ {
return "sign the specified paths"; return "sign store paths";
} }
void run(ref<Store> store, StorePaths storePaths) override void run(ref<Store> store, StorePaths storePaths) override