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.ListCommand; 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 ListCommand : Command 24 { 25 override protected void configure() 26 { 27 this 28 .setName("list") 29 .setDefinition(createDefinition()) 30 .setDescription("Lists commands") 31 .setHelp("The <info>%command.name%</info> command lists all commands:\n" ~ 32 "\n" ~ 33 " <info>%command.name%</info>\n" ~ 34 "\n" ~ 35 "You can also display the commands for a specific namespace:\n" ~ 36 "\n" ~ 37 " <info>%command.name% test</info>\n" ~ 38 "\n" ~ 39 "You can also output the information in other formats by using the <comment>--format</comment> option:\n" ~ 40 "\n" ~ 41 " <info>%command.name% --format=xml</info>\n" ~ 42 "\n" ~ 43 "It's also possible to get raw list of commands (useful for embedding command runner):\n" ~ 44 "\n" ~ 45 " <info>%command.name% --raw</info>\n") 46 ; 47 } 48 49 override public InputDefinition getNativeDefinition() 50 { 51 return createDefinition(); 52 } 53 54 override protected int execute(Input input, Output output) 55 { 56 if (input.getOption("xml") !is null) { 57 input.setOption("format", "xml"); 58 } 59 60 DescriptorHelper helper = new DescriptorHelper(); 61 helper.describe(output, getConsole(), (new DescriptorOptions()) 62 .set("format", input.getOption("format")) 63 .set("raw_text", input.getOption("raw")) 64 .set("namespace", input.getArgument("namespace")) 65 ); 66 67 return 0; 68 } 69 70 private InputDefinition createDefinition() 71 { 72 InputDefinition definition = new InputDefinition(); 73 definition.addArgument(new InputArgument("namespace", InputArgument.OPTIONAL, "The namespace name")); 74 definition.addOption(new InputOption("xml", null, InputOption.VALUE_NONE, "To output list as XML")); 75 definition.addOption(new InputOption("raw", null, InputOption.VALUE_NONE, "To output raw command list")); 76 definition.addOption(new InputOption("format", null, InputOption.VALUE_REQUIRED, "To output list in other formats", "txt")); 77 78 return definition; 79 } 80 }