1   /*
2    * SPDX-FileCopyrightText: none
3    * SPDX-License-Identifier: CC0-1.0
4    */
5   
6   package gov.nist.secauto.metaschema.cli.processor.command;
7   
8   import gov.nist.secauto.metaschema.cli.processor.CLIProcessor.CallingContext;
9   import gov.nist.secauto.metaschema.cli.processor.InvalidArgumentException;
10  import gov.nist.secauto.metaschema.core.util.CollectionUtil;
11  
12  import org.apache.commons.cli.CommandLine;
13  import org.apache.commons.cli.Option;
14  
15  import java.util.Collection;
16  import java.util.List;
17  
18  import edu.umd.cs.findbugs.annotations.NonNull;
19  
20  public interface ICommand {
21    @NonNull
22    String getName();
23  
24    @NonNull
25    String getDescription();
26  
27    @NonNull
28    default List<ExtraArgument> getExtraArguments() {
29      return CollectionUtil.emptyList();
30    }
31  
32    default int requiredExtraArgumentsCount() {
33      return (int) getExtraArguments().stream()
34          .filter(ExtraArgument::isRequired)
35          .count();
36    }
37  
38    @NonNull
39    default Collection<? extends Option> gatherOptions() {
40      // by default there are no options to handle
41      return CollectionUtil.emptyList();
42    }
43  
44    @NonNull
45    Collection<ICommand> getSubCommands();
46  
47    boolean isSubCommandRequired();
48  
49    @SuppressWarnings("unused")
50    default ICommand getSubCommandByName(@NonNull String name) {
51      // no sub commands by default
52      return null;
53    }
54  
55    @SuppressWarnings("unused")
56    default void validateOptions(
57        @NonNull CallingContext callingContext,
58        @NonNull CommandLine cmdLine) throws InvalidArgumentException {
59      // by default there are no options to handle
60    }
61  
62    @NonNull
63    ICommandExecutor newExecutor(@NonNull CallingContext callingContext, @NonNull CommandLine cmdLine);
64  }