You can use so-called deferred character variables. They do not have a fixed constant length, and their use can be found in issues such as data entry .
However, even with a variable deferred length used as (for this is the syntax)
character(len=:), allocatable :: argument allocate(character(some_length) :: argument) ! For integer some_length call GET_COMMAND_ARGUMENT(1,argument) print*, argument end
still need to worry about what some_length should be. If we just choose 100, we will go back to where we were.
We need to worry about this because get_command_argument does not accept such a deferred length argument and allocates it to the desired length. it
character(len=:), allocatable :: argument call GET_COMMAND_ARGUMENT(1,argument) ! Will argument be allocated in the subroutine? print*, argument end
offers the answer no.
Now, to handle this, we will look at other (optional) arguments for get_command_argument . In particular, there is one called length :
character(len=:), allocatable :: argument integer arglen call GET_COMMAND_ARGUMENT(1,length=arglen) allocate(character(arglen) :: argument) call GET_COMMAND_ARGUMENT(1,value=argument) print*, argument end
Naturally, you can create a wrapper routine that takes a portable variable of length of deferred length and it all worked.
source share