Warning: mysqli_query(): (HY000/1021): Disk full (/tmp/#sql-a2c-1.MAI); waiting for someone to free some space... (errno: 28 "No space left on device") in /var/www/html/wp-includes/wp-db.php on line 2024

Warning: mysqli_query(): (HY000/1021): Disk full (/tmp/#sql-a2c-1.MAI); waiting for someone to free some space... (errno: 28 "No space left on device") in /var/www/html/wp-includes/wp-db.php on line 2024
class="post-443770 post type-post status-publish format-standard hentry" id="post-443770">

How to print subcommand result automatically?

I have Java CLI application based on cliche library and I want to migrate it to picocli.

My application was based on cliche so I have a lot of methods with asg.cliche.Command annotation which return some result. cliche prints command methods’s result automatically so result was printed in command line. I replaced asg.cliche.Command annotations by picocli.CommandLine.Command and I see that picocli does not print command methods’s results.
I have following class:

import picocli.CommandLine;

@CommandLine.Command(subcommandsRepeatable = true)
public class Foo
{

    public static void main( String[] args )
    {
        new CommandLine( new Foo() ).execute( args );
    }

    @CommandLine.Command
    public String sayHello()
    {
        return "Hello";
    }

    @CommandLine.Command
    public String sayGoodbye()
    {
        return "GoodBye";
    }
}

when I call java -cp myJar.jar Foo sayHello sayGoodbye I do not see any output.
I see three solutions:
1. Modify each methods to print result instead of return it.

import picocli.CommandLine;

@CommandLine.Command( subcommandsRepeatable = true )
public class Foo2
{

    public static void main( String[] args )
    {
        new CommandLine( new Foo2() ).execute( args );
    }

    @CommandLine.Command
    public void sayHello()
    {
        System.out.println( "Hello" );
    }

    @CommandLine.Command
    public void sayGoodbye()
    {
        System.out.println( "GoodBye" );
    }
}

I am not happy with this solution. I prefer not modify my methods.

  1. Retrieve results after execution.
public static void main( String[] args )
    {
        final CommandLine commandLine = new CommandLine( new Foo() );
        commandLine.execute( args );
        CommandLine.ParseResult parseResult = commandLine.getParseResult();
        for( CommandLine.ParseResult pr : parseResult.subcommands() )
        {
            System.out.println( pr.commandSpec().commandLine()
                .getExecutionResult()
                .toString() );
        }
    }

I see a few problems with this solution. The main problem is formatting. Execution result can be null, array, collection. The second problem is that results are printed after execution of all subcommands. If second subcommand throws exception then I firstly see exception stack trace and after that I see result of first subcommand.

  1. Ask on stackoverflow if there is some better solution. I do not believe that there is no any configuration option in picocli which enable results printing.

Leave a Reply

Your email address will not be published. Required fields are marked *