Skip to content

Commit

Permalink
💡 Add Documentation
Browse files Browse the repository at this point in the history
  • Loading branch information
JesseKPhillips committed Oct 27, 2019
1 parent ce69059 commit 3b25f57
Show file tree
Hide file tree
Showing 2 changed files with 104 additions and 0 deletions.
34 changes: 34 additions & 0 deletions source/structopt/attributes.d
Original file line number Diff line number Diff line change
@@ -1,21 +1,55 @@
/**
* Compile Time attributes for specifying proprites to std.getopt.
*
* These structs are utilized by the the generator to populate
* getopt's properties.
*/
module structopt.attributes;

import std.traits;

/**
* Command Line Help Message for the arugement.
*/
struct Help {
/**
* Help Message.
*/
string msg;
}

/**
* Command Line Argument Option which causes assignement to this field.
*/
struct Option {
/**
* All argument options.
*/
string[] names;

/**
* Construct all options into an array.
*
* ```
* @Option("help", "h")
*/
this(string[] names...) {
this.names = names;
}

/**
* Used by generator to translate array into getopt argument.
*/
string cononical() {
import std.algorithm;
import std.conv;
return names.joiner("|").to!string;
}
///
unittest {
auto opt = Option("help", "h");

assert(opt.names == ["help", "h"]);
assert(opt.cononical() == "help|h");
}
}
70 changes: 70 additions & 0 deletions source/structopt/package.d
Original file line number Diff line number Diff line change
@@ -1,12 +1,53 @@
/**
* This generative method creates a getopt call based on the
* attributes assign to a structure.
*
* ```
import structopt;
// Specify The Parameter Structure
struct Options
{
@Option("threads", "t")
@Help("Number of threads to use.")
size_t threads;
@Option("file")
@Help("Input files")
string[] files;
}
void main(string[] args) {
Options props;
// Pass in the struct to generate UDA for
auto helpInfo = mixin(GenerateGetopt!(props, args));
defaultGetoptPrinter("Options: ",
helpInfo.options);
// Output to console:
//Options:
//-t --threads Number of threads to use.
// --file Input files
//-h --help This help information./
}
* ```
*/
module structopt;

public import structopt.attributes;
public import std.getopt;
public import std.traits;

/// Rename the Option struct of getopt
alias GetOption = std.getopt.Option;
/// Reastablish the name for the struct attributes
alias Option = structopt.attributes.Option;

/**
* Generate the std.getopt method call.
*/
string GenerateGetopt(alias Options, alias args)() pure nothrow {
import std.meta;
import std.typecons;
Expand All @@ -23,3 +64,32 @@ string GenerateGetopt(alias Options, alias args)() pure nothrow {

return ans ~ ")";
}
///
unittest {
// Specify The Parameter Structure
struct Options
{
@Option("threads", "t")
@Help("Number of threads to use.")
size_t threads;

@Option("file")
@Help("Input files")
string[] files;
}

Options props;
string[] arg = ["./structopt", "t", "24"];

// Pass in the struct to generate UDA for
auto helpInfo = mixin(GenerateGetopt!(props, arg));
auto expected = [
GetOption("-t", "--threads", "Number of threads to use.", false),
GetOption("", "--file", "Input files", false),
GetOption("-h", "--help", "This help information.", false)
];

import std.algorithm;
assert(helpInfo.options.equal(expected));

}

0 comments on commit 3b25f57

Please sign in to comment.