1 /* 2 * hunt-console eases the creation of beautiful and testable command line interfaces. 3 * 4 * Copyright (C) 2018-2019, HuntLabs 5 * 6 * Website: https://www.huntlabs.net 7 * 8 * Licensed under the Apache-2.0 License. 9 * 10 */ 11 12 module hunt.console.input.InputArgument; 13 14 import hunt.console.error.InvalidArgumentException; 15 import hunt.console.error.LogicException; 16 import std.conv; 17 18 class InputArgument 19 { 20 public static int REQUIRED = 1; 21 public static int OPTIONAL = 2; 22 public static int IS_ARRAY = 4; 23 24 private string name; 25 private int mode; 26 private string description; 27 private string defaultValue; 28 29 public this(string name) 30 { 31 this(name, OPTIONAL, null, null); 32 } 33 34 public this(string name, int mode) 35 { 36 this(name, mode, null, null); 37 } 38 39 public this(string name, int mode, string description) 40 { 41 this(name, mode, description, null); 42 } 43 44 public this(string name, int mode, string description, string defaultValue) 45 { 46 if (mode > 7 || mode < 1) { 47 throw new InvalidArgumentException("Argument mode " ~ mode.to!string ~ " is not valid."); 48 } 49 50 this.name = name; 51 this.mode = mode; 52 this.description = description; 53 54 setDefaultValue(defaultValue); 55 } 56 57 public string getName() 58 { 59 return name; 60 } 61 62 public bool isRequired() 63 { 64 return (mode & REQUIRED) == REQUIRED; 65 } 66 67 public bool isArray() 68 { 69 return (mode & IS_ARRAY) == IS_ARRAY; 70 } 71 72 public void setDefaultValue(string defaultValue) 73 { 74 if (mode == REQUIRED && defaultValue !is null) { 75 throw new LogicException("Cannot set a default value except for InputArgument.OPTIONAL mode."); 76 } 77 78 if (isArray()) { 79 // todo implement 80 } 81 82 this.defaultValue = defaultValue; 83 } 84 85 public string getDefaultValue() 86 { 87 return defaultValue; 88 } 89 90 public string getDescription() 91 { 92 return description; 93 } 94 }