132
|
1 /*******************************************************************************
|
|
2 Illustrates use of the Arguments class.
|
|
3 *******************************************************************************/
|
|
4
|
|
5 import tango.util.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
|
|
42 bool singleFileValidation(char[][] args, inout char[] invalidArg)
|
|
43 {
|
|
44 if (args.length > 1)
|
|
45 {
|
|
46 Stdout("Cannot specify multiple paths for argument.").newline;
|
|
47 invalidArg = args[1];
|
|
48 }
|
|
49 else
|
|
50 return true;
|
|
51 return false;
|
|
52 }
|
|
53
|
|
54 args.addValidation("response", &fileExistsValidation);
|
|
55 args.addValidation("response", &singleFileValidation);
|
|
56 args.addValidation("files", true, true);
|
|
57
|
|
58 bool argsValidated = true;
|
|
59 try
|
|
60 args.validate;
|
|
61 catch (ArgumentException ex)
|
|
62 {
|
|
63 if (ex.reason == ArgumentException.ExceptionReason.MISSING_ARGUMENT)
|
|
64 Stdout.format("Missing Argument: {} ({})", ex.name, ex.msg).newline;
|
|
65 else if (ex.reason == ArgumentException.ExceptionReason.MISSING_PARAMETER)
|
|
66 Stdout.format("Missing Parameter to Argument: {} ({})", ex.name, ex.msg).newline;
|
|
67 else if (ex.reason == ArgumentException.ExceptionReason.INVALID_PARAMETER)
|
|
68 Stdout.format("Invalid Parameter: {} ({})", ex.name, ex.msg).newline;
|
|
69 Stdout.newline;
|
|
70 argsValidated = false;
|
|
71 }
|
|
72
|
|
73 if ((!argsValidated) || ("help" in args))
|
|
74 usage();
|
|
75 else
|
|
76 {// ready to run
|
|
77 if ("response" in args)
|
|
78 {
|
|
79 Stdout(args["response"][0]).newline;
|
|
80 auto file = new FileConduit(args["response"][0]);
|
|
81 auto lines = new LineIterator!(char)(file);
|
|
82 char[][] arguments;
|
|
83 foreach (line; lines)
|
|
84 arguments ~= line.dup;
|
|
85 args.parse(arguments, implicitArguments, argumentAliases);
|
|
86 }
|
|
87 if ("cool" in args)
|
|
88 {
|
|
89 Stdout ("Listing the files to be actioned in a cool way.").newline;
|
|
90 foreach (char[] file; args["files"])
|
|
91 Stdout.format("{}", file).newline;
|
|
92 Stdout ("Cool and secret action performed.").newline;
|
|
93 }
|
|
94 if ("x" in args)
|
|
95 Stdout.format("User set the X factor to '{}'", args["x"]).newline;
|
|
96 }
|
|
97 }
|