if (opt.isSet("command") && args.isEmpty())
{
throw opt.usageError("option --command requires argument(s)");
}
CommandSession newSession = (login ? session : processor.createSession(
session.getKeyboard(), session.getConsole(), System.err));
if (opt.isSet("xtrace"))
{
newSession.put("echo", true);
}
if (login && interactive)
{
URI uri = baseURI.resolve("etc/gosh_profile");
if (!new File(uri).exists())
{
URL url = getClass().getResource("/ext/gosh_profile");
if (url == null) {
url = getClass().getResource("/gosh_profile");
}
uri = (url == null) ? null : url.toURI();
}
if (uri != null)
{
source(session, uri.toString());
}
}
// export variables starting with upper-case to newSession
for (String key : getVariables(session))
{
if (key.matches("[.]?[A-Z].*"))
{
newSession.put(key, session.get(key));
}
}
Object result = null;
if (args.isEmpty())
{
if (interactive)
{
result = console(newSession);
}
}
else
{
CharSequence program;
if (opt.isSet("command"))
{
StringBuilder buf = new StringBuilder();
for (String arg : args)
{
if (buf.length() > 0)
{
buf.append(' ');
}
buf.append(arg);
}
program = buf;
}
else
{
URI script = cwd(session).resolve(args.remove(0));
// set script arguments
newSession.put("0", script);
newSession.put("args", args);
for (int i = 0; i < args.size(); ++i)
{
newSession.put(String.valueOf(i + 1), args.get(i));
}
program = readScript(script);
}
result = newSession.execute(program);
}
if (login && interactive && !opt.isSet("noshutdown"))
{
System.out.println("gosh: stopping framework");