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.DescriptorHelper; 13 14 import std.string; 15 16 import hunt.console.descriptor.MarkdownDescriptor; 17 import hunt.console.error.InvalidArgumentException; 18 import hunt.console.descriptor.Descriptor; 19 import hunt.console.descriptor.DescriptorOptions; 20 import hunt.console.descriptor.TextDescriptor; 21 import hunt.console.output.Output; 22 23 import hunt.collection.HashMap; 24 import hunt.collection.Map; 25 import hunt.console.helper.AbstractHelper; 26 import hunt.Boolean; 27 28 class DescriptorHelper : AbstractHelper 29 { 30 private Map!(string, Descriptor) descriptors; 31 32 public this() 33 { 34 descriptors = new HashMap!(string, Descriptor)(); 35 register("txt", new TextDescriptor()); 36 register("md", new MarkdownDescriptor()); 37 } 38 39 public void describe(Output output, Object object, DescriptorOptions options) 40 { 41 options.set("raw_text", Boolean.FALSE.toString(), false); 42 options.set("format", "txt", false); 43 44 if (!descriptors.containsKey(options.get("format"))) { 45 throw new InvalidArgumentException(format("Unsupported format '%s'.", options.get("format"))); 46 } 47 48 Descriptor descriptor = descriptors.get(options.get("format")); 49 descriptor.describe(output, object, options); 50 } 51 52 private DescriptorHelper register(string format, Descriptor descriptor) 53 { 54 descriptors.put(format, descriptor); 55 56 return this; 57 } 58 59 /* override */ public string getName() 60 { 61 return "descriptor"; 62 } 63 }