-
Notifications
You must be signed in to change notification settings - Fork 194
Add get_argument, get_variable and set_variable #604
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
I found I almost always wanted a default for an environment variable and that I often had an alternate variable to look at so I think having a default would be nice; and because a functional approach is so easily nestable maybe consider a function instead of a subroutine, which also eliminates some issues with whether the result is allocatable or fixed length. If it is a function with a default things like in system_getenv program demo_system_getenv
use M_system, only : ge=>system_getenv
implicit none
character(len=:),allocatable :: TMPDIR,USER
! look first for USER then LOGNAME then USERNAME
USER=ge('USER', ge('LOGNAME', ge('USERNAME', 'UNKNOWN')))
TMPDIR= ge('TMPDIR', ge('TMP', ge('TEMPDIR', ge('TEMP', '/tmp'))))
write(*,*)'favorite scratch area is ',TMPDIR
end program demo_system_getenv |
Using a function sounds like a better idea, how do you handle the return status in this case? Also, do you have a good idea how to set variables? |
I actually just return a null string on error. I like functions to be pure, but you could return an optional parameter, but that is something I prefer to use a subroutine for; so the alternative would be a separate function that just returned an error code; sort of like having to check the allocation of a variable before allocating it, or testing for association. In practice I have found I rarely care about any error status; and if I did there is always the intrinsic itself for that case. In the M_system module there are procedures for setting environment variables but they only work on a POSIX platform; but it includes the routines for reading the environment table as well, which is handy for things like "show me all environment variable names starting with "FPM_" and for dumping the environment so you can restore it in a different process. I know there are equivalents on MS_WIndows but I very rarely use MSWindows except with WSL or CygWin so I have never made the routines work with MSWindows. I wonder if instead of always coming up against that if we should start a POSIX library for MSWIndows? It would not be trivial, but is obviously possible, as CygWin does it. |
Just about (maybe all) Fortran compilers seem to have common posix-like routines but I got tired of all the conditionals needed to use them when writing for multiple compilers; which resulted in M_system once the C interface was standardized; but I used to have wrappers for each compiler that normalized their versions of getenv, fstat, getcwd, ...; which was workable for the platforms I used. A horrible shame that got dropped from the standard; the PXF interface would have been a huge boon to Fortran but the only one I remember fully implementing it was Cray, which only had to do it for their own platform. Since we had a cray as a primary platform we converted a lot of codes to the PXF interface expecting it to become standard and were severely disappointed when everyone did not support it. Just as an example, gfortran has quite a few related extensions that could be wrapped as a seed, as I think just about everyone has a (different) simiilar extension; sometimes requiring a USE statement; almost always a different syntax • ABORT: Abort the program |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thank you. This sounds good to me.
Question: how to add tests for such procedures?
program demo | ||
use stdlib_system, only: sleep | ||
implicit none | ||
call sleep(150) | ||
end program demo |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
program demo | |
use stdlib_system, only: sleep | |
implicit none | |
call sleep(150) | |
end program demo | |
program demo_sleep | |
use stdlib_system, only: sleep | |
implicit none | |
call sleep(150) | |
end program demo_sleep |
program demo | ||
use stdlib_system, only: get_argument | ||
implicit none | ||
character(len=:), allocatable :: prog | ||
call get_argument(0, prog) | ||
print '(a)', prog | ||
end program demo |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
program demo | |
use stdlib_system, only: get_argument | |
implicit none | |
character(len=:), allocatable :: prog | |
call get_argument(0, prog) | |
print '(a)', prog | |
end program demo | |
program demo_get_argument | |
use stdlib_system, only: get_argument | |
implicit none | |
character(len=:), allocatable :: prog | |
call get_argument(0, prog) | |
print '(a)', prog | |
end program demo_get_argument |
This argument is `intent(out)`. | ||
|
||
|
||
#### Examples |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
#### Examples | |
#### Example |
program demo | ||
use stdlib_system, only: get_variable | ||
implicit none | ||
character(len=:), allocatable :: home | ||
call get_variable("HOME", home) | ||
print '(a)', home | ||
end program demo |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
program demo | |
use stdlib_system, only: get_variable | |
implicit none | |
character(len=:), allocatable :: home | |
call get_variable("HOME", home) | |
print '(a)', home | |
end program demo | |
program demo_get_variable | |
use stdlib_system, only: get_variable | |
implicit none | |
character(len=:), allocatable :: home | |
call get_variable("HOME", home) | |
print '(a)', home | |
end program demo_get_variable |
This argument is `intent(out)`. | ||
|
||
|
||
#### Examples |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
#### Examples | |
#### Example |
```fortran | ||
program demo | ||
use stdlib_system, only: set_variable | ||
implicit none | ||
call set_variable("OMP_NUM_THREADS", "1") | ||
end program demo |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
```fortran | |
program demo | |
use stdlib_system, only: set_variable | |
implicit none | |
call set_variable("OMP_NUM_THREADS", "1") | |
end program demo | |
```fortran | |
program demo_set_variable | |
use stdlib_system, only: set_variable | |
implicit none | |
call set_variable("OMP_NUM_THREADS", "1") | |
end program demo_set_variable |
Since the implementation of |
stdlib_system
module + documentationget_argument
to retrieve command line arguments in deferred length characters andstring_type
variablesget_variable
to retrieve environment variables in deferred length characters andstring_type
variablesset_variable
to write environment variables