2014-12-04 14 views
0

t.ms文件包含:如何在斷言失敗時使非交互式Maple退出?

interface(prettyprint=0): kernelopts(assertlevel=1): 
ASSERT(1<1): 
ASSERT(2<2): 

當我運行:

maple -q t.ms; echo $? 

我得到:

Error, assertion failed 
Error, assertion failed 
0 

當我運行:

maple -e 2 -q t.ms; echo $? 

我得到:

Error, assertion failed 
0 

我想:

Error, assertion failed 
4 

也就是說,我想楓與在第一次失敗的斷言非零退出狀態退出。 (我不在乎退出代碼是否爲1或其他任何東西,只要它不爲零。我從documentation得到了4,與錯誤中斷有關)我如何得到這個結果?

+0

「1」的退出碼意味着楓樹未能初始化/啓動。你爲什麼要使用它?退出代碼將報告給操作系統,以確定*爲什麼*楓關閉。在你的例子中,楓樹正在運行並正確關閉。 [看起來你可以用'quit(n)','done(n)'或者'stop(n)'來返回你自己的,但是。](http://www.maplesoft.com/support/help/maple /view.aspx?path=maple) – admdrew 2014-12-04 16:03:51

+1

通過使用'-e 2'選項,您可以在第一個未被捕獲的錯誤(在這種情況下由第一個失敗的斷言引起)退出。 – acer 2014-12-04 16:17:16

+0

@acer:感謝'-e 2'。它確實導致提前退出,但是退出代碼爲0。我更新了我的問題以反映這一點。如何通過非零退出代碼獲得提前退出? – pts 2014-12-04 20:30:23

回答

2

的文檔不會使它很清楚,必須使用,

`quit`(n) 

與名稱引號。現在

interface(prettyprint=0): 

handler:=proc(e::uneval) 
    local failed; 
    printf("entered\n"); # remove this when satisfied 
    failed:=false; 
    try 
    if evalb(eval(e)) <> true then 
     error; 
    end if;; 
    catch: 
    failed:=true; 
    printf("Error, assertion failed\n"); 
    finally; 
    if failed then 
     `quit`(5); 
    end if; 
    end try; 
    true; 
end proc: 

ASSERT(handler(1<1)): 
ASSERT(handler(2<2)): 

,保存此文件作爲然後uh.mpl使用華普18.01爲Linux我看到,

$ maple18.01 -q -A 2 ~/uh.mpl ; echo $? 
entered 
Error, assertion failed 
5 

如果沒有-A 2運行,那麼它不運行的斷言檢查。

[編輯]下面是一個稍作修改,以處理額外的參數作爲打印的一部分。

handler:=proc(e::uneval) 
    local failed; 
    printf("entered\n"); # remove this when satisfied 
    failed:=false; 
    try 
    if evalb(eval(e)) <> true then 
     error; 
    end if;; 
    catch: 
    failed:=true; 
    printf("Error, assertion failed, %q\n", _rest); 
    finally; 
    if failed then 
    `quit`(5); 
    end if; 
    end try; 
    true; 
end proc: 
+0

太棒了,謝謝!你可以添加更多的參數到'handler'來顯示一個自定義的錯誤信息,類似於'ASSERT(1 <1,(2,3,[4,5],「foo」))'顯示的內容嗎? – pts 2014-12-04 22:21:38

+0

我做了一個修改,用於額外的參數。 – acer 2014-12-05 01:34:42