2014-12-04 65 views
1

假設我有一個腳本,您通過命令行選項--stable--unstable。當傳遞--unstable,用戶也應該能夠通過--harmony這使得只存在於不穩定版本的功能,所以像命令行選項取決於其他選項

./script --unstable --harmony 

但是,如果用戶確實是這樣的:

./script --stable --harmony 

我應該拋出一個錯誤並停止執行?或者我應該忽略--harmony標誌?

+4

這完全取決於你。 – Barmar 2014-12-04 08:42:18

回答

1

您可以打印一條「警告」消息,指出該選項無效,您將忽略它。只見幾十個腳本這樣做

1

您可以輕鬆地在bash做到這一點只有=~運營商,測試所有的參數列入雙方stableharmony的:

#!/bin/bash 

[[ "[email protected]" =~ "stable" ]] && [[ "[email protected]" =~ "harmony" ]] && { 
    printf "\nerror: incompatible arguments provided.\n" 
    printf "  arguments 'stable' and 'harmony' are mutually exclusive.\n\n" 
    exit 1 
} 

printf "\n Only compatible arguments passed.\n\n" 

exit 0 

用法:

$ bash stblharm.sh --stable --harmony 

error: incompatible arguments provided. 
     arguments 'stable' and 'harmony' are mutually exclusive. 

$ bash stblharm.sh --stable --something_else 

Only compatible arguments passed.