From 8e758d402ba1045c7b8273f8cb1d6d8d917ca52b Mon Sep 17 00:00:00 2001 From: Eelco Dolstra Date: Wed, 27 Jan 2021 12:06:03 +0100 Subject: [PATCH] Remove mkFlag() --- src/libmain/shared.cc | 14 ++++++++++---- src/libutil/args.hh | 21 --------------------- src/nix/eval.cc | 6 +++++- src/nix/hash.cc | 43 +++++++++++++++++++++++++++++++++---------- src/nix/ls.cc | 23 ++++++++++++++++++++--- src/nix/path-info.cc | 30 ++++++++++++++++++++++++++---- src/nix/verify.cc | 13 +++++++++++-- 7 files changed, 105 insertions(+), 45 deletions(-) diff --git a/src/libmain/shared.cc b/src/libmain/shared.cc index 7e27e95c2..5baaff3e9 100644 --- a/src/libmain/shared.cc +++ b/src/libmain/shared.cc @@ -229,11 +229,17 @@ LegacyArgs::LegacyArgs(const std::string & programName, intSettingAlias(0, "max-silent-time", "Number of seconds of silence before a build is killed.", "max-silent-time"); intSettingAlias(0, "timeout", "Number of seconds before a build is killed.", "timeout"); - mkFlag(0, "readonly-mode", "Do not write to the Nix store.", - &settings.readOnlyMode); + addFlag({ + .longName = "readonly-mode", + .description = "Do not write to the Nix store.", + .handler = {&settings.readOnlyMode, true}, + }); - mkFlag(0, "no-gc-warning", "Disable warnings about not using `--add-root`.", - &gcWarning, false); + addFlag({ + .longName = "no-gc-warning", + .description = "Disable warnings about not using `--add-root`.", + .handler = {&gcWarning, true}, + }); addFlag({ .longName = "store", diff --git a/src/libutil/args.hh b/src/libutil/args.hh index b1020b101..42d8515ef 100644 --- a/src/libutil/args.hh +++ b/src/libutil/args.hh @@ -135,27 +135,6 @@ public: void addFlag(Flag && flag); - /* Helper functions for constructing flags / positional - arguments. */ - - void mkFlag(char shortName, const std::string & name, - const std::string & description, bool * dest) - { - mkFlag(shortName, name, description, dest, true); - } - - template - void mkFlag(char shortName, const std::string & longName, const std::string & description, - T * dest, const T & value) - { - addFlag({ - .longName = longName, - .shortName = shortName, - .description = description, - .handler = {[=]() { *dest = value; }} - }); - } - void expectArgs(ExpectedArg && arg) { expectedArgs.emplace_back(std::move(arg)); diff --git a/src/nix/eval.cc b/src/nix/eval.cc index b5049ac65..65d61e005 100644 --- a/src/nix/eval.cc +++ b/src/nix/eval.cc @@ -18,7 +18,11 @@ struct CmdEval : MixJSON, InstallableCommand CmdEval() { - mkFlag(0, "raw", "Print strings without quotes or escaping.", &raw); + addFlag({ + .longName = "raw", + .description = "Print strings without quotes or escaping.", + .handler = {&raw, true}, + }); addFlag({ .longName = "apply", diff --git a/src/nix/hash.cc b/src/nix/hash.cc index 79d506ace..4535e4ab0 100644 --- a/src/nix/hash.cc +++ b/src/nix/hash.cc @@ -19,18 +19,41 @@ struct CmdHashBase : Command CmdHashBase(FileIngestionMethod mode) : mode(mode) { - mkFlag(0, "sri", "Print the hash in SRI format.", &base, SRI); - mkFlag(0, "base64", "Print the hash in base-64 format.", &base, Base64); - mkFlag(0, "base32", "Print the hash in base-32 (Nix-specific) format.", &base, Base32); - mkFlag(0, "base16", "Print the hash in base-16 format.", &base, Base16); + addFlag({ + .longName = "sri", + .description = "Print the hash in SRI format.", + .handler = {&base, SRI}, + }); + + addFlag({ + .longName = "base64", + .description = "Print the hash in base-64 format.", + .handler = {&base, Base64}, + }); + + addFlag({ + .longName = "base32", + .description = "Print the hash in base-32 (Nix-specific) format.", + .handler = {&base, Base32}, + }); + + addFlag({ + .longName = "base16", + .description = "Print the hash in base-16 format.", + .handler = {&base, Base16}, + }); + addFlag(Flag::mkHashTypeFlag("type", &ht)); + #if 0 - mkFlag() - .longName("modulo") - .description("Compute the hash modulo specified the string.") - .labels({"modulus"}) - .dest(&modulus); - #endif + addFlag({ + .longName = "modulo", + .description = "Compute the hash modulo the specified string.", + .labels = {"modulus"}, + .handler = {&modulus}, + }); + #endif\ + expectArgs({ .label = "paths", .handler = {&paths}, diff --git a/src/nix/ls.cc b/src/nix/ls.cc index c0b1ecb32..c1dc9a95b 100644 --- a/src/nix/ls.cc +++ b/src/nix/ls.cc @@ -17,9 +17,26 @@ struct MixLs : virtual Args, MixJSON MixLs() { - mkFlag('R', "recursive", "List subdirectories recursively.", &recursive); - mkFlag('l', "long", "Show detailed file information.", &verbose); - mkFlag('d', "directory", "Show directories rather than their contents.", &showDirectory); + addFlag({ + .longName = "recursive", + .shortName = 'R', + .description = "List subdirectories recursively.", + .handler = {&recursive, true}, + }); + + addFlag({ + .longName = "long", + .shortName = 'l', + .description = "Show detailed file information.", + .handler = {&verbose, true}, + }); + + addFlag({ + .longName = "directory", + .shortName = 'd', + .description = "Show directories rather than their contents.", + .handler = {&showDirectory, true}, + }); } void listText(ref accessor) diff --git a/src/nix/path-info.cc b/src/nix/path-info.cc index 0fa88f1bf..518cd5568 100644 --- a/src/nix/path-info.cc +++ b/src/nix/path-info.cc @@ -18,10 +18,32 @@ struct CmdPathInfo : StorePathsCommand, MixJSON CmdPathInfo() { - mkFlag('s', "size", "Print the size of the NAR serialisation of each path.", &showSize); - mkFlag('S', "closure-size", "Print the sum of the sizes of the NAR serialisations of the closure of each path.", &showClosureSize); - mkFlag('h', "human-readable", "With `-s` and `-S`, print sizes in a human-friendly format such as `5.67G`.", &humanReadable); - mkFlag(0, "sigs", "Show signatures.", &showSigs); + addFlag({ + .longName = "size", + .shortName = 's', + .description = "Print the size of the NAR serialisation of each path.", + .handler = {&showSize, true}, + }); + + addFlag({ + .longName = "closure-size", + .shortName = 'S', + .description = "Print the sum of the sizes of the NAR serialisations of the closure of each path.", + .handler = {&showClosureSize, true}, + }); + + addFlag({ + .longName = "human-readable", + .shortName = 'h', + .description = "With `-s` and `-S`, print sizes in a human-friendly format such as `5.67G`.", + .handler = {&humanReadable, true}, + }); + + addFlag({ + .longName = "sigs", + .description = "Show signatures.", + .handler = {&showSigs, true}, + }); } std::string description() override diff --git a/src/nix/verify.cc b/src/nix/verify.cc index 9b04e032a..1721c7f16 100644 --- a/src/nix/verify.cc +++ b/src/nix/verify.cc @@ -18,8 +18,17 @@ struct CmdVerify : StorePathsCommand CmdVerify() { - mkFlag(0, "no-contents", "Do not verify the contents of each store path.", &noContents); - mkFlag(0, "no-trust", "Do not verify whether each store path is trusted.", &noTrust); + addFlag({ + .longName = "no-contents", + .description = "Do not verify the contents of each store path.", + .handler = {&noContents, true}, + }); + + addFlag({ + .longName = "no-trust", + .description = "Do not verify whether each store path is trusted.", + .handler = {&noTrust, true}, + }); addFlag({ .longName = "substituter",