nix eval: Add a --raw flag

Similar to "jq -r", this prints the evaluation result (which must be a
string value) unquoted.
This commit is contained in:
Eelco Dolstra 2017-05-03 14:08:18 +02:00
parent d3dcdfa006
commit 782c0bff45
No known key found for this signature in database
GPG key ID: 8170B4726D7198DE

View file

@ -10,6 +10,13 @@ using namespace nix;
struct CmdEval : MixJSON, InstallablesCommand struct CmdEval : MixJSON, InstallablesCommand
{ {
bool raw = false;
CmdEval()
{
mkFlag(0, "raw", "print strings unquoted", &raw);
}
std::string name() override std::string name() override
{ {
return "eval"; return "eval";
@ -22,13 +29,18 @@ struct CmdEval : MixJSON, InstallablesCommand
void run(ref<Store> store) override void run(ref<Store> store) override
{ {
if (raw && json)
throw UsageError("--raw and --json are mutually exclusive");
auto state = getEvalState(); auto state = getEvalState();
auto jsonOut = json ? std::make_unique<JSONList>(std::cout) : nullptr; auto jsonOut = json ? std::make_unique<JSONList>(std::cout) : nullptr;
for (auto & i : installables) { for (auto & i : installables) {
auto v = i->toValue(*state); auto v = i->toValue(*state);
if (json) { if (raw) {
std::cout << state->forceString(*v);
} else if (json) {
PathSet context; PathSet context;
auto jsonElem = jsonOut->placeholder(); auto jsonElem = jsonOut->placeholder();
printValueAsJSON(*state, true, *v, jsonElem, context); printValueAsJSON(*state, true, *v, jsonElem, context);