1 /** 2 * Copyright: Copyright (c) 2010-2011 Jacob Carlborg. All rights reserved. 3 * Authors: Jacob Carlborg 4 * Version: Initial created: Nov 8, 2010 5 * License: $(LINK2 http://www.boost.org/LICENSE_1_0.txt, Boost Software License 1.0) 6 */ 7 module dvm.commands.Command; 8 9 import dvm.dvm._; 10 import Path = dvm.io.Path; 11 12 abstract class Command 13 { 14 string name; 15 string summary; 16 17 protected Args args; 18 protected Options options; 19 20 this () {} 21 22 this (string name, string summary = "") 23 { 24 this.name = name; 25 this.summary = summary; 26 options = Options.instance; 27 } 28 29 abstract void execute (); 30 31 void invoke (string[] args ...) 32 { 33 this.args.args = args; 34 execute_; 35 } 36 37 void invoke (Args args) 38 { 39 this.args = args; 40 execute_; 41 } 42 43 private void execute_ () 44 { 45 deleteTmpDirectory; 46 execute; 47 } 48 49 private void deleteTmpDirectory () 50 { 51 if (Path.exists(options.path.tmp)) 52 Path.remove(options.path.tmp, true); 53 } 54 } 55 56 private struct Args 57 { 58 string[] args; 59 60 string opIndex (size_t index) 61 { 62 if (index > args.length - 1 && empty) 63 throw new MissingArgumentException("Missing argument(s)", __FILE__, __LINE__); 64 65 return args[index]; 66 } 67 68 string first () 69 { 70 return opIndex(0); 71 } 72 73 string first (string arg) 74 { 75 if (empty) 76 args ~= arg; 77 78 else 79 args[0] = arg; 80 81 return arg; 82 } 83 84 string last () 85 { 86 return opIndex(args.length - 1); 87 } 88 89 bool empty () 90 { 91 return args.length == 0; 92 } 93 94 bool any () 95 { 96 return !empty; 97 } 98 }