
package org.apache.zookeeper.cli;

import java.util.List;
import org.apache.commons.cli.*;
import org.apache.zookeeper.KeeperException;

/**
 * ls command for cli
 */
public class LsCommand extends CliCommand {

    private static Options options = new Options();
    private String args[];
    private CommandLine cl;

    {
        options.addOption("?", false, "help");
        options.addOption("w", false, "watch");
    }

    public LsCommand() {
        super("ls", "[-w] path");
    }

    private void printHelp() {
        HelpFormatter formatter = new HelpFormatter();
        formatter.printHelp("ls [options] path", options);
    }

    @Override
    public CliCommand parse(String[] cmdArgs) throws ParseException {
        Parser parser = new PosixParser();
        cl = parser.parse(options, cmdArgs);
        args = cl.getArgs();
        if (cl.hasOption("?")) {
            printHelp();
        }

        if (args.length < 2) {
            throw new ParseException(getUsageStr());
        }

        retainCompatibility(cmdArgs);
        
        return this;
    }

    private void retainCompatibility(String[] cmdArgs) throws ParseException {
        // get path [watch]
        if (args.length > 2) {
            // rewrite to option
            cmdArgs[2] = "-w";
            err.println("'ls path [watch]' has been deprecated. "
                    + "Please use 'ls [-w] path' instead.");
            Parser parser = new PosixParser();
            cl = parser.parse(options, cmdArgs);
            args = cl.getArgs();
        }
    }

    @Override
    public boolean exec() throws KeeperException, InterruptedException {
        String path = args[1];
        boolean watch = cl.hasOption("w");
        List<String> children = zk.getChildren(path, watch);
        out.println(children);
        return watch;
    }
}
