Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Fortran command-line arguments

In g++ I was using getopt_long to handle command-line options. Does there exist the same thing for Gfortran?

I want to be able to pass aguments to some Fortran unit tests.

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 
like image 845
Zeus Avatar asked Apr 07 '26 11:04

Zeus


2 Answers

Look at COMMAND_ARGUMENT_COUNT and GET_COMMAND_ARGUMENT. e.g., in the gfortran manual. They are standard Fortran intrinsics.

like image 114
M. S. B. Avatar answered Apr 10 '26 23:04

M. S. B.


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 (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

and

http://libsufr.sourceforge.net/doxygen/getopt_8f90_source.html (thanks to AstroFloyd)

like image 28
Vladimir F Героям слава Avatar answered Apr 11 '26 01:04

Vladimir F Героям слава



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!