Nix 2.36.0
Nix, the purely functional package manager; unstable internal interfaces
Loading...
Searching...
No Matches
common-args.hh
Go to the documentation of this file.
1#pragma once
3
4#include "nix/util/args.hh"
7
8namespace nix {
9
10// static constexpr auto commonArgsCategory = "Miscellaneous common options";
11static constexpr auto loggingCategory = "Logging-related options";
12static constexpr auto miscCategory = "Miscellaneous global options";
13
14class MixCommonArgs : public virtual Args
15{
16 void initialFlagsProcessed() override;
17public:
18 std::string programName;
19 MixCommonArgs(const std::string & programName);
20protected:
21 virtual void pluginsInited() {}
22};
23
24struct MixDryRun : virtual Args
25{
26 bool dryRun = false;
27
28 MixDryRun()
29 {
30 addFlag({
31 .longName = "dry-run",
32 .description = "Show what this command would do without doing it.",
33 .handler = {&dryRun, true},
34 });
35 }
36};
37
45struct MixPrintJSON : virtual Args
46{
47 bool outputPretty = isTTY(getStandardOutput());
48
49 MixPrintJSON()
50 {
51 addFlag({
52 .longName = "pretty",
53 .description =
54 R"(
55 Print multi-line, indented JSON output for readability.
56
57 Default: indent if output is to a terminal.
58
59 This option is only effective when `--json` is also specified.
60 )",
61 .handler = {&outputPretty, true},
62 });
63 addFlag({
64 .longName = "no-pretty",
65 .description =
66 R"(
67 Print compact JSON output on a single line, even when the output is a terminal.
68 Some commands may print multiple JSON objects on separate lines.
69
70 See `--pretty`.
71 )",
72 .handler = {&outputPretty, false},
73 });
74 };
75
90 template<typename T, typename = std::enable_if_t<std::is_same_v<T, nlohmann::json>>>
91 void printJSON(const T & json);
92};
93
95struct MixJSON : virtual Args, virtual MixPrintJSON
96{
97 bool json = false;
98
99 MixJSON()
100 {
101 addFlag({
102 .longName = "json",
103 .description = "Produce output in JSON format, suitable for consumption by another program.",
104 .handler = {&json, true},
105 });
106 }
107};
108
109struct MixRepair : virtual Args
110{
111 RepairFlag repair = NoRepair;
112
113 MixRepair()
114 {
115 addFlag({
116 .longName = "repair",
117 .description = "During evaluation, rewrite missing or corrupted files in the Nix store. "
118 "During building, rebuild missing or corrupted store paths.",
119 .category = miscCategory,
120 .handler = {&repair, Repair},
121 });
122 }
123};
124
125} // namespace nix
Definition args.hh:29
Descriptor getStandardOutput()
Definition file-descriptor.hh:239
void printJSON(const T &json)
Definition common-args.cc:103
bool isTTY(Descriptor fd)
Definition terminal.cc:68