I strongly support using one of the many command line parsing libraries available, as mentioned. However, in your particular case a simple program would do exactly what you asked for except that with some compiler and shell combinations quoting a filename can require double-quoting. For example:
program dummymain
implicit none
! define arguments and their default values
integer :: accmethod=2 ;namelist /args/ accmethod
integer :: massonly=1 ;namelist /args/ massonly
character(len=80) :: outfile='dummymain.lst' ;namelist /args/ outfile
character(len=256) :: input(3)=[character(len=256) :: '&args','','/']
character(len=256) :: message
integer :: i, ios
! read arguments from command line
do i=1,command_argument_count()
call get_command_argument(i,input(2))
read(input,nml=args,iostat=ios,iomsg=message)
if(ios.ne.0)then
write(*,*)'ERROR:'//trim(message)
stop 2
endif
enddo
write(*,nml=args)
end program dummymain
dummymain accmethod=10
technically you need to pass quotes or apostrophes around a string value
and shells often strip those characters from the command line values so
to conform to standard NAMELIST usage you need to do something like
dummymain outfile='"myfile.out"'
but most if not all compilers will work with an unquoted string as long
as it does not contain spaces or special characters, so it is an
extension but this will commonly work:
dummymain outfile=file.txt
numbers work great, even arrays and user-defined types but special
characters often need quoted to protect them from shell expansion.
Your request is unusual in that most requests are for something like ULS (Unix-Like Systems) syntax or MSWIndows DOS syntax:
dummymain --outfile 'myfile.out' --accmethod=10
dummymain /outfile myfile.out /accmethod 10
but you have asked for something very much like NAMELIST or bash or CDC NOS syntax (sans the commas).
Another method appropriate for bash/sh/fish and tcsh/csh users is to read environment variables and to call the program as part of an env(1) statement:
env outfile='file.txt' accmethod=20 dummymain
both methods work and are quite simple; but in the case of the environment variables you also have to convert the numeric values from strings to numerics; whereas with the first method using a NAMELIST group you do not.
PS;
Why I say this is “bash-like” is because it is, users just rarely use the built-in bash keyword-name syntax. If might surprise quite a few people that given the bash shell
#!/bin/bash
echo A is $A
echo B is $B
$ A=100 >out.txt B=200 ./a
is a completely standard way to run the script and place the output in the file “out.txt” with
contents like so:
A is 100
B is 200
the world just rarely sees it used that way, and if you start your bash shell with the -k switch
the script can be called as
a A=11 B=20
supporting exactly the syntax style in the original OP request. So it is VERY bash-like, but for some reason few users use bash that way.