Modern Fortran port of Python's shlex shell-like lexer, now with Windows mslex support.
This package provides two main lexer interfaces:
- shlex: a Unix-style shell lexer, modeled after Python's- shlex, and
- mslex: a Windows-style lexer that emulates- cmd.exeand- CommandLineToArgvWbehavior, inspired by the- mslexPython module.
The new MSLEX API allows parsing and quoting of strings according to Windows command-line conventions, which differ from POSIX shells.
Split a command-line string into arguments using Windows semantics.
Arguments:
- pattern(required): a- character(*)input string.
- like_cmd(optional): if- .true.(default), emulate both- cmd.exeand- CommandLineToArgvW.
- ucrt(optional): if- .true., parse using UCRT (modern CRT); use- msvcrt.dllif- .false.. If not provided, both methods are compared and if they differ (ambiguous pattern), an error is raised.
- success(optional): returns- .false.if the string is invalid.
- error(optional): alternative error return method, returns a- type(shlex_token)containing an error flag and message.
character(len=:), allocatable :: args(:)
logical :: ok
args = ms_split('"my file.txt" -DVALUE=42', like_cmd=.true., success=ok)
if (.not.ok) print *, 'mslex error'Quote a string so that it is parsed correctly by Windows command-line interpreters.
- If for_cmd = .true., quotes forcmd.exe, thenCommandLineToArgvW.
- If for_cmd = .false., quotes for direct use withCommandLineToArgvW.
print *, ms_quote('my file.txt')                 ! → "my file.txt"
print *, ms_quote('^& dangerous', for_cmd=.true.) ! → "^& dangerous"Just copy and paste shlex_module.f90 into your project. Or, use as a dependency in your Fortran Package Manager project:
[dependencies]
fortran-shlex = { git="https://github.com/perazz/fortran-shlex.git" }Splits a command-like string using Unix shell rules.
character(len=:), allocatable :: tokens(:)
tokens = split('gfortran -I /include "quoted string"')Same as split, but returns an type(shlex_token), allocatable, dimension(:) array.
- Handles escaping quotes ("...") and non-escaping quotes ('...')
- Skips newline and carriage return characters
- Returns an array of tokens or structured tokens
Use join_spaced=.true. to automatically combine flags and paths like:
tokens = split('gfortran -I /include -L /lib', join_spaced=.true.)
! Result: ["gfortran", "-I/include", "-L/lib"]Works for any single-letter compiler flags (-I, -L, -D, etc.) where the path does not start with -.
Use keep_quotes=.true. to retain original quoting:
tokens = split('"quoted string" unquoted', keep_quotes=.true.)
! Result: ['"quoted string"', 'unquoted']Note: if using join_spaced, you must also pass keep_quotes (both arguments required when either is set).
This project is licensed under the MIT License:
See LICENSE-MIT or https://opensource.org/licenses/MIT.
By contributing, you agree that your contributions may be dual licensed under the MIT and Apache-2.0 licenses.