Nix 2.36.0
Nix, the purely functional package manager; unstable internal interfaces
Loading...
Searching...
No Matches
args.hh
Go to the documentation of this file.
1#pragma once
3
4#include <functional>
5#include <filesystem>
6#include <map>
7#include <memory>
8#include <optional>
9
10#include <nlohmann/json_fwd.hpp>
11
12#include "nix/util/types.hh"
14#include "nix/util/fun.hh"
15#include "nix/util/ref.hh"
16
17namespace nix {
18
19enum struct HashAlgorithm : char;
20enum struct HashFormat : int;
21
22class MultiCommand;
23
24class RootArgs;
25
26class AddCompletions;
27
28class Args
29{
30
31public:
32
36 virtual std::string description()
37 {
38 return "";
39 }
40
41 virtual bool forceImpureByDefault()
42 {
43 return false;
44 }
45
49 virtual std::string doc()
50 {
51 return "";
52 }
53
63 virtual std::filesystem::path getCommandBaseDir() const;
64
65 virtual ~Args() = default;
66
67protected:
68
74 static const size_t ArityAny = std::numeric_limits<size_t>::max();
75
84 struct Handler
85 {
86 std::function<void(std::vector<std::string>)> fun;
87 size_t arity = 0;
88
89 Handler() = default;
90
91 Handler(std::function<void(std::vector<std::string>)> && fun)
92 : fun(std::move(fun))
93 , arity(ArityAny)
94 {
95 }
96
97 Handler(std::function<void()> && handler)
98 : fun([handler{std::move(handler)}](std::vector<std::string>) { handler(); })
99 , arity(0)
100 {
101 }
102
103 Handler(std::function<void(std::string)> && handler)
104 : fun([handler{std::move(handler)}](std::vector<std::string> ss) { handler(std::move(ss[0])); })
105 , arity(1)
106 {
107 }
108
109 Handler(std::function<void(std::string, std::string)> && handler)
110 : fun([handler{std::move(handler)}](std::vector<std::string> ss) {
111 handler(std::move(ss[0]), std::move(ss[1]));
112 })
113 , arity(2)
114 {
115 }
116
117 Handler(std::vector<std::string> * dest)
118 : fun([dest](std::vector<std::string> ss) { *dest = ss; })
119 , arity(ArityAny)
120 {
121 }
122
123 Handler(std::string * dest)
124 : fun([dest](std::vector<std::string> ss) { *dest = ss[0]; })
125 , arity(1)
126 {
127 }
128
129 Handler(std::optional<std::string> * dest)
130 : fun([dest](std::vector<std::string> ss) { *dest = ss[0]; })
131 , arity(1)
132 {
133 }
134
135 Handler(std::filesystem::path * dest)
136 : fun([dest](std::vector<std::string> ss) { *dest = ss[0]; })
137 , arity(1)
138 {
139 }
140
141 Handler(std::optional<std::filesystem::path> * dest)
142 : fun([dest](std::vector<std::string> ss) { *dest = ss[0]; })
143 , arity(1)
144 {
145 }
146
147 template<class T>
148 Handler(T * dest, const T & val)
149 : fun([dest, val](std::vector<std::string> ss) { *dest = val; })
150 , arity(0)
151 {
152 }
153
154 template<class I>
155 Handler(I * dest)
156 : fun([dest](std::vector<std::string> ss) { *dest = string2IntWithUnitPrefix<I>(ss[0]); })
157 , arity(1)
158 {
159 }
160
161 template<class I>
162 Handler(std::optional<I> * dest)
163 : fun([dest](std::vector<std::string> ss) { *dest = string2IntWithUnitPrefix<I>(ss[0]); })
164 , arity(1)
165 {
166 }
167 };
168
178 using CompleterFun = void(AddCompletions &, size_t, std::string_view);
179
186 using CompleterClosure = std::function<CompleterFun>;
187
188public:
189
196 struct Flag
197 {
198 using ptr = std::shared_ptr<Flag>;
199
200 std::string longName;
201 StringSet aliases;
202 char shortName = 0;
203 std::string description;
204 std::string category;
205 Strings labels;
206 Handler handler;
207 CompleterClosure completer;
208 bool required = false;
209
210 std::optional<ExperimentalFeature> experimentalFeature;
211
212 // FIXME: this should be private, but that breaks designated initializers.
213 size_t timesUsed = 0;
214 };
215
216protected:
217
222 std::map<std::string, Flag::ptr> longFlags;
223
228 std::map<char, Flag::ptr> shortFlags;
229
234 virtual bool processFlag(Strings::iterator & pos, Strings::iterator end);
235
236public:
237
245 {
246 std::string label;
247 bool optional = false;
248 Handler handler;
249 CompleterClosure completer;
250 };
251
252protected:
253
263 std::list<ExpectedArg> expectedArgs;
275 std::list<ExpectedArg> processedArgs;
276
284 virtual bool processArgs(const Strings & args, bool finish);
285
286 virtual Strings::iterator rewriteArgs(Strings & args, Strings::iterator pos)
287 {
288 return pos;
289 }
290
291 StringSet hiddenCategories;
292
293 virtual void checkArgs();
294
299 virtual void initialFlagsProcessed() {}
300
301public:
302
303 void addFlag(Flag && flag);
304
305 void removeFlag(const std::string & longName);
306
307 void expectArgs(ExpectedArg && arg)
308 {
309 expectedArgs.emplace_back(std::move(arg));
310 }
311
315 void expectArg(const std::string & label, std::string * dest, bool optional = false)
316 {
317 expectArgs({.label = label, .optional = optional, .handler = {dest}});
318 }
319
323 void expectArg(const std::string & label, std::filesystem::path * dest, bool optional = false)
324 {
325 expectArgs({.label = label, .optional = optional, .handler = {dest}});
326 }
327
331 void expectArgs(const std::string & label, std::vector<std::string> * dest)
332 {
333 expectArgs({.label = label, .handler = {dest}});
334 }
335
336 static CompleterFun completePath;
337
338 static CompleterFun completeDir;
339
340 virtual nlohmann::json toJSON();
341
342 friend class MultiCommand;
343
353 MultiCommand * parent = nullptr;
354
359 RootArgs & getRoot();
360};
361
366struct Command : virtual public Args
367{
368 friend class MultiCommand;
369
370 virtual ~Command() = default;
371
375 virtual void run() = 0;
376
377 using Category = int;
378
379 static constexpr Category catDefault = 0;
380
381 virtual std::optional<ExperimentalFeature> experimentalFeature();
382
383 virtual Category category()
384 {
385 return catDefault;
386 }
387};
388
389using Commands = std::map<std::string, fun<ref<Command>()>>;
390
395class MultiCommand : virtual public Args
396{
397public:
398 Commands commands;
399
400 std::map<Command::Category, std::string> categories;
401
405 std::optional<std::pair<std::string, ref<Command>>> command;
406
407 MultiCommand(std::string_view commandName, const Commands & commands);
408
409 bool processFlag(Strings::iterator & pos, Strings::iterator end) override;
410
411 bool processArgs(const Strings & args, bool finish) override;
412
413 nlohmann::json toJSON() override;
414
415 enum struct AliasStatus {
417 AcceptedShorthand,
419 Deprecated,
421 LinguisticVariation,
422 };
423
426 {
427 AliasStatus status;
428 std::vector<std::string> replacement;
429 };
430
435 std::map<std::string, AliasInfo> aliases;
436
437 Strings::iterator rewriteArgs(Strings & args, Strings::iterator pos) override;
438
439protected:
440 std::string commandName = "";
441 bool aliasUsed = false;
442
443 void checkArgs() override;
444};
445
446Strings argvToStrings(int argc, char ** argv);
447
449{
450 std::string completion;
451 std::string description;
452
453 auto operator<=>(const Completion & other) const noexcept;
454};
455
466{
467 /* VTable anchor to avoid weak linkage of the vtable - it breaks
468 dynamic_cast across shared libraries on Darwin. */
469 virtual void anchor();
470public:
471
475 enum class Type {
476 Normal,
477 Filenames,
478 Attrs,
479 };
480
486 virtual void setType(Type type) = 0;
487
491 virtual void add(std::string completion, std::string description = "") = 0;
492
493 virtual ~AddCompletions() = default;
494};
495
496Strings parseShebangContent(std::string_view s);
497
498} // namespace nix
Definition args.hh:466
virtual void setType(Type type)=0
virtual void add(std::string completion, std::string description="")=0
Type
Definition args.hh:475
Definition args.hh:29
MultiCommand * parent
Definition args.hh:353
virtual std::filesystem::path getCommandBaseDir() const
Get the base directory for the command.
Definition args.cc:377
virtual bool processFlag(Strings::iterator &pos, Strings::iterator end)
Definition args.cc:388
std::list< ExpectedArg > expectedArgs
Definition args.hh:263
std::function< CompleterFun > CompleterClosure
Definition args.hh:186
std::list< ExpectedArg > processedArgs
Definition args.hh:275
virtual std::string description()
Definition args.hh:36
RootArgs & getRoot()
Definition args.cc:66
void expectArgs(const std::string &label, std::vector< std::string > *dest)
Definition args.hh:331
virtual std::string doc()
Definition args.hh:49
void expectArg(const std::string &label, std::filesystem::path *dest, bool optional=false)
Definition args.hh:323
void(AddCompletions &, size_t, std::string_view) CompleterFun
Definition args.hh:178
virtual void initialFlagsProcessed()
Definition args.hh:299
std::map< char, Flag::ptr > shortFlags
Definition args.hh:228
virtual bool processArgs(const Strings &args, bool finish)
Definition args.cc:464
std::map< std::string, Flag::ptr > longFlags
Definition args.hh:222
void expectArg(const std::string &label, std::string *dest, bool optional=false)
Definition args.hh:315
static const size_t ArityAny
Definition args.hh:74
Definition args.hh:396
bool processFlag(Strings::iterator &pos, Strings::iterator end) override
Definition args.cc:645
std::optional< std::pair< std::string, ref< Command > > > command
Definition args.hh:405
bool processArgs(const Strings &args, bool finish) override
Definition args.cc:654
std::map< std::string, AliasInfo > aliases
Definition args.hh:435
AliasStatus
Definition args.hh:415
Definition root.hh:31
HashFormat
Enumeration representing the hash formats.
Definition hash.hh:42
@ Normal
Definition sqlite.hh:20
Definition args.hh:245
Definition args.hh:197
Definition args.hh:85
Definition args.hh:367
virtual void run()=0
Definition args.hh:449
Definition args.hh:426
std::set< std::string, std::less<> > StringSet
Definition types.hh:45
N string2IntWithUnitPrefix(std::string_view s)
Definition util.hh:111