Module info.picocli
Package picocli

Annotation Type CommandLine.ParentCommand


  • @Retention(RUNTIME)
    @Target(FIELD)
    public static @interface CommandLine.ParentCommand

    Fields annotated with @ParentCommand will be initialized with the parent command of the current subcommand. If the current command does not have a parent command, this annotation has no effect.

    Parent commands often define options that apply to all the subcommands. This annotation offers a convenient way to inject a reference to the parent command into a subcommand, so the subcommand can access its parent options. For example:

     @Command(name = "top", subcommands = Sub.class)
     class Top implements Runnable {
    
         @Option(names = {"-d", "--directory"}, description = "this option applies to all subcommands")
         File baseDirectory;
    
         public void run() { System.out.println("Hello from top"); }
     }
    
     @Command(name = "sub")
     class Sub implements Runnable {
    
         @ParentCommand
         private Top parent;
    
         public void run() {
             System.out.println("Subcommand: parent command 'directory' is " + parent.baseDirectory);
         }
     }
     
    Since:
    2.2