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.helper.HelperSet; 13 14 import std.string; 15 16 import hunt.console.error.InvalidArgumentException; 17 import hunt.console.command.Command; 18 19 import hunt.collection.HashMap; 20 import hunt.collection.Iterator; 21 import hunt.collection.List; 22 import hunt.collection.Map; 23 import hunt.util.Common; 24 import hunt.console.helper.Helper; 25 26 class HelperSet : Iterable!(Helper) 27 { 28 private Map!(string, Helper) helpers; 29 30 private Command command; 31 32 public this() 33 { 34 helpers = new HashMap!(string, Helper)(); 35 } 36 37 public this(List!(Helper) helpers) 38 { 39 this.helpers = new HashMap!(string, Helper)(); 40 foreach (Helper helper ; helpers) { 41 set(helper); 42 } 43 } 44 45 public void set(Helper helper) 46 { 47 set(helper, null); 48 } 49 50 public void set(Helper helper, string aliasName) 51 { 52 helpers.put(helper.getName(), helper); 53 if (aliasName !is null) { 54 helpers.put(aliasName, helper); 55 } 56 57 helper.setHelperSet(this); 58 } 59 60 public bool has(string name) 61 { 62 return helpers.containsKey(name); 63 } 64 65 public Helper get(string name) 66 { 67 if (!has(name)) { 68 throw new InvalidArgumentException(format("The helper '%s' is not defined.", name)); 69 } 70 71 return helpers.get(name); 72 } 73 74 public Command getCommand() 75 { 76 return command; 77 } 78 79 public void setCommand(Command command) 80 { 81 this.command = command; 82 } 83 84 // public Iterator!(Helper) iterator() 85 // { 86 // return helpers.byValue; 87 // } 88 override int opApply(scope int delegate(ref Helper) dg) { 89 int result = 0; 90 foreach (Helper v; helpers.byValue) { 91 result = dg(v); 92 if (result != 0) 93 return result; 94 } 95 return result; 96 } 97 }