我的漫漫程序之旅

          專注于JavaWeb開發(fā)
          隨筆 - 39, 文章 - 310, 評論 - 411, 引用 - 0
          數(shù)據(jù)加載中……

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

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

          1.groovysh: 啟動一個groovysh命令行shell,來執(zhí)行g(shù)roovy代碼交互。

          2.groovyConsole: 啟動一個可以執(zhí)行g(shù)roovy代碼的圖形界面,另外,groovyConsole還可以加載和執(zhí)行Groovy腳本文件。

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

           一:讓我們先來看看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程序。

          我們繼續(xù)在命令行內(nèi)輸入help或?,會有相應(yīng)的命令提示:
          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做的圖形用戶界面,顯示了有效的方法列表和已經(jīng)注冊的元方法。


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

          二 :GroovyConsole命令

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

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

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

          當(dāng)然也可以先編譯:
          groovyc test.groovy
          這時會生成test.class
          再執(zhí)行:
          groovy test
          即可正確執(zhí)行.


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


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


          網(wǎng)站導(dǎo)航:
           
          主站蜘蛛池模板: 舒城县| 霍州市| 九江市| 靖宇县| 上高县| 固原市| 北安市| 湖南省| 体育| 乌拉特前旗| 彰化市| 瑞金市| 新巴尔虎左旗| 乾安县| 宁晋县| 台湾省| 陇西县| 白朗县| 麻城市| 勃利县| 曲沃县| 会泽县| 新宁县| 陵川县| 绵阳市| 平定县| 凭祥市| 页游| 湖口县| 浦县| 黔西| 泰安市| 紫金县| 新平| 响水县| 项城市| 栾川县| 余江县| 蓬莱市| 南靖县| 南汇区|