M_args(3fm) - [ARGUMENTS:M_args::INTRO] define a NAMELIST in a module template to provide command line argument parsing (LICENSE:PD)
Synopsis
Description
Examples
Author
License
Usage:
use M_args, only : get_namelist, print_dictionary, unnamed use M_args, only : get_arg use M_args, only : get_command_arguments_as_raw_namelist use M_args, only : get_command_arguments_stack use M_args, only : get_command_arguments_string use M_args, only : longest_command_argument use M_args, only : debug use M_args, only : oneline
Use the M_args__arguments(3fp) module template in the following example program to allow for command line parsing much like standard Unix command line parsing. Just change the variables defined in the NAMELIST. There are further details in the documentation for get_namelist(3f) and print_dictionary(3f), but for basic use starting with the example program should be sufficient.Then, your program can be called with forms like:
cmd -x 1.0 -y -20 --points 1,2,3 -title This is my title cmd --help *.dataA variable of the form LETTER_ becomes the uppercase keyword -LETTER, and negative values do not need quoted as values. Single-letter keywords are assumed to be used on the command line as short options with a single dash prefix, while multi-letter keywords are assumed to be long options. variable names may be equivalenced to allow for short and long versions of a keyword.
Sample program
!program demo_M_args module M_args__arguments use M_args, only : get_namelist, print_dictionary, unnamed, oneline! >>> CHANGE THIS ! declare and initialize a namelist. Letter_ denotes an uppercase short command keyword real :: x=111.1, y=222.2, z=333.3 real :: point(3)=[10.0,20.0,30.0] character(len=80) :: title=" " logical :: l=.false., l_=.false. logical :: help=.false., version=.false., v=.false., h=.false. equivalence (help,h),(version,v) namelist /args/ x,y,z,point,title,help,h,version,v,l,l_ ! << END OF CHANGES
contains subroutine get_args() integer :: ios character(len=255) :: message ! use for I/O error messages character(len=:),allocatable :: readme ! stores updated namelist character(len=10000) :: hold_namelist(60) hold_namelist= write(hold_namelist,nml=args,iostat=ios,iomsg=message) if(ios.eq.0)then readme=get_namelist(oneline(hold_namelist)) read(readme,nml=args,iostat=ios,iomsg=message) endif if(ios.ne.0)then write(*,("ERROR:",i0,1x,a))ios, trim(message) call print_dictionary() stop 1 endif end subroutine get_args end module M_args__arguments
program short use M_args__arguments, only : get_args, unnamed use M_args__arguments ! make user variables available implicit none integer :: i call get_args() ! crack command line options ! >> USER YOUR VARIABLES HERE. FOR EXAMPLE: write(*,*)VALUES ARE NOW , new_line(A),& &x ,x, new_line(A),& &y ,y, new_line(A),& &z ,z, new_line(A),& &point ,point, new_line(A),& &title ,title, new_line(A),& &help ,help,h ,h, new_line(A),& &version ,version,v ,v, new_line(A),& &l ,l, new_line(A),& &l_ ,l_ if(size(unnamed).gt.0)then write(*,(a))UNNAMED: write(*,(i6.6,3a))(i,[,unnamed(i),],i=1,size(unnamed)) endif !<< END OF EXAMPLE USAGE OF VARIABLES end program short !end program demo_M_args
John S. Urban, 2019
Public Domain
Nemo Release 3.1 | M_args (3) | February 23, 2025 |