Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

...

Commands can have any signature - arguments are coerced to call the best matching method using reflection. A CommandSession argument is inserted if required:

Code Block
templatejava
public void sleep(long millis) throws InterruptedException{
        Thread.sleep(millis);
}

public void sleep(String[] args) throws Exception;

public boolean grep(CommandSession session, String[] args) throws Exception;

The CommandSession interface provides methods for executing commands and getting and setting session variables:

Code Block
templatejava
public interface org.osgi.service.command.CommandSession {
        Object execute(CharSequence commandline) throws Exception;
        Object get(String name);

    void put(String name, Object value);
        ...
}

Easy to use interactively - no unnecessary syntax.

// simple command

No Format
$g! echo hello world
hello world

// session variables

No Format
$g! msg = "hello world"
$g! echo $msg
hello world

// execution quotes () - similar to bash backquotes

No Format
$g! (bundle 1) location
file:/Users/derek/Downloads/felix-framework-23.0.10/bundle/org.apache.felix.bundlerepository-1.46.2.jar

Provides lists, pipes and closures.

Wiki Markup
// lists - \[\]

No Format
$g! list = [1 2 a b]
1
2
a
b

$g! map = [Jan=1 Feb=2 Mar=3]
Jan                 1
Feb                 2
Mar                 3

// pipes

No Format
$g! bundles | grep gogo
000002 ACT     2|Active     |    1|org.apache.felix.gogo.console-command (0.26.2
000003 ACT 0)
    3|Active     |    1|org.apache.felix.gogo.runtime-0.2.2 (0.6.0)
    4|Active     |    1|org.apache.felix.gogo.shell (0.6.0)

// closures - {}

No Format
$g! echo2 = { echo xxx $args yyy }
$g! echo2 hello world
xxx hello world yyy

...

// exception handling - console shows summary, but full context available

No Format
$g! start xxx
E: Cannot coerce start[xxx] to any of [(Bundle)]
$g! $exception printstacktrace
java.lang.IllegalArgumentException: Cannot coerce start[xxx] to any of [(Bundle)]
        at org.apache.felix.gogo.runtime.shell.Reflective.method(Reflective.java:162)
        at org.apache.felix.gogo.runtime.shell.Command.execute(Command.java:40)
        at org.apache.felix.gogo.runtime.shell.Closure.execute(Closure.java:211)
        at org.apache.felix.gogo.runtime.shell.Closure.executeStatement(Closure.java:146)
        at org.apache.felix.gogo.runtime.shell.Pipe.run(Pipe.java:91)
...

// add all public methods on java.lang.System as commands:

No Format
$g! addcommand system (loadClass java.lang.System)
$g! system:getproperties
sun.io.unicode.encodingUnicodeLittle
java.version        1.5.0_19
java.class.path     bin/felix.jar
java.awt.graphicsenvapple.awt.CGraphicsEnvironment
user.language       en
sun.os.patch.level  unknown
os.version          10.6.2
[snip]

...

Code Block
templatejava
public void each(CommandSession session, Collection<Object> list, Function, closure) throws Exception {
    for (Object x : list) {
        closure.execute(session, null);
    }
}
No Format
$g! each [Jan Feb Mar] { echo $it | grep . }
Jan
Feb
Mar

...