2017-03-28 28 views
1

在以下示例中,我預期錯誤消息來自xyz,而不是來自getopt。我究竟做錯了什麼?如何使用--name參數來getopt?

/tmp> getopt --name xyz --options "xyz:" -- -x 
-x -- 
/tmp> getopt --name xyz --options "xyz:" -- -x -z 
getopt: option requires an argument -- z 
-x -- 

我該怎麼說呢xyz: option requires an argument -- z;是不是--name是爲什麼?

UPDATE

似乎是一個錯誤。我的getopt來自cygwin

$ getopt --version 
getopt from util-linux 2.25.2 

回答

1

這似乎是在某些版本的程序中的錯誤。

它爲我在Centos的7.3和Fedora 19

[vps1 ~]$ cat /etc/redhat-release 
CentOS Linux release 7.3.1611 (Core) 
[vps1 ~]$ getopt --name xyz --options "xyz:" -- -x -z 
xyz: option requires an argument -- 'z' 
-x -- 
[vps1 ~]$ getopt -V 
getopt from util-linux 2.23.2 

但它在我的MinGW殼(從Git的適用於Windows)

$ getopt --name xyz --options "xyz:" -- -x -z 
getopt: option requires an argument -- z 
-x -- 
$ getopt -V 
getopt from util-linux 2.26.2 

更新沒有的功能:它的工作原理也在Linux的2.27.1中。並且它在(至少某些版本的)Cygwin中不起作用。所以這個問題似乎出現在Windows端口(有趣的是Mingw和Cygwin)。

我會扔胡亂猜測(擊中目標的並不大概率): 的getopt程序,因爲this commit嘗試處理一些environnments(特別是BSD; Linux)的有/使用getprogname/setprogname獲取/設置「當前」程序名稱(而不是依賴於argv[0])。

#if defined (HAVE_SETPROGNAME) && !defined (__linux__) 
     setprogname(name); 

現在,讓我們想象一下,

  1. Cygwin和MinGW/MSI的都支持這些功能。
  2. 然而,他們缺乏HAVE_SETPROGNAME定義
  3. 此外,他們getopt功能(請注意,不是程序),就像BSD版本,使用getprogname代替argv[0]

在這種情況下,問題會解釋。但是,我很懷疑 - 特別是第3點。

+0

我從'cygwin的'util-linux 2.25.2' getopt。無論是最近的錯誤或Windows相關? –

+0

相反,後來,我會說(更新)... – leonbloy

+1

也許加入標籤cygwin和mingw來吸引一些開發者....? – leonbloy

0

這是一個已在util-linux 2.28commit 30fbf2f6中修復的錯誤(或者僅僅是不可移植性問題)。在此修復之前,它僅適用於Linux,OSX和一些BSD風格,但不適用於WIN32或GNU-Hurd。

如果您不能升級util-linux(可能是很難建立在Windows上),那麼你可以使用這個shell解決方法:仍然使用--name選項會覆蓋這一招又一次如果

bash -c 'exec -a "XYZ" getopt --options "xyz:" -- -x -z' 

注getopt將在一天內更新。

當然,您也可以簡單地將getopt程序複製/鏈接/重命名爲您想要的任何名稱。