Processing Command Line

edited October 2015 in How To...

I would like to make a command line like program that would ask for user input, the user would enter in the method that they wish to call and any commands that are passed to it. Then the computer will do what it has to do and give back any return value. For example lets say I have this code: void printaNumber() { println(random(0,1); }

and then the user would enter in this into the program: "printaNumber()" the computer would then run the code above. I want to take this so that I can have voice recognition be my input. But I need to get this problem solved first. Thanks!

Answers

  • Answer ✓

    There are two problems here.

    First, Processing isn't made to do command line applications. It can now be run from the command line, but then it will show a window...

    Second, the problem of running a user-provided code is relatively complex.

    A solution is given in http://forum.processing.org/two/discussion/82/dynamically-loading-java-source-files

    You can also run a scripting language, like JavaScript, inside a Java program. Some examples has been shown in one of the old forums.

  • you could write a batch file for your command line, take in user input, and write that unto a text file that processing can use to run a command. You can then use processing to turn that text file into an array and separate each argument by if there is a space... your commands would look like this:

    printANumber random(1)

    you could then use the arguments in the array and run them through a switch statement to test for known commands.

    As far as turning user input into code, I have always been fascinated by this. I have asked several experienced developers before, and many of them thought that i was crazy. They did however say that it is hard to do this in java because it needs to be compiled, while other scripting and markup languages have this capability because they do not need to be compiled. With processing, you are very limited in this field (as processing is pretty much java). There are other cool things that you can do dynamically with code, most likely not in processing, but in java. I would look into the "object" data type and taking that in as a parameter for a function. This could possibly be translated into a function name and used (I do know that it works for class names)

    If your really wanted to, you would not need to even do this in processing. You can write a batch file and take in command line arguments(%1,%2,%3) and throw it unto the system32 folder. when you type the name of the batch file with the correct argument, it will run. Hope this helped.

    By the way, there is now a pretty good processing library out there for voice command called stt. It uses the google speech api and would work well for what you were talking about.

  • Actually, Processing programs have a field called args which stores the executable path
    plus any provided command line arguments!

    About compiled languages being unfit for command line arguments is 100% rubbish!
    Most UNIX utilities are made in a compiled language called C. And it's much older than script languages!

  • "About compiled languages being unfit for command line arguments is 100% rubbish!"
    Not sure where you have read that.

  • edited July 2014

    Sorry, I was thinking that you were telling that compiled languages didn't accept command line arguments!
    If that was the case, I'd stand for 100% rubbish! :bz

    Seems like you were actually talking about some kinda create a new user-typed program inside a compiled program on-the-fly. Indeed, that'd demand an interpreted/script language module! 8-X

    Lua & Python would be the best candidates for it! Just need to find such a module for Java! :-j

  • I wrote: "Processing isn't made to do command line applications"
    It just means you cannot use Processing to make programs like Unix' ls or awk or grep or even wc. Ie. programs to use on the command line, taking something (often text) as input (in general), and outputting something else on the command line (often text too, in the Unix philosophy) or to a file.
    Processing will always (unless using some tricks, perhaps) display a window. It is not suited for usage on the command line, on a server (as often asked), and the like.

    "Lua & Python would be the best candidates for it!"
    It depends on the objective.
    JavaScript has an advantage: it is available out of the box in Java. And lot of of people know (more or less) how to code it.
    BeanShell can be an option too, lightweight and leveraging the knowledge of the Java syntax.
    There are several other interpreters for Java, lot of them being JSR-223 compliant (specification of Java scripting languages).
    Among them, Jython, the Python interpreter, and several Lua interpreters for Java, like LuaJ, JNLua, etc.

  • edited July 2014

    It just means you cannot use Processing to make programs like Unix'...

    Ie. programs to use on the command line, taking something (often text) as input (in general),

    Processing's PApplet has a String[] field called args.
    It has the app's path at index 0 and any passed command line arguments as the other indices!
    Therefore, Processing accepts command line arguments as any C-like program does! :P

    ... and outputting something else on the command line (often text too, in the Unix philosophy) or to a file.

    Have you ever heard about println()? As long Processing got that, and many other Java's output niceties, outputting to console or to a file is 100% possible!

    Processing will always (unless using some tricks, perhaps) display a window.

    Although "using tricks" expression sounds as some kinda complicated hack, it's as simple as invoking exit() at the end of setup()! B/c the window is only setVisible(true) just before draw() starts!

    Of course Java, and any language that relies on a heavy VM, isn't particularly suitable for command line utils.
    Due to both slower startup & relatively high memory footprint. But in no way that's the same as "it can't"!!! [-X

  • edited July 2014

    JavaScript has an advantage: it is available out of the box in Java.

    Cool! Didn't know about it! Made a remix from a code I've found out about it: \m/

    /**
     * JS ScriptEngine (v2.0)
     * by  Joe (2012/Oct)
     * mod GoToLoop (2014/Jul)
     *
     * JavaPapers.com/core-java/run-javascript-from-java/
     *
     * forum.processing.org/two/discussion/316/processing-command-line
     */
    
    import javax.script.ScriptEngine;
    import javax.script.ScriptEngineManager;
    import javax.script.ScriptException;
    
    void setup() {
      size(300, 200, JAVA2D);
    
      ScriptEngine engine = new ScriptEngineManager()
        .getEngineByName("javascript");
    
      engine.put("dataPath", dataPath("") + "/");
    
      try {
        engine.eval("var x = 10, y = 20;");
        engine.eval("var z = x + y; println('Total: ' + z);");
    
        engine.eval("println(dataPath + '\\n')");
    
        //engine.eval("alert(\"JS\");");
      }
    
      catch (ScriptException e) {
        println();
        println(e);
      }
    
      println("From JS: " + engine.get("y"));
    
      exit();
    }
    
  • OK, have you tried your tricks in a headless server? I am not saying they won't work, as I have not tried myself, so I am mostly curious to see if they work, as several people in the past asked how to make Processing to work in such environment. Those on Unix system usually use some virtual display.

    And, to go back to my original message, "Processing isn't made to do command line applications". Perhaps it can, but frankly, running a whole JVM, loading core.jar, etc. to filter out a stream is a bit overkill. Compare the size of wc binary with the one of the Java binaries... :-)

    About the JS script engine: https://www.google.com/search?as_sitesearch=processing.org&as_q=ScriptEngineManager

  • Just move to eclipse or IntelliJ. There you can use the console as the input field. Let's hope we can use the console for the processing 3.0 IDE as an input.

  • edited July 2014

    OK, have you tried your tricks in a headless server?

    Nope! I was talking about regular terminal emulators. Perhaps this can help:

    /** 
     * Hide Canvas Frame II (v1.0)
     * by GoToLoop (2013/Dec)
     * 
     * forum.processing.org/two/discussion/1967/
     * is-it-possible-to-write-a-text-only-application
     */
    
    void setup() {
      size(800, 600);
      frameRate(60);
      smooth();
    }
    
    void draw() {
      println(frameCount);
    }
    
    @ Override boolean displayable() {
      return false;
    }
    

    Or this more complex example:

    //forum.processing.org/two/discussion/316/processing-command-line
    
    public static final void main(final String[] args) {
      final String sketch = Thread.currentThread()
        .getStackTrace()[1].getClassName();
    
      final PApplet p;
    
      try {
        p = (PApplet) Class.forName(sketch).newInstance();
      }
    
      catch (Exception e) {
        throw new RuntimeException(e);
      }
    
      p.setup();
    }
    
    void setup() {
      println(this);
      println(Thread.currentThread());
      exit();
    }
    
Sign In or Register to comment.