/*
 * $Id: man.java,v 1.1 2004/12/23 12:15:53 rampil Exp $
 * Copyright (c) 2004 LOGICAL-PARADOX.ORG
 */
package org.logical_paradox.gdrn.shell.command;

import java.io.OutputStream;
import java.io.PrintWriter;

import org.logical_paradox.shell.Argument;
import org.logical_paradox.shell.Command;
import org.logical_paradox.shell.ResultCode;
import org.logical_paradox.shell.ShellEnv;
import org.logical_paradox.shell.exception.CommandExecutionException;
import org.logical_paradox.shell.exception.TerminationException;

/**
 * <pre>
 * ■man
 * 
 * 【動作仕様】
 * コマンドの使用方法を出力します。
 * 
 * 【書式】
 * man <コマンド名>
 * </pre>
 * 
 * @author satoshi akabane@logical-paradox.org
 * @version $Revision: 1.1 $
 */
public class man implements Command {
	public static final String USAGE = "man <command-name>";

	public String usage() {
		return USAGE;
	}
	public ResultCode execute(OutputStream arg0, ShellEnv arg1, Argument arg2) throws CommandExecutionException, TerminationException {
		if(arg2 == null || arg2.getArguments().length == 0) {
			// 引数が指定されていないのでエラー
			throw new CommandExecutionException();
		}
		
		// コマンドのクラスをロードする
		String classname = arg1.getProperty("COMMAND_PACKAGE_NAME") + "." + arg2.getArguments()[0];
		try {
			Command command = (Command)Class.forName(classname).newInstance();
			PrintWriter pw = new PrintWriter(arg0);
			pw.println("usage: " + command.usage());
			pw.flush();
			pw.close();
		} catch (Exception e) {
			e.printStackTrace();
			throw new CommandExecutionException();
		}

		return new ResultCode(ResultCode.STATUS_OK);
	}

}
