From b8283773bd64d7da6859ed520ee19867742a03ba Mon Sep 17 00:00:00 2001 From: Eelco Dolstra Date: Wed, 7 Jun 2017 16:17:17 +0200 Subject: nix: Make all options available as flags MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Thus, instead of ‘--option ’, you can write ‘-- ’. So --option http-connections 100 becomes --http-connections 100 Apart from brevity, the difference is that it's not an error to set a non-existent option via --option, but unrecognized arguments are fatal. Boolean options have special treatment: they're mapped to the argument-less flags ‘--’ and ‘--no-’. E.g. --option auto-optimise-store false becomes --no-auto-optimise-store --- src/libutil/config.cc | 25 +++++++++++++++++++++++++ src/libutil/config.hh | 6 ++++++ 2 files changed, 31 insertions(+) (limited to 'src/libutil') diff --git a/src/libutil/config.cc b/src/libutil/config.cc index f7a46bfee63f..612fb6e68350 100644 --- a/src/libutil/config.cc +++ b/src/libutil/config.cc @@ -115,6 +115,13 @@ void Config::toJSON(JSONObject & out) } } +void Config::convertToArgs(Args & args) +{ + for (auto & s : _settings) + if (!s.second.isAlias) + s.second.setting->convertToArg(args); +} + AbstractSetting::AbstractSetting( const std::string & name, const std::string & description, @@ -128,12 +135,22 @@ void AbstractSetting::toJSON(JSONPlaceholder & out) out.write(to_string()); } +void AbstractSetting::convertToArg(Args & args) +{ +} + template void BaseSetting::toJSON(JSONPlaceholder & out) { out.write(value); } +template +void BaseSetting::convertToArg(Args & args) +{ + args.mkFlag(0, name, {}, description, 1, [=](Strings ss) { set(*ss.begin()); }); +} + template<> void BaseSetting::set(const std::string & str) { value = str; @@ -174,6 +191,12 @@ template<> std::string BaseSetting::to_string() return value ? "true" : "false"; } +template<> void BaseSetting::convertToArg(Args & args) +{ + args.mkFlag(0, name, {}, description, 0, [=](Strings ss) { value = true; }); + args.mkFlag(0, "no-" + name, {}, description, 0, [=](Strings ss) { value = false; }); +} + template<> void BaseSetting::set(const std::string & str) { value = tokenizeString(str); @@ -216,6 +239,8 @@ template class BaseSetting; template class BaseSetting; template class BaseSetting; template class BaseSetting; +template class BaseSetting; +template class BaseSetting; void PathSetting::set(const std::string & str) { diff --git a/src/libutil/config.hh b/src/libutil/config.hh index 77620d47d371..2ca643fe0e71 100644 --- a/src/libutil/config.hh +++ b/src/libutil/config.hh @@ -63,6 +63,8 @@ public: void resetOverriden(); void toJSON(JSONObject & out); + + void convertToArgs(Args & args); }; class AbstractSetting @@ -99,6 +101,8 @@ protected: virtual void toJSON(JSONPlaceholder & out); + virtual void convertToArg(Args & args); + bool isOverriden() { return overriden; } }; @@ -132,6 +136,8 @@ public: std::string to_string() override; + void convertToArg(Args & args) override; + void toJSON(JSONPlaceholder & out) override; }; -- cgit 1.4.1