Skip to content

Instantly share code, notes, and snippets.

@justephens
Created February 15, 2021 17:41
Show Gist options
  • Select an option

  • Save justephens/47feefbb3bfa97ee9ff3e99f552d22b3 to your computer and use it in GitHub Desktop.

Select an option

Save justephens/47feefbb3bfa97ee9ff3e99f552d22b3 to your computer and use it in GitHub Desktop.
Template for well-documented D scripts that run under rdmd
#!/usr/bin/env rdmd
/++
+ d_script_template.d
+
+ This file serves as a basic template for creating well-documented scripts in
+ D running through rdmd (https://dlang.org/rdmd.html).
+
+ This template uses std.getopt for command line arguments, and parses its own
+ source to output the contents of the top-most documentation block comment
+ (++ style comments) as part of the --help dialog.
+
+ Replace this text with a summary of and documentation for your script.
+
+ USAGE:
+ d_script_template.d [OPTIONS]
++/
import std.algorithm;
import std.array;
import std.stdio;
import std.file;
import std.getopt;
import core.stdc.stdlib;
string arg;
bool flag;
enum Mode { A, B, C };
Mode mode;
void main(string[] argv)
{
// Set up commandline options
auto cmdOpts = getopt(argv, std.getopt.config.bundling,
"arg|a", "a string argument", &arg,
"flag|f", "a true/false flag", &flag,
"mode|m", "an enum-based selection", &mode);
// Print global documentation blocks and option descriptions on '--help'
if (cmdOpts.helpWanted)
__FILE_FULL_PATH__.readText().findSplitAfter("/++")[1].findSplitBefore("++/")[0].strip('\n').split('\n')
.map!(a => a.findSplitAfter(" + ")[1]).map!(a => a.findSplitAfter(" +")[1]).each!writeln,
defaultGetoptPrinter("OPTIONS:", cmdOpts.options),
exit(0);
// Contents
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment