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.command.HelpCommand; 13 14 import hunt.console.descriptor.DescriptorOptions; 15 import hunt.console.helper.DescriptorHelper; 16 import hunt.console.input.Input; 17 import hunt.console.input.InputArgument; 18 import hunt.console.input.InputDefinition; 19 import hunt.console.input.InputOption; 20 import hunt.console.output.Output; 21 import hunt.console.command.Command; 22 23 class HelpCommand : Command 24 { 25 private Command command; 26 27 override protected void configure() 28 { 29 ignoreValidationErrors(); 30 31 InputDefinition definition = new InputDefinition(); 32 definition.addArgument(new InputArgument("command_name", InputArgument.OPTIONAL, "The command name", "help")); 33 definition.addOption(new InputOption("xml", null, InputOption.VALUE_NONE, "To output help as XML")); 34 definition.addOption(new InputOption("format", null, InputOption.VALUE_REQUIRED, "To output help in other formats", "txt")); 35 definition.addOption(new InputOption("raw", null, InputOption.VALUE_NONE, "To output raw command help")); 36 37 this 38 .setName("help") 39 .setDescription("Displays help for a command") 40 .setDefinition(definition) 41 .setHelp("The <info>%command.name%</info> command displays help for a given command:\n" ~ 42 "\n" ~ 43 " <info>%command.name% list</info>\n" ~ 44 "\n" ~ 45 "You can also output the help in other formats by using the <comment>--format</comment> option:\n" ~ 46 "\n" ~ 47 " <info>%command.name% --format=xml list</info>\n" ~ 48 "\n" ~ 49 "To display the list of available commands, please use the <info>list</info> command.\n") 50 ; 51 } 52 53 public void setCommand(Command command) 54 { 55 this.command = command; 56 } 57 58 override protected int execute(Input input, Output output) 59 { 60 if (command is null) { 61 command = getConsole().find(input.getArgument("command_name")); 62 } 63 64 if (input.getOption("xml") !is null) { 65 input.setOption("format", "xml"); 66 } 67 68 DescriptorHelper helper = new DescriptorHelper(); 69 helper.describe(output, command, (new DescriptorOptions()) 70 .set("format", input.getOption("format")) 71 .set("raw", input.getOption("raw")) 72 ); 73 74 return 0; 75 } 76 }