我的漫漫程序之旅

          專注于JavaWeb開發
          隨筆 - 39, 文章 - 310, 評論 - 411, 引用 - 0
          數據加載中……

          Groovy之旅系列九(Groovy常見命令)

          如果需要用Shell來執行Groovy腳本的話,可以用以下三個命令。

          1.groovysh: 啟動一個groovysh命令行shell,來執行groovy代碼交互。

          2.groovyConsole: 啟動一個可以執行groovy代碼的圖形界面,另外,groovyConsole還可以加載和執行Groovy腳本文件。

          3.groovy : 啟動groovy的腳本解釋程序。單行的groovy腳本可以作為命令行參數被指定。

           一:讓我們先來看看groovysh吧。
          在這個命令中,對于所有的腳本和代碼塊你都需要在shell中完成。相對來說,它還是比較簡便的。

          打一個shell,在命令行中輸入:groovysh

          你會看到如下提示:
          Groovy Shell (1.5.5, JVM: 10.0-b19)
          Type 
          'help' or '\h' for help.
          -----------------------------------------
          groovy:
          000>

          在命令行上輸入"Hello World",如:
          groovy:000> "Hello World"
          ===> Hello World

          這是最簡單的Hello World程序。

          我們繼續在命令行內輸入help或?,會有相應的命令提示:
          Available commands:
            help     (\h) Display 
          this help message
            
          ?        (\?) Alias to: help
            exit     (\x) Exit the shell
            quit     (\q) Alias to: exit
            
          import   (\i) Import a class into the namespace
            display  (\d) Display the current buffer
            clear    (\c) Clear the buffer
            show     (\S) Show variables, classes or imports
            inspect  (\n) Inspect a variable or the last result with the GUI object br
          r
            purge    (\p) Purge variables, classes, imports or preferences
            edit     (\e) Edit the current buffer
            load     (\l) Load a file or URL into the buffer
            .        (\.) Alias to: load
            save     (\s) Save the current buffer to a file
            record   (\r) Record the current session to a file
            history  (\H) Display, manage and recall edit
          -line history
            alias    (\a) Create an alias
            set      (\
          =) Set (or list) preferences

          For help on a specific command type:
              help command


          Display命令:
          Display顯示你上次運行的非命令代碼。
          groovy:000> display
          -->Hello World

          Binding命令:
          Binding顯示在一個groovysh會話里可以利用的變量 .在一些簡單的例子中我們不常用變量,但是,為了演示,
          在下面的例子中,我們改進一下:"Hello World"用變量
          greeting去控制消息輸出部分:
          groovy> greeting = "Hello"
          groovy
          > "${greeting}, World!"
          groovy
          > go
          ===> Hello, World!
          groovy
          > binding
          Available variables in the current binding
          greeting 
          = Hello

          Inscept命令:
          Inscept命令會打開一個Groovy Object Browser并定位 在最近的有值表達式上.
          這個一個用Swing做的圖形用戶界面,顯示了有效的方法列表和已經注冊的元方法。


          你還可以控制輸出的格式:
          groovy> 'test'.center 20'-'
          groovy
          > go
          ===> --------test--------

          二 :GroovyConsole命令

          提供了一個執行Groovy 腳本的圖形化界面。

          三:Groovy命令
          假設有一個groovy文件
          current = 1
          next    
          = 1
          10.times {   
             print current 
          + ' '
             newCurrent 
          = next
             next 
          = next + current
             current 
          = newCurrent
          }

          println 
          ''
          loop  
          10 times
          我們保存為 test.groovy
          執行
          groovy test.groovy即可執行這個文件。

          當然也可以先編譯:
          groovyc test.groovy
          這時會生成test.class
          再執行:
          groovy test
          即可正確執行.


          posted on 2008-05-26 14:53 々上善若水々 閱讀(1822) 評論(0)  編輯  收藏


          只有注冊用戶登錄后才能發表評論。


          網站導航:
           
          主站蜘蛛池模板: 庆安县| 象山县| 通渭县| 玉门市| 邵东县| 卢龙县| 敦化市| 通辽市| 扎赉特旗| 平阳县| 太仆寺旗| 合水县| 安阳市| 松桃| 汶川县| 乐平市| 墨玉县| 宜州市| 广灵县| 灌阳县| 河西区| 确山县| 合阳县| 藁城市| 桦南县| 梓潼县| 陕西省| 邢台市| 镶黄旗| 勃利县| 嵊州市| 陆良县| 平乐县| 佛山市| 从化市| 孝昌县| 曲沃县| 凌海市| 陕西省| 黎川县| 新绛县|