comparison CSP2/CSP2_env/env-d9b9114564458d9d-741b3de822f2aaca6c6caa4325c4afce/include/kj/main.h @ 69:33d812a61356

planemo upload commit 2e9511a184a1ca667c7be0c6321a36dc4e3d116d
author jpayne
date Tue, 18 Mar 2025 17:55:14 -0400
parents
children
comparison
equal deleted inserted replaced
67:0e9998148a16 69:33d812a61356
1 // Copyright (c) 2013-2014 Sandstorm Development Group, Inc. and contributors
2 // Licensed under the MIT License:
3 //
4 // Permission is hereby granted, free of charge, to any person obtaining a copy
5 // of this software and associated documentation files (the "Software"), to deal
6 // in the Software without restriction, including without limitation the rights
7 // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8 // copies of the Software, and to permit persons to whom the Software is
9 // furnished to do so, subject to the following conditions:
10 //
11 // The above copyright notice and this permission notice shall be included in
12 // all copies or substantial portions of the Software.
13 //
14 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17 // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19 // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
20 // THE SOFTWARE.
21
22 #pragma once
23
24 #include "array.h"
25 #include "string.h"
26 #include "vector.h"
27 #include "function.h"
28
29 KJ_BEGIN_HEADER
30
31 namespace kj {
32
33 class ProcessContext {
34 // Context for command-line programs.
35
36 public:
37 virtual StringPtr getProgramName() = 0;
38 // Get argv[0] as passed to main().
39
40 KJ_NORETURN(virtual void exit()) = 0;
41 // Indicates program completion. The program is considered successful unless `error()` was
42 // called. Typically this exits with _Exit(), meaning that the stack is not unwound, buffers
43 // are not flushed, etc. -- it is the responsibility of the caller to flush any buffers that
44 // matter. However, an alternate context implementation e.g. for unit testing purposes could
45 // choose to throw an exception instead.
46 //
47 // At first this approach may sound crazy. Isn't it much better to shut down cleanly? What if
48 // you lose data? However, it turns out that if you look at each common class of program, _Exit()
49 // is almost always preferable. Let's break it down:
50 //
51 // * Commands: A typical program you might run from the command line is single-threaded and
52 // exits quickly and deterministically. Commands often use buffered I/O and need to flush
53 // those buffers before exit. However, most of the work performed by destructors is not
54 // flushing buffers, but rather freeing up memory, placing objects into freelists, and closing
55 // file descriptors. All of this is irrelevant if the process is about to exit anyway, and
56 // for a command that runs quickly, time wasted freeing heap space may make a real difference
57 // in the overall runtime of a script. Meanwhile, it is usually easy to determine exactly what
58 // resources need to be flushed before exit, and easy to tell if they are not being flushed
59 // (because the command fails to produce the expected output). Therefore, it is reasonably
60 // easy for commands to explicitly ensure all output is flushed before exiting, and it is
61 // probably a good idea for them to do so anyway, because write failures should be detected
62 // and handled. For commands, a good strategy is to allocate any objects that require clean
63 // destruction on the stack, and allow them to go out of scope before the command exits.
64 // Meanwhile, any resources which do not need to be cleaned up should be allocated as members
65 // of the command's main class, whose destructor normally will not be called.
66 //
67 // * Interactive apps: Programs that interact with the user (whether they be graphical apps
68 // with windows or console-based apps like emacs) generally exit only when the user asks them
69 // to. Such applications may store large data structures in memory which need to be synced
70 // to disk, such as documents or user preferences. However, relying on stack unwind or global
71 // destructors as the mechanism for ensuring such syncing occurs is probably wrong. First of
72 // all, it's 2013, and applications ought to be actively syncing changes to non-volatile
73 // storage the moment those changes are made. Applications can crash at any time and a crash
74 // should never lose data that is more than half a second old. Meanwhile, if a user actually
75 // does try to close an application while unsaved changes exist, the application UI should
76 // prompt the user to decide what to do. Such a UI mechanism is obviously too high level to
77 // be implemented via destructors, so KJ's use of _Exit() shouldn't make a difference here.
78 //
79 // * Servers: A good server is fault-tolerant, prepared for the possibility that at any time
80 // it could crash, the OS could decide to kill it off, or the machine it is running on could
81 // just die. So, using _Exit() should be no problem. In fact, servers generally never even
82 // call exit anyway; they are killed externally.
83 //
84 // * Batch jobs: A long-running batch job is something between a command and a server. It
85 // probably knows exactly what needs to be flushed before exiting, and it probably should be
86 // fault-tolerant.
87 //
88 // Meanwhile, regardless of program type, if you are adhering to KJ style, then the use of
89 // _Exit() shouldn't be a problem anyway:
90 //
91 // * KJ style forbids global mutable state (singletons) in general and global constructors and
92 // destructors in particular. Therefore, everything that could possibly need cleanup either
93 // lives on the stack or is transitively owned by something living on the stack.
94 //
95 // * Calling exit() simply means "Don't clean up anything older than this stack frame.". If you
96 // have resources that require cleanup before exit, make sure they are owned by stack frames
97 // beyond the one that eventually calls exit(). To be as safe as possible, don't place any
98 // state in your program's main class, and don't call exit() yourself. Then, runMainAndExit()
99 // will do it, and the only thing on the stack at that time will be your main class, which
100 // has no state anyway.
101 //
102 // TODO(someday): Perhaps we should use the new std::quick_exit(), so that at_quick_exit() is
103 // available for those who really think they need it. Unfortunately, it is not yet available
104 // on many platforms.
105
106 virtual void warning(StringPtr message) = 0;
107 // Print the given message to standard error. A newline is printed after the message if it
108 // doesn't already have one.
109
110 virtual void error(StringPtr message) = 0;
111 // Like `warning()`, but also sets a flag indicating that the process has failed, and that when
112 // it eventually exits it should indicate an error status.
113
114 KJ_NORETURN(virtual void exitError(StringPtr message)) = 0;
115 // Equivalent to `error(message)` followed by `exit()`.
116
117 KJ_NORETURN(virtual void exitInfo(StringPtr message)) = 0;
118 // Displays the given non-error message to the user and then calls `exit()`. This is used to
119 // implement things like --help.
120
121 virtual void increaseLoggingVerbosity() = 0;
122 // Increase the level of detail produced by the debug logging system. `MainBuilder` invokes
123 // this if the caller uses the -v flag.
124
125 // TODO(someday): Add interfaces representing standard OS resources like the filesystem, so that
126 // these things can be mocked out.
127 };
128
129 class TopLevelProcessContext final: public ProcessContext {
130 // A ProcessContext implementation appropriate for use at the actual entry point of a process
131 // (as opposed to when you are trying to call a program's main function from within some other
132 // program). This implementation writes errors to stderr, and its `exit()` method actually
133 // calls the C `quick_exit()` function.
134
135 public:
136 explicit TopLevelProcessContext(StringPtr programName);
137
138 struct CleanShutdownException { int exitCode; };
139 // If the environment variable KJ_CLEAN_SHUTDOWN is set, then exit() will actually throw this
140 // exception rather than exiting. `kj::runMain()` catches this exception and returns normally.
141 // This is useful primarily for testing purposes, to assist tools like memory leak checkers that
142 // are easily confused by quick_exit().
143
144 StringPtr getProgramName() override;
145 KJ_NORETURN(void exit() override);
146 void warning(StringPtr message) override;
147 void error(StringPtr message) override;
148 KJ_NORETURN(void exitError(StringPtr message) override);
149 KJ_NORETURN(void exitInfo(StringPtr message) override);
150 void increaseLoggingVerbosity() override;
151
152 private:
153 StringPtr programName;
154 bool cleanShutdown;
155 bool hadErrors = false;
156 };
157
158 typedef Function<void(StringPtr programName, ArrayPtr<const StringPtr> params)> MainFunc;
159
160 int runMainAndExit(ProcessContext& context, MainFunc&& func, int argc, char* argv[]);
161 // Runs the given main function and then exits using the given context. If an exception is thrown,
162 // this will catch it, report it via the context and exit with an error code.
163 //
164 // Normally this function does not return, because returning would probably lead to wasting time
165 // on cleanup when the process is just going to exit anyway. However, to facilitate memory leak
166 // checkers and other tools that require a clean shutdown to do their job, if the environment
167 // variable KJ_CLEAN_SHUTDOWN is set, the function will in fact return an exit code, which should
168 // then be returned from main().
169 //
170 // Most users will use the KJ_MAIN() macro rather than call this function directly.
171
172 #define KJ_MAIN(MainClass) \
173 int main(int argc, char* argv[]) { \
174 ::kj::TopLevelProcessContext context(argv[0]); \
175 MainClass mainObject(context); \
176 return ::kj::runMainAndExit(context, mainObject.getMain(), argc, argv); \
177 }
178 // Convenience macro for declaring a main function based on the given class. The class must have
179 // a constructor that accepts a ProcessContext& and a method getMain() which returns
180 // kj::MainFunc (probably building it using a MainBuilder).
181
182 class MainBuilder {
183 // Builds a main() function with nice argument parsing. As options and arguments are parsed,
184 // corresponding callbacks are called, so that you never have to write a massive switch()
185 // statement to interpret arguments. Additionally, this approach encourages you to write
186 // main classes that have a reasonable API that can be used as an alternative to their
187 // command-line interface.
188 //
189 // All StringPtrs passed to MainBuilder must remain valid until option parsing completes. The
190 // assumption is that these strings will all be literals, making this an easy requirement. If
191 // not, consider allocating them in an Arena.
192 //
193 // Some flags are automatically recognized by the main functions built by this class:
194 // --help: Prints help text and exits. The help text is constructed based on the
195 // information you provide to the builder as you define each flag.
196 // --verbose: Increase logging verbosity.
197 // --version: Print version information and exit.
198 //
199 // Example usage:
200 //
201 // class FooMain {
202 // public:
203 // FooMain(kj::ProcessContext& context): context(context) {}
204 //
205 // bool setAll() { all = true; return true; }
206 // // Enable the --all flag.
207 //
208 // kj::MainBuilder::Validity setOutput(kj::StringPtr name) {
209 // // Set the output file.
210 //
211 // if (name.endsWith(".foo")) {
212 // outputFile = name;
213 // return true;
214 // } else {
215 // return "Output file must have extension .foo.";
216 // }
217 // }
218 //
219 // kj::MainBuilder::Validity processInput(kj::StringPtr name) {
220 // // Process an input file.
221 //
222 // if (!exists(name)) {
223 // return kj::str(name, ": file not found");
224 // }
225 // // ... process the input file ...
226 // return true;
227 // }
228 //
229 // kj::MainFunc getMain() {
230 // return MainBuilder(context, "Foo Builder v1.5", "Reads <source>s and builds a Foo.")
231 // .addOption({'a', "all"}, KJ_BIND_METHOD(*this, setAll),
232 // "Frob all the widgets. Otherwise, only some widgets are frobbed.")
233 // .addOptionWithArg({'o', "output"}, KJ_BIND_METHOD(*this, setOutput),
234 // "<filename>", "Output to <filename>. Must be a .foo file.")
235 // .expectOneOrMoreArgs("<source>", KJ_BIND_METHOD(*this, processInput))
236 // .build();
237 // }
238 //
239 // private:
240 // bool all = false;
241 // kj::StringPtr outputFile;
242 // kj::ProcessContext& context;
243 // };
244
245 public:
246 MainBuilder(ProcessContext& context, StringPtr version,
247 StringPtr briefDescription, StringPtr extendedDescription = nullptr);
248 ~MainBuilder() noexcept(false);
249
250 class OptionName {
251 public:
252 OptionName() = default;
253 inline OptionName(char shortName): isLong(false), shortName(shortName) {}
254 inline OptionName(const char* longName): isLong(true), longName(longName) {}
255
256 private:
257 bool isLong;
258 union {
259 char shortName;
260 const char* longName;
261 };
262 friend class MainBuilder;
263 };
264
265 class Validity {
266 public:
267 inline Validity(bool valid) {
268 if (!valid) errorMessage = heapString("invalid argument");
269 }
270 inline Validity(const char* errorMessage)
271 : errorMessage(heapString(errorMessage)) {}
272 inline Validity(String&& errorMessage)
273 : errorMessage(kj::mv(errorMessage)) {}
274
275 inline const Maybe<String>& getError() const { return errorMessage; }
276 inline Maybe<String> releaseError() { return kj::mv(errorMessage); }
277
278 private:
279 Maybe<String> errorMessage;
280 friend class MainBuilder;
281 };
282
283 MainBuilder& addOption(std::initializer_list<OptionName> names, Function<Validity()> callback,
284 StringPtr helpText);
285 // Defines a new option (flag). `names` is a list of characters and strings that can be used to
286 // specify the option on the command line. Single-character names are used with "-" while string
287 // names are used with "--". `helpText` is a natural-language description of the flag.
288 //
289 // `callback` is called when the option is seen. Its return value indicates whether the option
290 // was accepted. If not, further option processing stops, and error is written, and the process
291 // exits.
292 //
293 // Example:
294 //
295 // builder.addOption({'a', "all"}, KJ_BIND_METHOD(*this, showAll), "Show all files.");
296 //
297 // This option could be specified in the following ways:
298 //
299 // -a
300 // --all
301 //
302 // Note that single-character option names can be combined into a single argument. For example,
303 // `-abcd` is equivalent to `-a -b -c -d`.
304 //
305 // The help text for this option would look like:
306 //
307 // -a, --all
308 // Show all files.
309 //
310 // Note that help text is automatically word-wrapped.
311
312 MainBuilder& addOptionWithArg(std::initializer_list<OptionName> names,
313 Function<Validity(StringPtr)> callback,
314 StringPtr argumentTitle, StringPtr helpText);
315 // Like `addOption()`, but adds an option which accepts an argument. `argumentTitle` is used in
316 // the help text. The argument text is passed to the callback.
317 //
318 // Example:
319 //
320 // builder.addOptionWithArg({'o', "output"}, KJ_BIND_METHOD(*this, setOutput),
321 // "<filename>", "Output to <filename>.");
322 //
323 // This option could be specified with an argument of "foo" in the following ways:
324 //
325 // -ofoo
326 // -o foo
327 // --output=foo
328 // --output foo
329 //
330 // Note that single-character option names can be combined, but only the last option can have an
331 // argument, since the characters after the option letter are interpreted as the argument. E.g.
332 // `-abofoo` would be equivalent to `-a -b -o foo`.
333 //
334 // The help text for this option would look like:
335 //
336 // -o FILENAME, --output=FILENAME
337 // Output to FILENAME.
338
339 MainBuilder& addSubCommand(StringPtr name, Function<MainFunc()> getSubParser,
340 StringPtr briefHelpText);
341 // If exactly the given name is seen as an argument, invoke getSubParser() and then pass all
342 // remaining arguments to the parser it returns. This is useful for implementing commands which
343 // have lots of sub-commands, like "git" (which has sub-commands "checkout", "branch", "pull",
344 // etc.).
345 //
346 // `getSubParser` is only called if the command is seen. This avoids building main functions
347 // for commands that aren't used.
348 //
349 // `briefHelpText` should be brief enough to show immediately after the command name on a single
350 // line. It will not be wrapped. Users can use the built-in "help" command to get extended
351 // help on a particular command.
352
353 MainBuilder& expectArg(StringPtr title, Function<Validity(StringPtr)> callback);
354 MainBuilder& expectOptionalArg(StringPtr title, Function<Validity(StringPtr)> callback);
355 MainBuilder& expectZeroOrMoreArgs(StringPtr title, Function<Validity(StringPtr)> callback);
356 MainBuilder& expectOneOrMoreArgs(StringPtr title, Function<Validity(StringPtr)> callback);
357 // Set callbacks to handle arguments. `expectArg()` and `expectOptionalArg()` specify positional
358 // arguments with special handling, while `expect{Zero,One}OrMoreArgs()` specifies a handler for
359 // an argument list (the handler is called once for each argument in the list). `title`
360 // specifies how the argument should be represented in the usage text.
361 //
362 // All options callbacks are called before argument callbacks, regardless of their ordering on
363 // the command line. This matches GNU getopt's behavior of permuting non-flag arguments to the
364 // end of the argument list. Also matching getopt, the special option "--" indicates that the
365 // rest of the command line is all arguments, not options, even if they start with '-'.
366 //
367 // The interpretation of positional arguments is fairly flexible. The non-optional arguments can
368 // be expected at the beginning, end, or in the middle. If more arguments are specified than
369 // the number of non-optional args, they are assigned to the optional argument handlers in the
370 // order of registration.
371 //
372 // For example, say you called:
373 // builder.expectArg("<foo>", ...);
374 // builder.expectOptionalArg("<bar>", ...);
375 // builder.expectArg("<baz>", ...);
376 // builder.expectZeroOrMoreArgs("<qux>", ...);
377 // builder.expectArg("<corge>", ...);
378 //
379 // This command requires at least three arguments: foo, baz, and corge. If four arguments are
380 // given, the second is assigned to bar. If five or more arguments are specified, then the
381 // arguments between the third and last are assigned to qux. Note that it never makes sense
382 // to call `expect*OrMoreArgs()` more than once since only the first call would ever be used.
383 //
384 // In practice, you probably shouldn't create such complicated commands as in the above example.
385 // But, this flexibility seems necessary to support commands where the first argument is special
386 // as well as commands (like `cp`) where the last argument is special.
387
388 MainBuilder& callAfterParsing(Function<Validity()> callback);
389 // Call the given function after all arguments have been parsed.
390
391 MainFunc build();
392 // Build the "main" function, which simply parses the arguments. Once this returns, the
393 // `MainBuilder` is no longer valid.
394
395 private:
396 struct Impl;
397 Own<Impl> impl;
398
399 class MainImpl;
400 };
401
402 } // namespace kj
403
404 KJ_END_HEADER