1 package org.ibex.xt.shell;
6 import java.io.IOException;
8 public abstract class Command {
9 /** Returns the command name. */
10 public abstract String name();
12 /** Returns single-line of usage information, eg. <tt>pattern]</tt> */
13 public abstract String usage();
15 /** Returns single-line description of command. */
16 public abstract String summary();
18 /** Returns multi-line description. */
19 public abstract String help();
21 /** Writes result of execution, even if result is an error. */
22 public abstract void execute(Writer w, String[] args, Env env) throws IOException;
24 public static class Help extends Command {
25 public String name() { return "help"; }
26 public String usage() { return "[command name]"; }
27 public String summary() { return "Lists available commands."; }
28 public String help() { return ""; }
30 public void execute(Writer w, String[] c, Env env) throws IOException {
32 Command cmd = env.command(c[1]);
36 w.write(": command not found\n");
48 for (int i=0; i < env.commands.length; i++)
49 len = Math.max(env.commands[i].name().length(), len);
51 w.write("Available commands:\n");
52 for (int i=0; i < env.commands.length; i++) {
53 Command cmd = env.commands[i];
56 for (int j=len - cmd.name().length(); j >= 0; j--) w.write(" ");
58 w.write(cmd.summary());
61 w.write("\nFor usage details, type help [command name].\n");
66 public static class Ls extends Command {
67 public String name() { return "ls"; }
68 public String usage() { return "[path]"; }
69 public String summary() { return "List object entries."; }
70 public String help() { return
71 "Lists the keys in an object. Modelled after the UNIX ls command.";
74 public void execute(Writer w, String[] c, Env env) throws IOException {
75 if (c.length > 2) { w.write(usage()); return; }
76 String p = env.path(c.length == 1 ? "*" : c[1]);
78 Request.Response ret = env.send(new Request.Key(p));
79 if (!(ret instanceof Request.Key.Res)) {
81 w.write(ret.error().getMessage());
84 List l = ((Request.Key.Res)ret).keys();
85 Iterator i = l.iterator(); while (i.hasNext()) {
86 w.write(i.next().toString());
93 public static class Pwd extends Command {
94 public String name() { return "pwd"; }
95 public String usage() { return ""; }
96 public String summary() { return "Path to current object."; }
97 public String help() { return "Print the path to the current object."; }
98 public void execute(Writer w, String[] c, Env env) throws IOException {
99 if (c.length != 1) { w.write(usage()); return; }
100 w.write(env.path.equals("") ? "/" : env.path.replace('.', '/'));
105 public static class Cd extends Command {
106 public String name() { return "cd"; }
107 public String usage() { return "[path]"; }
108 public String summary() { return "Change current object."; }
109 public String help() { return
110 "Chnages the current object that all other commands use "+
111 "as the base for running.\n Pass either a relative path "+
112 "(e.g. in /prevalent, type cd myob, now in /prevalent/myob) "+
113 "or an absolute path (e.g. cd /prevalent/myob).\n\n" +
114 "To go up one level, cd .. can be used.";
116 public void execute(Writer w, String[] c, Env env) throws IOException {
117 if (c.length > 2) w.write(usage());
118 else if (c.length == 1 || c[1].equals("") || c[1].equals("/")) env.path = "";
119 else if (c[1].equals("..")) {
121 n = n.substring(0, n.lastIndexOf('.'));
124 String n = env.path(c[1]);
126 Request.Response ret = env.send(new Request.Key(n));
127 if (!(ret instanceof Request.Key.Res)) {
129 w.write(ret.error().getMessage());
132 List l = ((Request.Key.Res)ret).keys();
136 w.write(": no such path\n");
145 public static class Rm extends Command {
146 public String name() { return "rm"; }
147 public String usage() { return "[options] [path]"; }
148 public String summary() { return "Removes objects."; }
149 public String help() { return "Removes objects."; } // FIXME
150 public void execute(Writer w, String[] c, Env env) throws IOException {
151 if (c.length == 1) { w.write(usage()); }
153 String[] r = new String[c.length - 1];
154 for (int i=0; i < r.length; i++) r[i] = env.path(c[i + 1]);
155 // Object ret = send(new KeyRequest( FIXME: CompositeRequest