| 1 |
/******************************************************************************* |
|---|
| 2 |
Illustrates use of the Arguments class. |
|---|
| 3 |
*******************************************************************************/ |
|---|
| 4 |
|
|---|
| 5 |
import tetra.app.Arguments; |
|---|
| 6 |
import tango.io.Stdout; |
|---|
| 7 |
import tango.io.FileConduit; |
|---|
| 8 |
import tango.text.stream.LineIterator; |
|---|
| 9 |
|
|---|
| 10 |
void _usage() |
|---|
| 11 |
{ |
|---|
| 12 |
Stdout("Usage: [OPTIONS]... FILES...").newline; |
|---|
| 13 |
Stdout("This is a program that does something.").newline; |
|---|
| 14 |
Stdout.newline; |
|---|
| 15 |
Stdout("OPTIONS: ").newline; |
|---|
| 16 |
Stdout("Output this help message: -?, --help").newline; |
|---|
| 17 |
Stdout("Do cool things to your files: -c, -C, --cool").newline; |
|---|
| 18 |
Stdout("Use filename as response file: -r, -R, --response").newline; |
|---|
| 19 |
} |
|---|
| 20 |
|
|---|
| 21 |
void main(char[][] cmdlArgs) |
|---|
| 22 |
{ |
|---|
| 23 |
char[][] implicitArguments = ["files"]; |
|---|
| 24 |
|
|---|
| 25 |
char[][][] argumentAliases; |
|---|
| 26 |
argumentAliases ~= ["help", "?"]; |
|---|
| 27 |
argumentAliases ~= ["cool", "C", "c"]; |
|---|
| 28 |
argumentAliases ~= ["response", "R", "r"]; |
|---|
| 29 |
|
|---|
| 30 |
auto args = new Arguments(cmdlArgs, implicitArguments, argumentAliases); |
|---|
| 31 |
|
|---|
| 32 |
bool _fileExistsValidation(char[] arg) |
|---|
| 33 |
{ |
|---|
| 34 |
bool rtn; |
|---|
| 35 |
FilePath argFile = new FilePath(arg); |
|---|
| 36 |
rtn = argFile.exists; |
|---|
| 37 |
if (!rtn) |
|---|
| 38 |
Stdout.format("Specified path does not exist: {}", arg).newline; |
|---|
| 39 |
return rtn; |
|---|
| 40 |
} |
|---|
| 41 |
bool _singleFileValidation(char[][] args, inout char[] invalidArg) |
|---|
| 42 |
{ |
|---|
| 43 |
if (args.length > 1) |
|---|
| 44 |
{ |
|---|
| 45 |
Stdout("Cannot specify multiple paths for argument.").newline; |
|---|
| 46 |
invalidArg = args[1]; |
|---|
| 47 |
} |
|---|
| 48 |
else |
|---|
| 49 |
return true; |
|---|
| 50 |
return false; |
|---|
| 51 |
} |
|---|
| 52 |
args.addValidation("response", &_fileExistsValidation); |
|---|
| 53 |
args.addValidation("response", &_singleFileValidation); |
|---|
| 54 |
args.addValidation("files", true, true); |
|---|
| 55 |
|
|---|
| 56 |
bool argsValidated = true; |
|---|
| 57 |
try |
|---|
| 58 |
args.validate; |
|---|
| 59 |
catch (ArgumentException ex) |
|---|
| 60 |
{ |
|---|
| 61 |
if (ex.reason == ArgumentException.ExceptionReason.MISSING_ARGUMENT) |
|---|
| 62 |
Stdout.format("Missing Argument: {} ({})", ex.name, ex.msg).newline; |
|---|
| 63 |
else if (ex.reason == ArgumentException.ExceptionReason.MISSING_PARAMETER) |
|---|
| 64 |
Stdout.format("Missing Parameter to Argument: {} ({})", ex.name, ex.msg).newline; |
|---|
| 65 |
else if (ex.reason == ArgumentException.ExceptionReason.INVALID_PARAMETER) |
|---|
| 66 |
Stdout.format("Invalid Parameter: {} ({})", ex.name, ex.msg).newline; |
|---|
| 67 |
Stdout.newline; |
|---|
| 68 |
argsValidated = false; |
|---|
| 69 |
} |
|---|
| 70 |
|
|---|
| 71 |
if ((!argsValidated) || ("help" in args)) |
|---|
| 72 |
_usage(); |
|---|
| 73 |
else |
|---|
| 74 |
{// ready to run |
|---|
| 75 |
if ("response" in args) |
|---|
| 76 |
{ |
|---|
| 77 |
auto file = new FileConduit(args["response"]); |
|---|
| 78 |
auto lines = new LineIterator!(char)(file); |
|---|
| 79 |
char[][] arguments; |
|---|
| 80 |
foreach (line; lines) |
|---|
| 81 |
arguments ~= line; |
|---|
| 82 |
args.parse(arguments, implicitArguments, argumentAliases); |
|---|
| 83 |
} |
|---|
| 84 |
if ("cool" in args) |
|---|
| 85 |
{ |
|---|
| 86 |
Stdout ("Listing the files to be actioned in a cool way.").newline; |
|---|
| 87 |
foreach (char[] file; args.getArray("files")) |
|---|
| 88 |
Stdout.format("{}", file).newline; |
|---|
| 89 |
Stdout ("Cool and secret action performed.").newline; |
|---|
| 90 |
} |
|---|
| 91 |
if ("x" in args) |
|---|
| 92 |
Stdout.format("User set the X factor to '{}'", args["x"]).newline; |
|---|
| 93 |
} |
|---|
| 94 |
} |
|---|