Fortran命令行参数 [英] Fortran command-line arguments
问题描述
在g ++中,我使用 getopt_long
来处理命令行选项。
是否与 Gfortran 一样?
In g++ I was using getopt_long
to handle command-line options. Does there
exist the same thing for Gfortran?
我希望能够将参数传递给某些Fortran单元测试。
I want to be able to pass aguments to some Fortran unit tests.
当前,我有以下内容。可以注意到,我正在小心
来获得钥匙并珍惜自己。使用C ++时,getopt_long
正是为我做的。
Currently I have the following. As one can notice I am taking care of getting the key and value myself. When using C++ getopt_long was doing this for me.
i = 1
Do
Call Get_command_argument (i, arg)
If (Len_trim (arg) == 0) Exit
pos = Index (arg, "=")
!!$ Long option argument.
If (arg(1:2) == "--") Then
If (pos == 0) Then
c = arg
val = ""
Else
c = arg(:pos-1)
val = arg(pos+1:)
End If
!!$ Short option argument.
Elseif (arg(1:1) == "-") Then
c = arg
val = arg(pos+1:)
!!$ Non option argument.
Else
c = arg
val = arg
End If
!!$------------------------------------------------------------
Select Case (c)
Case ("-b","--brief")
arg_brief = .True.
Case ("-h","--help")
arg_help = .True.
Case ("-v","-V","--version")
arg_version = .True.
! Output model geographical definition
Case ("-cunit")
arg_cunit = val
Case default
arg_nonopt = Trim (Adjustl (arg))
Write (*,*) "Warning: Non option argument"
End Select
i = i + 1
End Do
!!$-------------------------------------------------------------
!!$ [TODO] Get numbers from arg_cunit
If (arg_cunit .contains. "-") Then
If (arg_cunit .contains. ",") Then
!!$ "-" and "," are present.
Else
!!$ "-" only are present.
End If
Else If (arg_cunit .contains. ",") Then
!!$ "," only are present
End If
推荐答案
当然可以使用 GET_COMMAND_ARGUMENT
,就像您已经做的那样,这很容易。问题的难点是根据这些参数字符串,用各种数字,逻辑和字符串值填充变量。这就是以下内容所做的事情:
Of course you can use GET_COMMAND_ARGUMENT
as you already do but that is the easy part. The hard part of the problem is to fill your variables with various numerical, logical and string values according to those argument strings. That is what the the following does:
一个人可以使用名称列表进行简单的参数解析。只需添加开始和结束标记。
One can use a namelist for easy argument parsing. Just add the begin and end markers. It is not too flexible, though, but very simple!
A(非常!)简短的Google搜索 getopt
的简单方法!展示了该库的几个Fortran端口(或包括解析在内的类似端口):
A (very!) short google search for getopt
reveals a couple of Fortran ports of this library (or similar ones which include parsing):
http://fortranwiki.org/fortran/show/getopt_long_module
> http://www.dominik-epple.de/getoptions/
http://lagrange.mechse.illinois .edu / partmc / partmc-2.4.0 / doc / html / getopt_8_f90_source.html
和
http://libsufr.sourceforge.net/doxygen/getopt_8f90_source.html (感谢AstroFloyd)
http://libsufr.sourceforge.net/doxygen/getopt_8f90_source.html (thanks to AstroFloyd)
这篇关于Fortran命令行参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!