I’m happy to announce that I’ve released a Modern Fortran version of the shlex
simple lexical analyzer, which is based on Python’s shlex
library. I’ve implemented this because it’s needed to solve this issue with the fpm
MPI metapackage, and there is no other way to address it unless we have a relatively full-featured command line parser for unix-like commands. (this is also what Meson uses for the same purpose).
So there we go, I hope it will be useful for that purpose and more.
fortran-shlex
Modern Fortran port of Python’s shlex shell-like lexer. This package implements the simple shlex lexer, inspired by the Python shlex module, and based on the Golang implementation. The interface comes with two functions, split
which parses a command-like string and returns an array of allocatable character strings; and shlex
that perform the same, but return a list of type(shlex_token)
tokens. Error control is optional, via a boolean success
keyword or a token that may return an error string.
Install
Just copy and paste shlex_module.f90
into your project. Alternatively, fortran-shlex
can be used as a dependency in your Fortran Package Manager project:
[dependencies]
shlex = { git="https://github.com/perazz/fortran-shlex.git" }
Usage
The split
function returns a list of strings, split according to unix shell rules, which support:
- escaping quotes (
"..."
) - non-escaping quotes (
'...'
) - line feed, carriage return etc.
use shlex_module
character(len=:), allocatable :: tokens(:)
type(shlex_token) :: error
logical :: success
! Simple usage
tokens = split('my -W"ery" -Llong //Input \n "string"')
! With logical error flag
tokens = split('whatever ',success=success)
! With complete error flag
tokens = split('whatever ',error)
print *, 'error message=',error%string
And the shlex
function has the same API, but returns a list of type(shlex_token)
s instead of an allocatable character array.
use shlex_module
type(shlex_token), allocatable :: tokens(:)
type(shlex_token) :: error
logical :: success
! Simple usage
tokens = shlex('my -W"ery" -Llong //Input \n "string"')
! With logical error flag
tokens = shlex('whatever ',success=success)
! With complete error flag
tokens = shlex('whatever ',error)
print *, 'error message=',error%string
License
The source code in this repository is Licensed under MIT license (LICENSE-MIT or The MIT License – Open Source Initiative).
Unless you explicitly state otherwise, any contribution intentionally submitted for inclusion in the work by you, as defined in the Apache-2.0 license, shall be dual licensed as above, without any additional terms or conditions.