Skip to content
/ run-f Public

Fortran library to execute a command in the command line and receive the output as a string.

License

Notifications You must be signed in to change notification settings

minhqdao/run-f

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

26 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

run-f

License Release CI

This Fortran library allows you to execute a command in the command line and receive the result as a string without the need for a temporary file.

It was inspired by this article and uses iso_c_binding to call popen, fgets and pclose from the C standard library.

Usage

First, import the run_f module into your Fortran code:

use run_f, only: run

Then you can use the run function to execute a command and save its result as a string:

character(len=:), allocatable :: output

output = run("whoami")

Error Handling

Use the optional has_error argument to check if an error occurred while executing the command:

character(len=:), allocatable :: output
logical :: has_error

output = run("whoami", has_error)
if (has_error) then
  print *, "Handle gracefully."; stop 1
end if

If you don't provide an error handler and something goes wrong while executing the command, the program will continue:

character(:), allocatable :: output

output = run("abcxyz")
print *, "This line will be executed."

Be careful with different shell behavior and directives. For example, executing "." will not return an error on Ubuntu (bash) but it will do so on macOS (zsh).

Print Command

You can also print the command before executing it by setting the optional print_cmd argument to .true.:

character(len=:), allocatable :: output

output = run("whoami", print_cmd=.true.)
print *, output

Output:

Running command: 'whoami'
minh

Install

fpm

Using fpm, you can simply add this package as a dependency to your fpm.toml file:

[dependencies]

[dependencies.run-f]
git = "https://github.com/minhqdao/run-f.git"
tag = "v0.1.0"

Then import the run_f module into your Fortran code:

use run_f, only: run

Run fpm build to download and compile the dependency.

Tests

Run tests with:

fpm test

Formatting

The CI will fail if the code is not formatted correctly. Please configure your editor to use fprettify and use an indentation width of 2 or run fprettify -i 2 -r . before committing.

Contribute

Feel free to create an issue in case you found a bug, have any questions or want to propose further improvements. Please stick to the existing coding style when opening a pull request.

License

You can use, redistribute and/or modify the code under the terms of the MIT License.