GitXplorerGitXplorer
m

run-f

public
2 stars
0 forks
1 issues

Commits

List of commits on branch main.
Verified
a746db6a473056ee45024491f7b4213cfdd36c95

Merge pull request #1 from minhqdao/use-matrix-for-msys2

mminhqdao committed 6 months ago
Unverified
6410b862c3e62edd80c9facbfa413612feed54fd

Use fpm

mminhqdao committed 6 months ago
Unverified
1451db542431557744a94cca197b43f9d8e2abf3

Use correct name

mminhqdao committed 6 months ago
Unverified
41ab3aae7d9c4b910aa2963345ac5c4328cbfc96

Add build variant

mminhqdao committed 6 months ago
Unverified
1d6a6941b3031f8b483f68b4178182f806bd91b0

Use matrirx for msys2

mminhqdao committed 6 months ago
Unverified
1b65b939bd3d8a36155eddbdc2aeea84d90afc67

Fix shield

mminhqdao committed 6 months ago

README

The README file for this repository.

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.